Class: Evil::Client::Model

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/evil/client/model.rb

Overview

Data structure with validators and memoizers

Direct Known Subclasses

Settings

Class Method Summary collapse

Class Method Details

.extend(other) ⇒ self

Merges [.option]-s, virtual attributes [.let] and [.validation]-s from another model into the current one.

rubocop: disable Metrics/AbcSize

Parameters:

Returns:

  • (self)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/evil/client/model.rb', line 83

def extend(other)
  return super if other.instance_of? Module

  unless other.ancestors.include? Evil::Client::Model
    raise TypeError, "#{other} is not a subclass of Evil::Client::Model"
  end

  other.dry_initializer.options.each do |definition|
    option definition.source, definition.options
  end

  other.lets.each { |key, block| let(key, &block) }
  other.policy.all.each { |validator| policy.local << validator }
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)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/evil/client/model.rb', line 38

def let(key, &block)
  NameError.check!(key)
  lets[key.to_sym] = block

  define_method(key) do
    instance_variable_get(:"@#{key}") ||
      instance_variable_set(:"@#{key}", instance_exec(&block))
  end

  self
end

.letsHash<Symbol, Proc>

Definitions for virtual attributes

Returns:

  • (Hash<Symbol, Proc>)


54
55
56
# File 'lib/evil/client/model.rb', line 54

def lets
  @lets ||= {}
end

.new(op = {}) ⇒ Evil::Client::Model Also known as: call

Model instance constructor

Parameters:

  • op (Hash) (defaults to: {})

    Model options

Returns:



104
105
106
107
108
109
# File 'lib/evil/client/model.rb', line 104

def new(op = {})
  op = Hash(op).each_with_object({}) { |(k, v), obj| obj[k.to_sym] = v }
  super(op).tap { |item| in_english { policy[item].validate! } }
rescue StandardError => 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)

    (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:



25
26
27
28
29
# File 'lib/evil/client/model.rb', line 25

def option(key, type = nil, as: key.to_sym, **opts)
  NameError.check!(as)
  super
  self
end

.options(key, type = nil, opts = {}) ⇒ self

Creates or updates the settings’ initializer

Parameters:

  • key (#to_sym)

    Symbolic name of the option

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

    (nil) Type coercer for the option

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:



25
26
27
28
29
# File 'lib/evil/client/model.rb', line 25

def option(key, type = nil, as: key.to_sym, **opts)
  NameError.check!(as)
  super
  self
end

.policyEvil::Client::Policy

Policy object for model instances



62
63
64
# File 'lib/evil/client/model.rb', line 62

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)


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

def validate(&block)
  policy.validate(&block)
  self
end