Class: Dry::Struct

Inherits:
Object
  • Object
show all
Extended by:
Configurable, ClassInterface
Defined in:
lib/dry/struct.rb,
lib/dry/struct/value.rb,
lib/dry/struct/errors.rb,
lib/dry/struct/hashify.rb,
lib/dry/struct/version.rb,
lib/dry/struct/class_interface.rb

Direct Known Subclasses

Value

Defined Under Namespace

Modules: ClassInterface, Hashify Classes: RepeatedAttributeError, Value

Constant Summary collapse

Error =
Class.new(TypeError)
VERSION =
'0.0.1'.freeze

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?

Constructor Details

#initialize(attributes) ⇒ Struct

Returns a new instance of Struct.



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

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

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  public_send(name)
end

#to_hashObject Also known as: to_h



21
22
23
24
25
# File 'lib/dry/struct.rb', line 21

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