Class: Dry::Struct
- Inherits:
-
Object
- Object
- Dry::Struct
- 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
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
- #[](name) ⇒ Object
-
#initialize(attributes) ⇒ Struct
constructor
A new instance of Struct.
- #to_hash ⇒ Object (also: #to_h)
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_hash ⇒ Object 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 |