lib/evil/client/settings.rb in evil-client-1.1.0 vs lib/evil/client/settings.rb in evil-client-2.0.0
- old
+ new
@@ -2,20 +2,43 @@
#
# Container for settings assigned to some operation or scope.
#
class Settings
Names.clean(self) # Remove unnecessary methods from the instance
- require_relative "settings/validator"
extend ::Dry::Initializer
+ @policy = Policy
+
class << self
- # The schema klass settings belongs to
+ # Subclasses itself for a given schema
#
+ # @param [Class] schema
+ # @return [Class] a subclass for the schema
+ #
+ def for(schema)
+ Class.new(self).tap do |klass|
+ klass.send(:instance_variable_set, :@schema, schema)
+ end
+ end
+
+ # Reference to the schema klass the settings belongs to
+ #
# @return [Class]
#
- attr_reader :schema
+ attr_reader :schema, :locale
+ # Human-friendly representation of settings class
+ #
+ # @return [String]
+ #
+ def name
+ super || @schema.to_s
+ end
+ alias_method :to_s, :name
+ alias_method :to_str, :name
+ alias_method :inspect, :name
+
# Only options can be defined for the settings container
# @private
def param(*args)
option(*args)
end
@@ -52,61 +75,59 @@
instance_variable_set(:"@#{key}", instance_exec(&block))
end
self
end
- # Define validator for the attribute
+ # Policy class that collects all the necessary validators
#
- # @param [#to_sym] key The name of the attribute
- # @param [Proc] block The body of new attribute
- # @return [self]
+ # @return [Class] a subclass of [Tram::Policy] named after the scope
#
- def validate(key, &block)
- validators[key] = Validator.new(@schema, key, &block)
- self
+ def policy
+ @policy ||= superclass.policy.for(self)
end
- # Collection of validators to check initialized settings
+ # Add validation rule to the [#policy]
#
- # @return [Hash<Symbol, Evil::Client::Validator>]
+ # @param [Proc] block The body of new attribute
+ # @return [self]
#
- def validators
- @validators ||= {}
+ def validate(&block)
+ policy.validate(&block)
+ self
end
- # Human-friendly representation of settings class
- #
- # @return [String]
- #
- def name
- super || @schema.to_s
- end
- alias_method :to_s, :name
- alias_method :to_str, :name
- alias_method :inspect, :name
-
# Builds settings with options
#
# @param [Logger, nil] logger
# @param [Hash<#to_sym, Object>, nil] opts
# @return [Evil::Client::Settings]
#
def new(logger, opts = {})
logger&.debug(self) { "initializing with options #{opts}..." }
opts = Hash(opts).each_with_object({}) { |(k, v), o| o[k.to_sym] = v }
- super logger, opts
+ in_english { super logger, opts }
rescue => error
raise ValidationError, error.message
end
+
+ private
+
+ def in_english(&block)
+ available_locales = I18n.available_locales
+ I18n.available_locales = %i[en]
+ I18n.with_locale(:en, &block)
+ ensure
+ I18n.available_locales = available_locales
+ end
end
# The processed hash of options contained by the instance of settings
#
# @return [Hash<Symbol, Object>]
#
def options
- Options.new @__options__
+ @options ||= Options.new self.class.dry_initializer.attributes(self)
end
# @!attribute logger
# @return [Logger, nil] The logger attached to current settings
attr_accessor :logger
@@ -141,28 +162,12 @@
alias_method :to_s, :inspect
private
def initialize(logger, **options)
- @logger = logger
super(options)
-
+ @logger = logger
+ self.class.policy[self].validate!
logger&.debug(self) { "initialized" }
- __validate__!
- end
-
- def __validate__!
- __validators__.reverse.each { |validator| validator.call(self) }
- end
-
- def __validators__
- klass = self.class
- [].tap do |list|
- loop do
- list.concat klass.validators.values
- klass = klass.superclass
- break if klass == Evil::Client::Settings
- end
- end
end
end
end