From 0a203f408786ae2e28616a0e1bf488595f7f115b Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 30 Mar 2018 10:32:28 +0200 Subject: Improve naming and optimize methods of the build class --- app/models/ci/build.rb | 10 ++++++++-- spec/models/ci/build_spec.rb | 10 +++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index b42555e46e4..18e96389199 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -289,7 +289,9 @@ module Ci # Variables that do not depend on the environment name. # def simple_variables - scoped_variables(environment: nil).to_runner_variables + strong_memoize(:simple_variables) do + scoped_variables(environment: nil).to_runner_variables + end end ## @@ -303,7 +305,11 @@ module Ci .to_runner_variables end - def variables_hash + ## + # Regular Ruby hash of scoped variables, without duplicates that are + # possible to be present in an array of hashes returned from `variables`. + # + def scoped_variables_hash scoped_variables.to_hash end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 4b9ddb3c831..a12717835b0 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -2070,7 +2070,7 @@ describe Ci::Build do end end - describe '#variables_hash' do + describe '#scoped_variables_hash' do context 'when overriding secret variables' do before do project.variables.create!(key: 'MY_VAR', value: 'my value 1') @@ -2078,8 +2078,8 @@ describe Ci::Build do end it 'returns a regular hash created using valid ordering' do - expect(build.variables_hash).to include('MY_VAR': 'my value 2') - expect(build.variables_hash).not_to include('MY_VAR': 'my value 1') + expect(build.scoped_variables_hash).to include('MY_VAR': 'my value 2') + expect(build.scoped_variables_hash).not_to include('MY_VAR': 'my value 1') end end @@ -2090,8 +2090,8 @@ describe Ci::Build do end it 'returns a hash including variable with higher precedence' do - expect(build.variables_hash).to include('MY_VAR': 'pipeline value') - expect(build.variables_hash).not_to include('MY_VAR': 'myvar') + expect(build.scoped_variables_hash).to include('MY_VAR': 'pipeline value') + expect(build.scoped_variables_hash).not_to include('MY_VAR': 'myvar') end end end -- cgit v1.2.1