Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-09-22 13:17:01 +0300
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-09-22 13:29:06 +0300
commit87637693acbd443c674bcbf26d87281156a70761 (patch)
tree5e9acb924d93b27c1485bb1e5fdcdc1fcda180d1 /lib/gitlab/ci/variables
parent26607a1690631916baf5a39d198ff7096cb5bde6 (diff)
Introduce CI/CD variables collection class
Diffstat (limited to 'lib/gitlab/ci/variables')
-rw-r--r--lib/gitlab/ci/variables/collection.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/gitlab/ci/variables/collection.rb b/lib/gitlab/ci/variables/collection.rb
new file mode 100644
index 00000000000..0d830293c3c
--- /dev/null
+++ b/lib/gitlab/ci/variables/collection.rb
@@ -0,0 +1,60 @@
+module Gitlab
+ module Ci
+ module Variables
+ class Collection
+ include Enumerable
+
+ Variable = Struct.new(:key, :value, :public, :file)
+
+ def initialize(variables = [])
+ @variables = []
+
+ variables.each { |variable| append(variable) }
+ end
+
+ def append(resource)
+ @variables.append(fabricate(resource))
+ end
+
+ def each
+ @variables.each { |variable| yield variable }
+ end
+
+ def +(other)
+ self.class.new.tap do |collection|
+ self.each { |variable| collection.append(variable) }
+ other.each { |variable| collection.append(variable) }
+ end
+ end
+
+ def to_h
+ self.map do |variable|
+ variable.to_h.reject do |key, value|
+ key == :file && value == false
+ end
+ end
+ end
+
+ alias_method :to_hash, :to_h
+
+ private
+
+ def fabricate(resource)
+ case resource
+ when Hash
+ 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
+ end
+ end
+ end
+ end
+end