module Dry

Public Class Methods

Struct(attributes = Dry::Core::Constants::EMPTY_HASH, &block) click to toggle source

Constructor method for easily creating a {Dry::Struct}. @return [Dry::Struct] @example

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
# File lib/dry/struct.rb, line 33
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