Commit 0d7d7c10 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use aspect-oriented design in CI/CD config entries

parent 5ced2d8d
...@@ -15,9 +15,10 @@ module Gitlab ...@@ -15,9 +15,10 @@ module Gitlab
# #
module Configurable module Configurable
extend ActiveSupport::Concern extend ActiveSupport::Concern
include Validatable
included do included do
include Validatable
validations do validations do
validates :config, type: Hash validates :config, type: Hash
end end
......
...@@ -16,8 +16,9 @@ module Gitlab ...@@ -16,8 +16,9 @@ module Gitlab
@metadata = metadata @metadata = metadata
@entries = {} @entries = {}
@validator = self.class.validator.new(self) self.class.aspects.to_a.each do |aspect|
@validator.validate(:new) instance_exec(&aspect)
end
end end
def [](key) def [](key)
...@@ -47,7 +48,7 @@ module Gitlab ...@@ -47,7 +48,7 @@ module Gitlab
end end
def errors def errors
@validator.messages + descendants.flat_map(&:errors) []
end end
def value def value
...@@ -79,8 +80,8 @@ module Gitlab ...@@ -79,8 +80,8 @@ module Gitlab
def self.default def self.default
end end
def self.validator def self.aspects
Validator @aspects ||= []
end end
end end
end end
......
...@@ -5,6 +5,17 @@ module Gitlab ...@@ -5,6 +5,17 @@ module Gitlab
module Validatable module Validatable
extend ActiveSupport::Concern extend ActiveSupport::Concern
def self.included(node)
node.aspects.append -> do
@validator = self.class.validator.new(self)
@validator.validate(:new)
end
end
def errors
@validator.messages + descendants.flat_map(&:errors)
end
class_methods do class_methods do
def validator def validator
@validator ||= Class.new(Entry::Validator).tap do |validator| @validator ||= Class.new(Entry::Validator).tap do |validator|
......
...@@ -30,7 +30,7 @@ module Gitlab ...@@ -30,7 +30,7 @@ module Gitlab
def key_name def key_name
if key.blank? if key.blank?
@entry.class.name.demodulize.underscore.humanize @entry.class.name.to_s.demodulize.underscore.humanize
else else
key key
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Config::Entry::Configurable do describe Gitlab::Ci::Config::Entry::Configurable do
let(:entry) { Class.new } let(:entry) do
Class.new(Gitlab::Ci::Config::Entry::Node) do
before do include Gitlab::Ci::Config::Entry::Configurable
entry.include(described_class) end
end end
describe 'validations' do describe 'validations' do
let(:validator) { entry.validator.new(instance) } context 'when entry is a hash' do
let(:instance) { entry.new(key: 'value') }
before do
entry.class_eval do
attr_reader :config
def initialize(config) it 'correctly validates an instance' do
@config = config expect(instance).to be_valid
end
end end
validator.validate
end end
context 'when entry validator is invalid' do context 'when entry is not a hash' do
let(:instance) { entry.new('ls') } let(:instance) { entry.new('ls') }
it 'returns invalid validator' do it 'invalidates the instance' do
expect(validator).to be_invalid expect(instance).not_to be_valid
end
end
context 'when entry instance is valid' do
let(:instance) { entry.new(key: 'value') }
it 'returns valid validator' do
expect(validator).to be_valid
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Config::Entry::Validatable do describe Gitlab::Ci::Config::Entry::Validatable do
let(:entry) { Class.new } let(:entry) do
Class.new(Gitlab::Ci::Config::Entry::Node) do
before do include Gitlab::Ci::Config::Entry::Validatable
entry.include(described_class) end
end end
describe '.validator' do describe '.validator' do
...@@ -28,7 +28,7 @@ describe Gitlab::Ci::Config::Entry::Validatable do ...@@ -28,7 +28,7 @@ describe Gitlab::Ci::Config::Entry::Validatable do
end end
context 'when validating entry instance' do context 'when validating entry instance' do
let(:entry_instance) { entry.new } let(:entry_instance) { entry.new('something') }
context 'when attribute is valid' do context 'when attribute is valid' do
before do before do
......
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