Class: Dry::Struct

Inherits:
Object
  • Object
show all
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.

Examples:

require 'dry-struct'

module Types
  include Dry::Types.module
end

class Book < Dry::Struct
  attribute :title, Types::Strict::String
  attribute :subtitle, Types::Strict::String.optional
end

rom_n_roda = Book.new(
  title: 'Web Development with ROM and Roda',
  subtitle: nil
)
rom_n_roda.title #=> 'Web Development with ROM and Roda'
rom_n_roda.subtitle #=> nil

refactoring = Book.new(
  title: 'Refactoring',
  subtitle: 'Improving the Design of Existing Code'
)
refactoring.title #=> 'Refactoring'
refactoring.subtitle #=> 'Improving the Design of Existing Code'

Direct Known Subclasses

Value

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

Instance Method Summary collapse

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

#attributesObject 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

Examples:

class Book < Dry::Struct
  attribute :title, Types::Strict::String
  attribute :subtitle, Types::Strict::String.optional
end

rom_n_roda = Book.new(
  title: 'Web Development with ROM and Roda',
  subtitle: nil
)
rom_n_roda[:title] #=> 'Web Development with ROM and Roda'
rom_n_roda[:subtitle] #=> nil


122
123
124
125
126
# File 'lib/dry/struct.rb', line 122

def [](name)
  @attributes.fetch(name)
rescue KeyError
  raise MissingAttributeError.new(name)
end

#inputDry::Types::Hash::Schema

Types::Hash::Schema subclass with specific behaviour defined for



91
# File 'lib/dry/struct.rb', line 91

defines :input

#inspectString



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

Examples:

class Book < Dry::Struct
  attribute :title, Types::Strict::String
  attribute :subtitle, Types::Strict::String.optional
end

rom_n_roda = Book.new(
  title: 'Web Development with ROM and Roda',
  subtitle: '2nd edition'
)
  #=> #<Book title="Web Development with ROM and Roda" subtitle="2nd edition">

rom_n_roda.new(subtitle: '3nd edition')
  #=> #<Book title="Web Development with ROM and Roda" subtitle="3nd edition">


172
173
174
# File 'lib/dry/struct.rb', line 172

def new(changeset)
  self.class[__attributes__.merge(changeset)]
end

#to_hashHash{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

Examples:

class Book < Dry::Struct
  attribute :title, Types::Strict::String
  attribute :subtitle, Types::Strict::String.optional
end

rom_n_roda = Book.new(
  title: 'Web Development with ROM and Roda',
  subtitle: nil
)
rom_n_roda.to_hash
  #=> {title: 'Web Development with ROM and Roda', subtitle: nil}


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