Class: Dry::Struct::StructBuilder

Inherits:
Types::Compiler
  • Object
show all
Defined in:
lib/dry/struct/struct_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(struct) ⇒ StructBuilder

Returns a new instance of StructBuilder.



9
10
11
12
# File 'lib/dry/struct/struct_builder.rb', line 9

def initialize(struct)
  super(Dry::Types)
  @struct = struct
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



7
8
9
# File 'lib/dry/struct/struct_builder.rb', line 7

def struct
  @struct
end

Instance Method Details

#call(attr_name, type) { ... } ⇒ Object

Parameters:

  • attr_name (Symbol|String)

    the name of the nested type

  • type (Dry::Struct, Dry::Types::Type::Array)

    the superclass of the nested struct

Yields:

  • the body of the nested struct



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dry/struct/struct_builder.rb', line 17

def call(attr_name, type, &block)
  const_name = const_name(type, attr_name)
  check_name(const_name)

  new_type = Class.new(parent(type), &block)
  struct.const_set(const_name, new_type)

  if array?(type)
    type.of(new_type)
  else
    new_type
  end
end