2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Config
|
|
|
|
module Entry
|
|
|
|
class Simplifiable < SimpleDelegator
|
2019-12-26 22:10:19 +05:30
|
|
|
EntryStrategy = Struct.new(:name, :klass, :condition)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
attr_reader :subject
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def initialize(config, **metadata, &blk)
|
2019-02-15 15:39:39 +05:30
|
|
|
unless self.class.const_defined?(:UnknownStrategy)
|
|
|
|
raise ArgumentError, 'UndefinedStrategy not available!'
|
|
|
|
end
|
|
|
|
|
|
|
|
strategy = self.class.strategies.find do |variant|
|
|
|
|
variant.condition.call(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
entry = self.class.entry_class(strategy)
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
@subject = entry.new(config, **metadata, &blk)
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
super(@subject)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.strategy(name, **opts)
|
2019-12-26 22:10:19 +05:30
|
|
|
EntryStrategy.new(name, opts.dig(:class), opts.fetch(:if)).tap do |strategy|
|
2019-02-15 15:39:39 +05:30
|
|
|
strategies.append(strategy)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.strategies
|
|
|
|
@strategies ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.entry_class(strategy)
|
|
|
|
if strategy.present?
|
2019-12-26 22:10:19 +05:30
|
|
|
strategy.klass || self.const_get(strategy.name, false)
|
2019-02-15 15:39:39 +05:30
|
|
|
else
|
|
|
|
self::UnknownStrategy
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
def self.default
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|