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.1.1'.freeze

Instance Attribute Summary

Attributes included from ClassInterface

#constructor, #constructor_type, #equalizer

Instance Method Summary collapse

Methods included from ClassInterface

attribute, attributes, constrained?, default?, default_attributes, extended, failure, inherited, new, primitive, result, schema, success, try, valid?

Constructor Details

#initialize(attributes) ⇒ Struct

Returns a new instance of Struct.



15
16
17
# File 'lib/dry/struct.rb', line 15

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

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  public_send(name)
end

#to_hashObject Also known as: to_h



23
24
25
26
27
# File 'lib/dry/struct.rb', line 23

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