Commit a42cce1b authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve code, remove unused validator, improve names

parent 69dad967
...@@ -8,8 +8,10 @@ module Gitlab ...@@ -8,8 +8,10 @@ module Gitlab
class Cache < Entry class Cache < Entry
include Configurable include Configurable
ALLOWED_KEYS = %i[key untracked paths]
validations do validations do
validates :config, allowed_keys: %i[key untracked paths] validates :config, allowed_keys: ALLOWED_KEYS
end end
node :key, Node::Key, node :key, Node::Key,
......
...@@ -11,22 +11,20 @@ module Gitlab ...@@ -11,22 +11,20 @@ module Gitlab
validations do validations do
include LegacyValidationHelpers include LegacyValidationHelpers
validate :string_or_array_of_strings validate do
unless string_or_array_of_strings?(config)
def string_or_array_of_strings
unless config_valid?
errors.add(:config, errors.add(:config,
'should be a string or an array of strings') 'should be a string or an array of strings')
end end
end end
def config_valid? def string_or_array_of_strings?(field)
validate_string(config) || validate_array_of_strings(config) validate_string(field) || validate_array_of_strings(field)
end end
end end
def value def value
[@config].flatten Array(@config)
end end
end end
end end
......
...@@ -56,10 +56,9 @@ module Gitlab ...@@ -56,10 +56,9 @@ module Gitlab
end end
define_method("#{symbol}_value") do define_method("#{symbol}_value") do
if @entries[symbol] return unless @entries[symbol] && @entries[symbol].valid?
return unless @entries[symbol].valid?
@entries[symbol].value @entries[symbol].value
end
end end
alias_method symbol.to_sym, "#{symbol}_value".to_sym alias_method symbol.to_sym, "#{symbol}_value".to_sym
......
...@@ -42,7 +42,7 @@ module Gitlab ...@@ -42,7 +42,7 @@ module Gitlab
super super
compose_jobs! compose_jobs!
compose_stages! compose_deprecated_entries!
end end
def compose_jobs! def compose_jobs!
...@@ -54,7 +54,7 @@ module Gitlab ...@@ -54,7 +54,7 @@ module Gitlab
@entries[:jobs] = factory.create! @entries[:jobs] = factory.create!
end end
def compose_stages! def compose_deprecated_entries!
## ##
# Deprecated `:types` key workaround - if types are defined and # Deprecated `:types` key workaround - if types are defined and
# stages are not defined we use types definition as stages. # stages are not defined we use types definition as stages.
......
...@@ -66,10 +66,10 @@ module Gitlab ...@@ -66,10 +66,10 @@ module Gitlab
node :services, Services, node :services, Services,
description: 'Services that will be used to execute this job.' description: 'Services that will be used to execute this job.'
node :only, While, node :only, Trigger,
description: 'Refs policy this job will be executed for.' description: 'Refs policy this job will be executed for.'
node :except, While, node :except, Trigger,
description: 'Refs policy this job will be executed for.' description: 'Refs policy this job will be executed for.'
node :variables, Variables, node :variables, Variables,
......
...@@ -3,7 +3,7 @@ module Gitlab ...@@ -3,7 +3,7 @@ module Gitlab
class Config class Config
module Node module Node
## ##
# This class represents an undefined and unspecified node. # This class represents an undefined node.
# #
# Implements the Null Object pattern. # Implements the Null Object pattern.
# #
......
...@@ -3,9 +3,9 @@ module Gitlab ...@@ -3,9 +3,9 @@ module Gitlab
class Config class Config
module Node module Node
## ##
# Entry that represents a ref and trigger policy for the job. # Entry that represents a trigger policy for the job.
# #
class While < Entry class Trigger < Entry
include Validatable include Validatable
validations do validations do
......
...@@ -3,16 +3,12 @@ module Gitlab ...@@ -3,16 +3,12 @@ module Gitlab
class Config class Config
module Node module Node
## ##
# This class represents an undefined and unspecified entry node. # This class represents an unspecified entry node.
# #
# It decorates original entry adding method that indicates it is # It decorates original entry adding method that indicates it is
# unspecified. # unspecified.
# #
class Undefined < SimpleDelegator class Undefined < SimpleDelegator
def initialize(entry)
super
end
def specified? def specified?
false false
end end
......
...@@ -44,15 +44,6 @@ module Gitlab ...@@ -44,15 +44,6 @@ module Gitlab
end end
end end
class RequiredValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value.nil?
raise Entry::InvalidError,
"Entry needs #{attribute} attribute set internally."
end
end
end
class KeyValidator < ActiveModel::EachValidator class KeyValidator < ActiveModel::EachValidator
include LegacyValidationHelpers include LegacyValidationHelpers
......
...@@ -8,7 +8,7 @@ describe Gitlab::Ci::Config::Node::Artifacts do ...@@ -8,7 +8,7 @@ describe Gitlab::Ci::Config::Node::Artifacts do
let(:config) { { paths: %w[public/] } } let(:config) { { paths: %w[public/] } }
describe '#value' do describe '#value' do
it 'returns image string' do it 'returns artifacs configuration' do
expect(entry.value).to eq config expect(entry.value).to eq config
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Config::Node::While do describe Gitlab::Ci::Config::Node::Trigger do
let(:entry) { described_class.new(config) } let(:entry) { described_class.new(config) }
describe 'validations' do describe 'validations' do
...@@ -48,7 +48,7 @@ describe Gitlab::Ci::Config::Node::While do ...@@ -48,7 +48,7 @@ describe Gitlab::Ci::Config::Node::While do
describe '#errors' do describe '#errors' do
it 'saves errors' do it 'saves errors' do
expect(entry.errors) expect(entry.errors)
.to include 'while config should be an array of strings or regexps' .to include 'trigger config should be an array of strings or regexps'
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