diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-03-12 13:58:54 +0100 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-03-12 13:58:54 +0100 |
commit | 5ed8286e742597720d48e3bf6fc56938ba717f5e (patch) | |
tree | ef52db059c2d04ce620c0871fa41809b0b074b23 /lib | |
parent | 9d4c9272e016442cd84fbada82493b03b350bb8e (diff) | |
download | gitlab-ce-5ed8286e742597720d48e3bf6fc56938ba717f5e.tar.gz |
Extract variables collection item to a separate class
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/ci/variables/collection.rb | 33 | ||||
-rw-r--r-- | lib/gitlab/ci/variables/collection/item.rb | 46 |
2 files changed, 48 insertions, 31 deletions
diff --git a/lib/gitlab/ci/variables/collection.rb b/lib/gitlab/ci/variables/collection.rb index 2f5987f2846..83db5492d43 100644 --- a/lib/gitlab/ci/variables/collection.rb +++ b/lib/gitlab/ci/variables/collection.rb @@ -4,8 +4,6 @@ module Gitlab class Collection include Enumerable - Variable = Struct.new(:key, :value, :public, :file) - def initialize(variables = []) @variables = [] @@ -13,7 +11,7 @@ module Gitlab end def append(resource) - @variables.append(fabricate(resource)) + @variables.append(Collection::Item.fabricate(resource)) end def each @@ -27,35 +25,8 @@ module Gitlab end end - ## - # If `file: true` has been provided we expose it, otherwise we - # don't expose `file` attribute at all (stems from what the runner - # expects). - # def to_runner_variables - self.map do |variable| - variable.to_h.reject do |component, value| - component == :file && value == false - end - end - end - - private - - def fabricate(resource) - case resource - when Hash - Collection::Variable.new(resource.fetch(:key), - resource.fetch(:value), - resource.fetch(:public, false), - resource.fetch(:file, false)) - when ::Ci::Variable - Variable.new(resource.key, resource.value, false, false) - when Collection::Variable - resource.dup - else - raise ArgumentError, 'Unknown CI/CD variable resource!' - end + self.map(&:to_hash) end end end diff --git a/lib/gitlab/ci/variables/collection/item.rb b/lib/gitlab/ci/variables/collection/item.rb new file mode 100644 index 00000000000..4de96e97417 --- /dev/null +++ b/lib/gitlab/ci/variables/collection/item.rb @@ -0,0 +1,46 @@ +module Gitlab + module Ci + module Variables + class Collection + class Item + def initialize(**options) + @variable = { + key: options.fetch(:key), + value: options.fetch(:value), + public: options.fetch(:public, false), + file: options.fetch(:files, false) + } + end + + def ==(other) + to_hash == self.class.fabricate(other).to_hash + end + + ## + # If `file: true` has been provided we expose it, otherwise we + # don't expose `file` attribute at all (stems from what the runner + # expects). + # + def to_hash + @variable.reject do |hash_key, hash_value| + hash_key == :file && hash_value == false + end + end + + def self.fabricate(resource) + case resource + when Hash + self.new(resource) + when ::Ci::Variable + self.new(resource.to_hash) + when self + resource.dup + else + raise ArgumentError, 'Unknown CI/CD variable resource!' + end + end + end + end + end + end +end |