Class: Dry::Struct
- Inherits:
-
Object
- Object
- Dry::Struct
- Extended by:
- ClassInterface
- Includes:
- Core::Constants
- Defined in:
- lib/dry/struct.rb,
lib/dry/struct/sum.rb,
lib/dry/struct/value.rb,
lib/dry/struct/errors.rb,
lib/dry/struct/hashify.rb,
lib/dry/struct/version.rb,
lib/dry/struct/constructor.rb,
lib/dry/struct/struct_builder.rb,
lib/dry/struct/class_interface.rb
Overview
Typed Struct with virtus-like DSL for defining schema.
Differences between dry-struct and virtus
Struct look somewhat similar to Virtus but there are few significant differences:
- Structs don't provide attribute writers and are meant to be used as "data objects" exclusively.
- Handling of attribute values is provided by standalone type objects from
dry-types. - Handling of attribute hashes is provided by standalone hash schemas from
dry-types. - Struct classes quack like
dry-types, which means you can use them in hash schemas, as array members or sum them
Struct class can specify a constructor type, which uses hash schemas
to handle attributes in .new method.
Direct Known Subclasses
Defined Under Namespace
Modules: ClassInterface, Hashify Classes: Constructor, MissingAttributeError, RepeatedAttributeError, StructBuilder, Sum, Value
Constant Summary collapse
- Error =
Raised when given input doesn't conform schema and constructor type
Class.new(TypeError)
- VERSION =
'0.5.0'.freeze
Instance Attribute Summary collapse
-
#attributes ⇒ Object
(also: #__attributes__)
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Retrieves value of previously defined attribute by its'
name. -
#initialize(attributes) ⇒ Struct
constructor
A new instance of Struct.
-
#input ⇒ Dry::Types::Hash::Schema
Types::Hash::Schema subclass with specific behaviour defined for.
- #inspect ⇒ String
-
#new(changeset) ⇒ Struct
(also: #__new__)
Create a copy of Struct with overriden attributes.
-
#to_hash ⇒ Hash{Symbol => Object}
(also: #to_h)
Converts the Struct to a hash with keys representing each attribute (as symbols) and their corresponding values.
Methods included from ClassInterface
attribute, attribute?, attribute_names, call, constrained?, constructor, default?, failure, inherited, meta, optional?, primitive, result, schema, success, transform_keys, transform_types, try, try_struct, valid?, |
Constructor Details
#initialize(attributes) ⇒ Struct
101 102 103 |
# File 'lib/dry/struct.rb', line 101 def initialize(attributes) @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object Also known as: __attributes__
Returns the value of attribute attributes.
97 98 99 |
# File 'lib/dry/struct.rb', line 97 def attributes @attributes end |
Instance Method Details
#[](name) ⇒ Object
Retrieves value of previously defined attribute by its' name
122 123 124 125 126 |
# File 'lib/dry/struct.rb', line 122 def [](name) @attributes.fetch(name) rescue KeyError raise MissingAttributeError.new(name) end |
#input ⇒ Dry::Types::Hash::Schema
Types::Hash::Schema subclass with specific behaviour defined for
91 |
# File 'lib/dry/struct.rb', line 91 defines :input |
#inspect ⇒ String
178 179 180 181 182 |
# File 'lib/dry/struct.rb', line 178 def inspect klass = self.class attrs = klass.attribute_names.map { |key| " #{key}=#{@attributes[key].inspect}" }.join "#<#{ klass.name || klass.inspect }#{ attrs }>" end |
#new(changeset) ⇒ Struct Also known as: __new__
Create a copy of Dry::Struct with overriden attributes
172 173 174 |
# File 'lib/dry/struct.rb', line 172 def new(changeset) self.class[__attributes__.merge(changeset)] end |
#to_hash ⇒ Hash{Symbol => Object} Also known as: to_h
Converts the Dry::Struct to a hash with keys representing each attribute (as symbols) and their corresponding values
145 146 147 148 149 |
# File 'lib/dry/struct.rb', line 145 def to_hash self.class.schema.keys.each_with_object({}) do |key, result| result[key] = Hashify[self[key]] if attributes.key?(key) end end |