Simplify undefined node definition in CI config

This commit is contained in:
Grzegorz Bizon 2016-07-12 14:10:18 +02:00
parent d41d330147
commit 06641a3fee
8 changed files with 60 additions and 118 deletions

View file

@ -51,7 +51,7 @@ module Gitlab
def helpers(*nodes)
nodes.each do |symbol|
define_method("#{symbol}_defined?") do
@entries[symbol].try(:defined?)
@entries[symbol].specified?
end
define_method("#{symbol}_value") do

View file

@ -66,14 +66,14 @@ module Gitlab
@config
else
meaningful = @entries.select do |_key, value|
value.defined? && value.relevant?
value.specified? && value.relevant?
end
Hash[meaningful.map { |key, entry| [key, entry.value] }]
end
end
def defined?
def specified?
true
end

View file

@ -40,11 +40,21 @@ module Gitlab
# See issue #18775.
#
if @value.nil?
Node::Undefined.new(@node, attributes)
Node::Undefined.new(fabricate_undefined(attributes))
else
@node.new(@value, attributes)
end
end
private
def fabricate_undefined(attributes)
if @node.default.nil?
Node::Null.new(nil, attributes)
else
@node.new(@node.default, attributes)
end
end
end
end
end

View file

@ -8,10 +8,6 @@ module Gitlab
# Implements the Null Object pattern.
#
class Null < Entry
def initialize(config = nil, **attributes)
super
end
def value
nil
end
@ -23,6 +19,14 @@ module Gitlab
def errors
[]
end
def specified?
false
end
def relevant?
false
end
end
end
end

View file

@ -3,66 +3,19 @@ module Gitlab
class Config
module Node
##
# This class represents an undefined entry node.
# This class represents an undefined and unspecified entry node.
#
# It takes original entry class as configuration and creates an object
# if original entry has a default value. If there is default value
# some methods are delegated to it.
# It decorates original entry adding method that idicates it is
# unspecified.
#
#
class Undefined < Entry
include Validatable
delegate :valid?, :errors, :value, to: :@strategy
validations do
validates :config, type: Class
end
def initialize(node, **attributes)
class Undefined < SimpleDelegator
def initialize(entry)
super
@strategy = create_strategy(node, node.default)
end
def defined?
def specified?
false
end
private
def create_strategy(node, default)
if default.nil?
Undefined::NullStrategy.new
else
entry = node.new(default, attributes)
Undefined::DefaultStrategy.new(entry)
end
end
class DefaultStrategy
delegate :valid?, :errors, :value, to: :@default
def initialize(entry)
@default = entry
end
end
class NullStrategy
def initialize(*)
end
def value
nil
end
def valid?
true
end
def errors
[]
end
end
end
end
end

View file

@ -233,9 +233,9 @@ describe Gitlab::Ci::Config::Node::Global do
end
end
describe '#defined?' do
describe '#specified?' do
it 'is concrete entry that is defined' do
expect(global.defined?).to be true
expect(global.specified?).to be true
end
end
end

View file

@ -1,7 +1,7 @@
require 'spec_helper'
describe Gitlab::Ci::Config::Node::Null do
let(:null) { described_class.new }
let(:null) { described_class.new(nil) }
describe '#leaf?' do
it 'is leaf node' do
@ -26,4 +26,16 @@ describe Gitlab::Ci::Config::Node::Null do
expect(null.value).to eq nil
end
end
describe '#relevant?' do
it 'is not relevant' do
expect(null.relevant?).to eq false
end
end
describe '#specified?' do
it 'is not defined' do
expect(null.specified?).to eq false
end
end
end

View file

@ -4,66 +4,29 @@ describe Gitlab::Ci::Config::Node::Undefined do
let(:undefined) { described_class.new(entry) }
let(:entry) { spy('Entry') }
context 'when entry does not have a default value' do
before { allow(entry).to receive(:default).and_return(nil) }
describe '#leaf?' do
it 'is leaf node' do
expect(undefined).to be_leaf
end
end
describe '#valid?' do
it 'is always valid' do
expect(undefined).to be_valid
end
end
describe '#errors' do
it 'is does not contain errors' do
expect(undefined.errors).to be_empty
end
end
describe '#value' do
it 'returns nil' do
expect(undefined.value).to eq nil
end
describe '#valid?' do
it 'delegates method to entry' do
expect(undefined.valid).to eq entry
end
end
context 'when entry has a default value' do
before do
allow(entry).to receive(:default).and_return('some value')
allow(entry).to receive(:value).and_return('some value')
end
describe '#value' do
it 'returns default value for entry' do
expect(undefined.value).to eq 'some value'
end
end
describe '#errors' do
it 'delegates errors to default entry' do
expect(entry).to receive(:errors)
undefined.errors
end
end
describe '#valid?' do
it 'delegates valid? to default entry' do
expect(entry).to receive(:valid?)
undefined.valid?
end
describe '#errors' do
it 'delegates method to entry' do
expect(undefined.errors).to eq entry
end
end
describe '#undefined?' do
it 'is not a defined entry' do
expect(undefined.defined?).to be false
describe '#value' do
it 'delegates method to entry' do
expect(undefined.value).to eq entry
end
end
describe '#specified?' do
it 'is always false' do
allow(entry).to receive(:specified?).and_return(true)
expect(undefined.specified?).to be false
end
end
end