diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-06-10 14:01:07 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-06-10 14:01:07 +0200 |
commit | cc373a35504bc1f92f1a040c87a712a6480757ec (patch) | |
tree | e0d0b254668c241749b41aa1d23bd91cfe8385cf /lib | |
parent | 5abfc7fa7157e876299d1675f1cc96b78a3feadc (diff) | |
download | gitlab-ce-cc373a35504bc1f92f1a040c87a712a6480757ec.tar.gz |
Add factory for fabricating new ci config nodes
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/ci/config/node/configurable.rb | 24 | ||||
-rw-r--r-- | lib/gitlab/ci/config/node/entry.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/ci/config/node/factory.rb | 44 |
3 files changed, 57 insertions, 21 deletions
diff --git a/lib/gitlab/ci/config/node/configurable.rb b/lib/gitlab/ci/config/node/configurable.rb index b72bc0d592a..650c6efba63 100644 --- a/lib/gitlab/ci/config/node/configurable.rb +++ b/lib/gitlab/ci/config/node/configurable.rb @@ -30,19 +30,10 @@ module Gitlab private - def add_node(key, metadata) - entry = create_entry(key, metadata[:class]) - entry.description = metadata[:description] - - @nodes[key] = entry - end - - def create_entry(key, entry_class) - if @value.has_key?(key) - entry_class.new(@value[key]) - else - Node::Null.new(nil) - end + def create_node(key, factory) + factory.with_value(@value[key]) + factory.null_node unless @value.has_key?(key) + factory.create! end class_methods do @@ -51,9 +42,8 @@ module Gitlab private def allow_node(symbol, entry_class, metadata) - node = { symbol.to_sym => - { class: entry_class, - description: metadata[:description] } } + factory = Node::Factory.new(entry_class) + .with_description(metadata[:description]) define_method(symbol) do raise Entry::InvalidError unless valid? @@ -61,7 +51,7 @@ module Gitlab @nodes[symbol].try(:value) end - (@allowed_nodes ||= {}).merge!(node) + (@allowed_nodes ||= {}).merge!(symbol => factory) end end end diff --git a/lib/gitlab/ci/config/node/entry.rb b/lib/gitlab/ci/config/node/entry.rb index f7649784c28..2f327fa9bf3 100644 --- a/lib/gitlab/ci/config/node/entry.rb +++ b/lib/gitlab/ci/config/node/entry.rb @@ -27,8 +27,8 @@ module Gitlab end def compose! - allowed_nodes.each do |key, entry| - add_node(key, entry) + allowed_nodes.each do |key, factory| + @nodes[key] = create_node(key, factory.dup) end end @@ -52,7 +52,7 @@ module Gitlab {} end - def add_node(key, entry) + def validate! raise NotImplementedError end @@ -60,7 +60,9 @@ module Gitlab raise NotImplementedError end - def validate! + private + + def create_node(key, factory) raise NotImplementedError end end diff --git a/lib/gitlab/ci/config/node/factory.rb b/lib/gitlab/ci/config/node/factory.rb new file mode 100644 index 00000000000..969af45272e --- /dev/null +++ b/lib/gitlab/ci/config/node/factory.rb @@ -0,0 +1,44 @@ +module Gitlab + module Ci + class Config + module Node + ## + # Factory class responsible for fabricating node entry objects. + # + # It uses Fluent Interface pattern to set all necessary attributes. + # + class Factory + class InvalidFactory < StandardError; end + + def initialize(entry_class) + @entry_class = entry_class + @attributes = {} + end + + def with_value(value) + @attributes[:value] = value + self + end + + def with_description(description) + @attributes[:description] = description + self + end + + def null_node + @entry_class = Node::Null + self + end + + def create! + raise InvalidFactory unless @attributes.has_key?(:value) + + @entry_class.new(@attributes[:value]).tap do |entry| + entry.description = @attributes[:description] + end + end + end + end + end + end +end |