Commit cc373a35 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add factory for fabricating new ci config nodes

parent 5abfc7fa
...@@ -30,19 +30,10 @@ module Gitlab ...@@ -30,19 +30,10 @@ module Gitlab
private private
def add_node(key, metadata) def create_node(key, factory)
entry = create_entry(key, metadata[:class]) factory.with_value(@value[key])
entry.description = metadata[:description] factory.null_node unless @value.has_key?(key)
factory.create!
@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
end end
class_methods do class_methods do
...@@ -51,9 +42,8 @@ module Gitlab ...@@ -51,9 +42,8 @@ module Gitlab
private private
def allow_node(symbol, entry_class, metadata) def allow_node(symbol, entry_class, metadata)
node = { symbol.to_sym => factory = Node::Factory.new(entry_class)
{ class: entry_class, .with_description(metadata[:description])
description: metadata[:description] } }
define_method(symbol) do define_method(symbol) do
raise Entry::InvalidError unless valid? raise Entry::InvalidError unless valid?
...@@ -61,7 +51,7 @@ module Gitlab ...@@ -61,7 +51,7 @@ module Gitlab
@nodes[symbol].try(:value) @nodes[symbol].try(:value)
end end
(@allowed_nodes ||= {}).merge!(node) (@allowed_nodes ||= {}).merge!(symbol => factory)
end end
end end
end end
......
...@@ -27,8 +27,8 @@ module Gitlab ...@@ -27,8 +27,8 @@ module Gitlab
end end
def compose! def compose!
allowed_nodes.each do |key, entry| allowed_nodes.each do |key, factory|
add_node(key, entry) @nodes[key] = create_node(key, factory.dup)
end end
end end
...@@ -52,7 +52,7 @@ module Gitlab ...@@ -52,7 +52,7 @@ module Gitlab
{} {}
end end
def add_node(key, entry) def validate!
raise NotImplementedError raise NotImplementedError
end end
...@@ -60,7 +60,9 @@ module Gitlab ...@@ -60,7 +60,9 @@ module Gitlab
raise NotImplementedError raise NotImplementedError
end end
def validate! private
def create_node(key, factory)
raise NotImplementedError raise NotImplementedError
end end
end end
......
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
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Factory do
describe '#create!' do
let(:factory) { described_class.new(entry_class) }
let(:entry_class) { Gitlab::Ci::Config::Node::Script }
context 'when value setting value' do
it 'creates entry with valid value' do
entry = factory
.with_value(['ls', 'pwd'])
.create!
expect(entry.value).to eq "ls\npwd"
end
context 'when setting description' do
it 'creates entry with description' do
entry = factory
.with_value(['ls', 'pwd'])
.with_description('test description')
.create!
expect(entry.value).to eq "ls\npwd"
expect(entry.description).to eq 'test description'
end
end
end
context 'when not setting value' do
it 'raises error' do
expect { factory.create! }.to raise_error(
Gitlab::Ci::Config::Node::Factory::InvalidFactory
)
end
end
context 'when creating a null entry' do
it 'creates a null entry' do
entry = factory
.with_value(nil)
.null_node
.create!
expect(entry).to be_an_instance_of Gitlab::Ci::Config::Node::Null
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment