Class: Evil::Struct
- Inherits:
-
Object
- Object
- Evil::Struct
- Extended by:
- Dry::Initializer::Mixin
- Defined in:
- lib/evil/struct.rb
Overview
Nested structure with type constraints, based on the ‘dry-initializer` DSL
Defined Under Namespace
Modules: Utils Classes: Attributes
Class Method Summary collapse
-
.attributes(options) ⇒ self
Shares options between definitions made inside the block.
-
.list_of_attributes ⇒ Array<Symbol>
Returns the list of defined attributes.
-
.new(value = {}) ⇒ Evil::Struct
(also: call, [], load)
Builds a struct from value that respond to ‘to_h` or `to_hash`.
-
.option(name, type = nil, as: nil, **opts) ⇒ self
(also: attribute, param)
Declares the attribute.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks an equality to other object that respond to ‘to_h` or `to_hash`.
-
#merge(other) ⇒ self.class
Shallowly merges other object to the current struct.
-
#merge_deeply(other) ⇒ self.class
(also: #deep_merge)
Deeply merges other object to the current struct.
-
#to_h ⇒ Hash
(also: #to_hash, #dump)
Converts nested structure to hash.
Class Method Details
.attributes(options) ⇒ self
Shares options between definitions made inside the block
48 49 50 51 |
# File 'lib/evil/struct.rb', line 48 def attributes(**, &block) Attributes.call(self, , &block) self end |
.list_of_attributes ⇒ Array<Symbol>
Returns the list of defined attributes
57 58 59 |
# File 'lib/evil/struct.rb', line 57 def list_of_attributes @list_of_attributes ||= [] end |
.new(value = {}) ⇒ Evil::Struct Also known as: call, [], load
Builds a struct from value that respond to ‘to_h` or `to_hash`
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/evil/struct.rb', line 20 def new(value = {}) value if value.instance_of? self.class hash = value if value.is_a? Hash hash ||= value.to_h if value.respond_to? :to_h hash ||= value.to_hash if value.respond_to? :to_hash hash_with_symbolic_keys = hash.each_with_object({}) do |(key, val), obj| obj[key.to_sym] = val end super hash_with_symbolic_keys end |
.option(name, type = nil, as: nil, **opts) ⇒ self Also known as: attribute, param
Declares the attribute
75 76 77 78 |
# File 'lib/evil/struct.rb', line 75 def option(name, type = nil, as: nil, **opts) super.tap { list_of_attributes << (as || name).to_sym } self end |
Instance Method Details
#==(other) ⇒ Boolean
Checks an equality to other object that respond to ‘to_h` or `to_hash`
95 96 97 98 99 100 101 102 103 |
# File 'lib/evil/struct.rb', line 95 def ==(other) if other&.respond_to?(:to_h) to_h == other.to_h elsif other.respond_to?(:to_hash) to_h == other.to_hash else false end end |
#merge(other) ⇒ self.class
Shallowly merges other object to the current struct
159 160 161 |
# File 'lib/evil/struct.rb', line 159 def merge(other) self.class[Utils.merge(to_h, other)] end |
#merge_deeply(other) ⇒ self.class Also known as: deep_merge
Deeply merges other object to the current struct
It iterates through hashes and objects responding to ‘to_h` and `to_hash`. The iteration stops when any non-hash value reached.
187 188 189 |
# File 'lib/evil/struct.rb', line 187 def merge_deeply(other) self.class[Utils.merge_deeply(self, other)] end |
#to_h ⇒ Hash Also known as: to_hash, dump
Converts nested structure to hash
Makes conversion through nested hashes, arrays, enumerables, as well as trhough values that respond to ‘to_a`, `to_h`, and `to_hash`. Doesn’t convert ‘nil`.
116 117 118 119 120 121 |
# File 'lib/evil/struct.rb', line 116 def to_h self.class.list_of_attributes.each_with_object({}) do |key, hash| val = instance_variable_get :"@#{key}" hash[key] = Utils.hashify(val) unless val == Dry::Initializer::UNDEFINED end end |