Class: Dry::Struct
- Inherits:
-
Object
- Object
- Dry::Struct
- Extended by:
- ClassInterface
- Includes:
- Core::Constants
- 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/constructor.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, which means there are different types of constructors in Struct (see ClassInterface#constructor_type) - 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.
See ClassInterface#new for constructor types descriptions and examples.
Direct Known Subclasses
Defined Under Namespace
Modules: ClassInterface, Hashify Classes: Constructor, RepeatedAttributeError, Value
Constant Summary collapse
- CONSTRUCTOR_TYPE =
Dry::Types['symbol'].enum(:permissive, :schema, :strict, :strict_with_defaults)
- Error =
Raised when given input doesn't conform schema and constructor type
Class.new(TypeError)
- VERSION =
'0.4.0'.freeze
Instance Method Summary collapse
-
#[](name) ⇒ Object
Retrieves value of previously defined attribute by its'
name. -
#__attributes__ ⇒ Object
private
@return[Hash=> Object].
-
#constructor_type ⇒ Object
Sets or retrieves ClassInterface#constructor type as a symbol.
- #equalizer ⇒ Dry::Equalizer
-
#initialize(attributes) ⇒ Struct
constructor
A new instance of Struct.
-
#input ⇒ Dry::Types::Hash
Types::Hash subclass with specific behaviour defined for.
-
#new(changeset) ⇒ Struct
(also: #__new__)
Create a copy of Struct with overriden attributes.
- #schema ⇒ Hash{Symbol => Dry::Types::Definition, Dry::Struct}
-
#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
argument_error_msg, attribute, attribute?, attribute_names, attributes, call, check_invalid_schema_keys, constrained?, constructor, default?, default_attributes, failure, inherited, optional?, primitive, result, success, try, valid?
Constructor Details
#initialize(attributes) ⇒ Struct
Returns a new instance of Struct.
182 183 184 |
# File 'lib/dry/struct.rb', line 182 def initialize(attributes) attributes.each { |key, value| instance_variable_set("@#{key}", value) } end |
Instance Method Details
#[](name) ⇒ Object
Retrieves value of previously defined attribute by its' name
203 204 205 |
# File 'lib/dry/struct.rb', line 203 def [](name) public_send(name) end |
#__attributes__ ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
@return[Hash=> Object]
258 259 260 261 262 |
# File 'lib/dry/struct.rb', line 258 def __attributes__ self.class.attribute_names.each_with_object({}) do |key, h| h[key] = instance_variable_get(:"@#{ key }") end end |
#constructor_type(type) ⇒ Symbol #constructor_type ⇒ Symbol
All examples below assume that you have defined Dry::Struct with following attributes and explicitly call only #constructor_type:
class User < Dry::Struct
attribute :name, Types::Strict::String.default('John Doe')
attribute :age, Types::Strict::Int
end
Sets or retrieves Dry::Struct::ClassInterface#constructor type as a symbol
Common constructor types include:
:permissive- the default constructor type, useful for defining Dry::Structs that are instantiated using data from the database (i.e. results of a database query), where you expect all defined attributes to be present and it's OK to ignore other keys (i.e. keys used for joining, that are not relevant from your domain Dry::Structs point of view). Default values are not used otherwise you wouldn't notice missing data.:schema- missing keys will result in setting them using default values, unexpected keys will be ignored.:strict- useful when you do not expect keys other than the ones you specified as attributes in the input hash:strict_with_defaults- same as:strictbut you are OK that some values may be nil and you want defaults to be set
To feel the difference between constructor types, look into examples. Each of them provide the same attributes' definitions, different constructor type, and 4 cases of given input:
- Input omits a key for a value that does not have a default
- Input omits a key for a value that has a default
- Input contains nil for a value that specifies a default
- Input includes a key that was not specified in the schema
175 |
# File 'lib/dry/struct.rb', line 175 defines :constructor_type, type: CONSTRUCTOR_TYPE |
#equalizer ⇒ Dry::Equalizer
179 |
# File 'lib/dry/struct.rb', line 179 defines :equalizer |
#input ⇒ Dry::Types::Hash
Types::Hash subclass with specific behaviour defined for
66 |
# File 'lib/dry/struct.rb', line 66 defines :input |
#new(changeset) ⇒ Struct Also known as: __new__
Create a copy of Dry::Struct with overriden attributes
251 252 253 |
# File 'lib/dry/struct.rb', line 251 def new(changeset) self.class[__attributes__.merge(changeset)] end |
#schema ⇒ Hash{Symbol => Dry::Types::Definition, Dry::Struct}
70 |
# File 'lib/dry/struct.rb', line 70 defines :schema |
#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
224 225 226 227 228 |
# File 'lib/dry/struct.rb', line 224 def to_hash self.class.schema.keys.each_with_object({}) do |key, result| result[key] = Hashify[self[key]] end end |