Class: Dry::Types::Struct

Inherits:
Object
  • Object
show all
Extended by:
ClassInterface
Defined in:
lib/dry/types/struct.rb,
lib/dry/types/struct/class_interface.rb

Direct Known Subclasses

Result::Failure, Result::Success, Value

Defined Under Namespace

Modules: ClassInterface

Instance Attribute Summary

Attributes included from ClassInterface

#constructor, #constructor_type, #equalizer

Instance Method Summary collapse

Methods included from ClassInterface

attribute, attributes, default?, default_attributes, inherited, maybe?, new, schema, try, valid?

Methods included from Builder

#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|

Constructor Details

#initialize(attributes) ⇒ Struct

Returns a new instance of Struct.



9
10
11
# File 'lib/dry/types/struct.rb', line 9

def initialize(attributes)
  attributes.each { |key, value| instance_variable_set("@#{key}", value) }
end

Instance Method Details

#[](name) ⇒ Object



13
14
15
# File 'lib/dry/types/struct.rb', line 13

def [](name)
  public_send(name)
end

#to_hashObject Also known as: to_h



17
18
19
20
21
# File 'lib/dry/types/struct.rb', line 17

def to_hash
  self.class.schema.keys.each_with_object({}) do |key, result|
    result[key] = Hashify[self[key]]
  end
end