Module: Dry

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/printer.rb,
lib/dry/struct/version.rb,
lib/dry/struct/compiler.rb,
lib/dry/struct/constructor.rb,
lib/dry/struct/struct_builder.rb,
lib/dry/struct/class_interface.rb,
lib/dry/struct/extensions/pretty_print.rb

Defined Under Namespace

Modules: Types Classes: Struct

Class Method Summary collapse

Class Method Details

.Struct(attributes = Dry::Core::Constants::EMPTY_HASH, &block) ⇒ Dry::Struct

Constructor method for easily creating a Struct.

Examples:

require 'dry-struct'

module Types
  include Dry.Types()
end

Person = Dry.Struct(name: Types::String, age: Types::Integer)
matz = Person.new(name: "Matz", age: 52)
matz.name #=> "Matz"
matz.age #=> 52

Test = Dry.Struct(expected: Types::String) { schema(schema.strict) }
Test[expected: "foo", unexpected: "bar"]
#=> Dry::Struct::Error: [Test.new] unexpected keys [:unexpected] in Hash input

Returns:



30
31
32
33
34
35
# File 'lib/dry/struct.rb', line 30

def self.Struct(attributes = Dry::Core::Constants::EMPTY_HASH, &block)
  Class.new(Dry::Struct) do
    attributes.each { |a, type| attribute a, type }
    module_eval(&block) if block
  end
end