Class: Evil::Client::Settings

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

Methods inherited from Model

extend, let, lets, option, options, policy

Class Attribute Details

.localeClass (readonly)

Reference to the schema klass the settings belongs to

Returns:

  • (Class)


24
25
26
# File 'lib/evil/client/settings.rb', line 24

def locale
  @locale
end

.schemaClass (readonly)

Reference to the schema klass the settings belongs to

Returns:

  • (Class)


24
25
26
# File 'lib/evil/client/settings.rb', line 24

def schema
  @schema
end

Instance Attribute Details

#loggerLogger?

Returns The logger attached to current settings.

Returns:

  • (Logger, nil)

    The logger attached to current settings



72
73
74
# File 'lib/evil/client/settings.rb', line 72

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



14
15
16
17
18
# File 'lib/evil/client/settings.rb', line 14

def for(schema)
  Class.new(self).tap do |klass|
    klass.send(:instance_variable_set, :@schema, schema)
  end
end

.nameString Also known as: to_s, to_str, inspect

Human-friendly representation of settings class

Returns:

  • (String)


30
31
32
# File 'lib/evil/client/settings.rb', line 30

def name
  super || @schema.to_s
end

.new(logger, op = {}) ⇒ Evil::Client::Settings

Builds settings with options

Parameters:

  • logger (Logger, nil)
  • opts (Hash<#to_sym, Object>, nil)

Returns:



53
54
55
56
57
58
59
# File 'lib/evil/client/settings.rb', line 53

def new(logger, op = {})
  logger&.debug(self) { "initializing with options #{op}..." }
  super(op).tap do |item|
    item.logger = logger
    logger&.debug(item) { "initialized" }
  end
end

.validate(&block) ⇒ self

Add validation rule to the [#policy]

Parameters:

  • block (Proc)

    The body of new attribute

Returns:

  • (self)


42
43
44
45
# File 'lib/evil/client/settings.rb', line 42

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:



81
82
83
84
85
86
87
88
89
# File 'lib/evil/client/settings.rb', line 81

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)


95
96
97
98
99
# File 'lib/evil/client/settings.rb', line 95

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>)


66
67
68
# File 'lib/evil/client/settings.rb', line 66

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