Class: Evil::Client::Settings

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.localeClass (readonly)

Reference to the schema klass the settings belongs to

Returns:

  • (Class)


27
28
29
# File 'lib/evil/client/settings.rb', line 27

def locale
  @locale
end

.schemaClass (readonly)

Reference to the schema klass the settings belongs to

Returns:

  • (Class)


27
28
29
# File 'lib/evil/client/settings.rb', line 27

def schema
  @schema
end

Instance Attribute Details

#loggerLogger?

Returns The logger attached to current settings.

Returns:

  • (Logger, nil)

    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

Parameters:

  • schema (Class)

Returns:

  • (Class)

    a subclass for the 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

Parameters:

  • key (#to_sym)

    The name of the attribute

  • block (Proc)

    The body of new attribute

Returns:

  • (self)


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

.nameString Also known as: to_s, to_str, inspect

Human-friendly representation of settings class

Returns:

  • (String)


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

Parameters:

  • logger (Logger, nil)
  • opts (Hash<#to_sym, Object>, nil) (defaults to: {})

Returns:



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.message
end

.option(key, type = nil, as: key.to_sym, **opts) ⇒ self

Creates or updates the settings’ initializer

Parameters:

  • key (#to_sym)

    Symbolic name of the option

  • type (#call) (defaults to: nil)

    Type coercer for the option

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :type (#call)

    Another way to assign type coercer

  • :default (#call)

    Proc containing default value

  • :optional (Boolean)

    Whether it can be missed

  • :as (#to_sym)

    The name of settings variable

  • :reader (false, :private, :protected)

    Reader method type

Returns:

  • (self)

See Also:



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

.policyClass

Policy class that collects all the necessary validators

Returns:

  • (Class)

    a subclass of [Tram::Policy] named after the scope



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]

Parameters:

  • block (Proc)

    The body of new attribute

Returns:

  • (self)


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

Parameters:

  • value (Date, String, nil)

    Value to be formatted

Returns:

  • (String, nil)

See Also:



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

#inspectString Also known as: to_str, to_s

Human-readable representation of settings instance

Returns:

  • (String)


156
157
158
159
160
# File 'lib/evil/client/settings.rb', line 156

def inspect
  number = super.match(/\>\:([^ ]+) /)[1]
  params = options.map { |k, v| "@#{k}=#{v}" }.join(", ")
  number ? "#<#{self.class}:#{number} #{params}>" : super
end

#optionsHash<Symbol, Object>

The processed hash of options contained by the instance of settings

Returns:

  • (Hash<Symbol, Object>)


127
128
129
# File 'lib/evil/client/settings.rb', line 127

def options
  @options ||= Options.new self.class.dry_initializer.attributes(self)
end