2018-11-29 06:44:48 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Config
|
|
|
|
module Entry
|
|
|
|
class Simplifiable < SimpleDelegator
|
2019-11-08 07:06:32 -05:00
|
|
|
EntryStrategy = Struct.new(:name, :klass, :condition)
|
2018-11-29 06:44:48 -05:00
|
|
|
|
2019-01-15 05:23:32 -05:00
|
|
|
attr_reader :subject
|
|
|
|
|
2019-11-08 07:06:32 -05:00
|
|
|
def initialize(config, **metadata, &blk)
|
2018-11-29 06:44:48 -05:00
|
|
|
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)
|
|
|
|
|
2020-09-30 05:10:11 -04:00
|
|
|
@subject = entry.new(config, **metadata, &blk)
|
2019-04-03 05:50:54 -04:00
|
|
|
|
|
|
|
super(@subject)
|
2018-11-29 06:44:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.strategy(name, **opts)
|
2019-11-08 07:06:32 -05:00
|
|
|
EntryStrategy.new(name, opts.dig(:class), opts.fetch(:if)).tap do |strategy|
|
2018-11-29 06:44:48 -05:00
|
|
|
strategies.append(strategy)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.strategies
|
|
|
|
@strategies ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.entry_class(strategy)
|
|
|
|
if strategy.present?
|
2019-11-08 07:06:32 -05:00
|
|
|
strategy.klass || self.const_get(strategy.name, false)
|
2018-11-29 06:44:48 -05:00
|
|
|
else
|
|
|
|
self::UnknownStrategy
|
|
|
|
end
|
|
|
|
end
|
2019-01-14 08:22:13 -05:00
|
|
|
|
2019-01-14 11:27:13 -05:00
|
|
|
def self.default
|
2019-01-14 08:22:13 -05:00
|
|
|
end
|
2018-11-29 06:44:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|