Class: Evil::Client::Settings
- Inherits:
-
Object
- Object
- Evil::Client::Settings
- Extended by:
- Dry::Initializer
- Defined in:
- lib/evil/client/settings.rb
Overview
Container for settings assigned to some operation or scope.
Class Attribute Summary collapse
-
.locale ⇒ Class
readonly
Reference to the schema klass the settings belongs to.
-
.schema ⇒ Class
readonly
Reference to the schema klass the settings belongs to.
Instance Attribute Summary collapse
-
#logger ⇒ Logger?
The logger attached to current settings.
Class Method Summary collapse
-
.for(schema) ⇒ Class
Subclasses itself for a given schema.
-
.let(key, &block) ⇒ self
Creates or reloads memoized attribute.
-
.name ⇒ String
(also: to_s, to_str, inspect)
Human-friendly representation of settings class.
-
.new(logger, opts = {}) ⇒ Evil::Client::Settings
Builds settings with options.
-
.option(key, type = nil, as: key.to_sym, **opts) ⇒ self
Creates or updates the settings’ initializer.
-
.param(*args) ⇒ Object
Only options can be defined for the settings container.
-
.policy ⇒ Class
Policy class that collects all the necessary validators.
-
.validate(&block) ⇒ self
Add validation rule to the [#policy].
Instance Method Summary collapse
-
#datetime(value) ⇒ String?
DSL helper to format datetimes following RFC7231/RFC2822.
-
#inspect ⇒ String
(also: #to_str, #to_s)
Human-readable representation of settings instance.
-
#options ⇒ Hash<Symbol, Object>
The processed hash of options contained by the instance of settings.
Class Attribute Details
.locale ⇒ Class (readonly)
Reference to the schema klass the settings belongs to
27 28 29 |
# File 'lib/evil/client/settings.rb', line 27 def locale @locale end |
.schema ⇒ Class (readonly)
Reference to the schema klass the settings belongs to
27 28 29 |
# File 'lib/evil/client/settings.rb', line 27 def schema @schema end |
Instance Attribute Details
#logger ⇒ Logger?
Returns The logger attached to current settings.
133 134 135 |
# File 'lib/evil/client/settings.rb', line 133 def logger @logger end |
Class Method Details
.for(schema) ⇒ Class
Subclasses itself for a given schema
17 18 19 20 21 |
# File 'lib/evil/client/settings.rb', line 17 def for(schema) Class.new(self).tap do |klass| klass.send(:instance_variable_set, :@schema, schema) end end |
.let(key, &block) ⇒ self
Creates or reloads memoized attribute
71 72 73 74 75 76 77 78 |
# File 'lib/evil/client/settings.rb', line 71 def let(key, &block) NameError.check!(key) define_method(key) do instance_variable_get(:"@#{key}") || instance_variable_set(:"@#{key}", instance_exec(&block)) end self end |
.name ⇒ String Also known as: to_s, to_str, inspect
Human-friendly representation of settings class
33 34 35 |
# File 'lib/evil/client/settings.rb', line 33 def name super || @schema.to_s end |
.new(logger, opts = {}) ⇒ Evil::Client::Settings
Builds settings with options
104 105 106 107 108 109 110 |
# File 'lib/evil/client/settings.rb', line 104 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 } in_english { super logger, opts } rescue => error raise ValidationError, error. end |
.option(key, type = nil, as: key.to_sym, **opts) ⇒ self
Creates or updates the settings’ initializer
59 60 61 62 63 |
# File 'lib/evil/client/settings.rb', line 59 def option(key, type = nil, as: key.to_sym, **opts) NameError.check!(as) super self end |
.param(*args) ⇒ Object
Only options can be defined for the settings container
42 43 44 |
# File 'lib/evil/client/settings.rb', line 42 def param(*args) option(*args) end |
.policy ⇒ Class
Policy class that collects all the necessary validators
84 85 86 |
# File 'lib/evil/client/settings.rb', line 84 def policy @policy ||= superclass.policy.for(self) end |
.validate(&block) ⇒ self
Add validation rule to the [#policy]
93 94 95 96 |
# File 'lib/evil/client/settings.rb', line 93 def validate(&block) policy.validate(&block) self end |
Instance Method Details
#datetime(value) ⇒ String?
DSL helper to format datetimes following RFC7231/RFC2822
142 143 144 145 146 147 148 149 150 |
# File 'lib/evil/client/settings.rb', line 142 def datetime(value) return unless value value = DateTime.parse(value) if value.is_a? String value = value.to_datetime if value.respond_to? :to_datetime raise "Cannot convert #{value} to DateTime" unless value.is_a?(DateTime) value.rfc2822 end |
#inspect ⇒ String Also known as: to_str, to_s
Human-readable representation of settings instance
156 157 158 159 160 |
# File 'lib/evil/client/settings.rb', line 156 def inspect number = super.match(/\>\:([^ ]+) /)[1] params = .map { |k, v| "@#{k}=#{v}" }.join(", ") number ? "#<#{self.class}:#{number} #{params}>" : super end |
#options ⇒ Hash<Symbol, Object>
The processed hash of options contained by the instance of settings
127 128 129 |
# File 'lib/evil/client/settings.rb', line 127 def @options ||= Options.new self.class.dry_initializer.attributes(self) end |