Class: Evil::Client::Model
- Inherits:
-
Object
- Object
- Evil::Client::Model
- Extended by:
- Dry::Initializer
- Defined in:
- lib/evil/client/model.rb
Overview
Data structure with validators and memoizers
Direct Known Subclasses
Class Method Summary collapse
-
.extend(other) ⇒ self
Merges [.option]-s, virtual attributes [.let] and [.validation]-s from another model into the current one.
-
.let(key, &block) ⇒ self
Creates or reloads memoized attribute.
-
.lets ⇒ Hash<Symbol, Proc>
Definitions for virtual attributes.
-
.new(op = {}) ⇒ Evil::Client::Model
(also: call)
Model instance constructor.
-
.option(key, type = nil, as: key.to_sym, **opts) ⇒ self
Creates or updates the settings’ initializer.
-
.options(key, type = nil, opts = {}) ⇒ self
Creates or updates the settings’ initializer.
-
.policy ⇒ Evil::Client::Policy
Policy object for model instances.
-
.validate(&block) ⇒ self
Add validation rule to the [#policy].
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
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..each do |definition| option definition.source, definition. 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
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 |
.lets ⇒ Hash<Symbol, Proc>
Definitions for virtual attributes
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
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. end |
.option(key, type = nil, as: key.to_sym, **opts) ⇒ self
Creates or updates the settings’ initializer
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
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 |
.policy ⇒ Evil::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]
71 72 73 74 |
# File 'lib/evil/client/model.rb', line 71 def validate(&block) policy.validate(&block) self end |