Commit 06641a3f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Simplify undefined node definition in CI config

parent d41d3301
...@@ -51,7 +51,7 @@ module Gitlab ...@@ -51,7 +51,7 @@ module Gitlab
def helpers(*nodes) def helpers(*nodes)
nodes.each do |symbol| nodes.each do |symbol|
define_method("#{symbol}_defined?") do define_method("#{symbol}_defined?") do
@entries[symbol].try(:defined?) @entries[symbol].specified?
end end
define_method("#{symbol}_value") do define_method("#{symbol}_value") do
......
...@@ -66,14 +66,14 @@ module Gitlab ...@@ -66,14 +66,14 @@ module Gitlab
@config @config
else else
meaningful = @entries.select do |_key, value| meaningful = @entries.select do |_key, value|
value.defined? && value.relevant? value.specified? && value.relevant?
end end
Hash[meaningful.map { |key, entry| [key, entry.value] }] Hash[meaningful.map { |key, entry| [key, entry.value] }]
end end
end end
def defined? def specified?
true true
end end
......
...@@ -40,11 +40,21 @@ module Gitlab ...@@ -40,11 +40,21 @@ module Gitlab
# See issue #18775. # See issue #18775.
# #
if @value.nil? if @value.nil?
Node::Undefined.new(@node, attributes) Node::Undefined.new(fabricate_undefined(attributes))
else else
@node.new(@value, attributes) @node.new(@value, attributes)
end end
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 end
end end
......
...@@ -8,10 +8,6 @@ module Gitlab ...@@ -8,10 +8,6 @@ module Gitlab
# Implements the Null Object pattern. # Implements the Null Object pattern.
# #
class Null < Entry class Null < Entry
def initialize(config = nil, **attributes)
super
end
def value def value
nil nil
end end
...@@ -23,6 +19,14 @@ module Gitlab ...@@ -23,6 +19,14 @@ module Gitlab
def errors def errors
[] []
end end
def specified?
false
end
def relevant?
false
end
end end
end end
end end
......
...@@ -3,66 +3,19 @@ module Gitlab ...@@ -3,66 +3,19 @@ module Gitlab
class Config class Config
module Node 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 # It decorates original entry adding method that idicates it is
# if original entry has a default value. If there is default value # unspecified.
# some methods are delegated to it.
# #
# class Undefined < SimpleDelegator
class Undefined < Entry def initialize(entry)
include Validatable
delegate :valid?, :errors, :value, to: :@strategy
validations do
validates :config, type: Class
end
def initialize(node, **attributes)
super super
@strategy = create_strategy(node, node.default)
end end
def defined? def specified?
false false
end 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 end
end end
......
...@@ -233,9 +233,9 @@ describe Gitlab::Ci::Config::Node::Global do ...@@ -233,9 +233,9 @@ describe Gitlab::Ci::Config::Node::Global do
end end
end end
describe '#defined?' do describe '#specified?' do
it 'is concrete entry that is defined' do it 'is concrete entry that is defined' do
expect(global.defined?).to be true expect(global.specified?).to be true
end end
end end
end end
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Config::Node::Null do describe Gitlab::Ci::Config::Node::Null do
let(:null) { described_class.new } let(:null) { described_class.new(nil) }
describe '#leaf?' do describe '#leaf?' do
it 'is leaf node' do it 'is leaf node' do
...@@ -26,4 +26,16 @@ describe Gitlab::Ci::Config::Node::Null do ...@@ -26,4 +26,16 @@ describe Gitlab::Ci::Config::Node::Null do
expect(null.value).to eq nil expect(null.value).to eq nil
end end
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 end
...@@ -4,66 +4,29 @@ describe Gitlab::Ci::Config::Node::Undefined do ...@@ -4,66 +4,29 @@ describe Gitlab::Ci::Config::Node::Undefined do
let(:undefined) { described_class.new(entry) } let(:undefined) { described_class.new(entry) }
let(:entry) { spy('Entry') } let(:entry) { spy('Entry') }
context 'when entry does not have a default value' do describe '#valid?' do
before { allow(entry).to receive(:default).and_return(nil) } it 'delegates method to entry' do
expect(undefined.valid).to eq entry
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
end end
end end
context 'when entry has a default value' do describe '#errors' do
before do it 'delegates method to entry' do
allow(entry).to receive(:default).and_return('some value') expect(undefined.errors).to eq entry
allow(entry).to receive(:value).and_return('some value')
end end
end
describe '#value' do describe '#value' do
it 'returns default value for entry' do it 'delegates method to entry' do
expect(undefined.value).to eq 'some value' expect(undefined.value).to eq entry
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
end end
end end
describe '#undefined?' do describe '#specified?' do
it 'is not a defined entry' do it 'is always false' do
expect(undefined.defined?).to be false allow(entry).to receive(:specified?).and_return(true)
expect(undefined.specified?).to be false
end end
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