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
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
......
......@@ -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
......
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