Class: Structure::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/structure/builder.rb

Overview

Builder class for accumulating attribute definitions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

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.

Returns a new instance of Builder.



12
13
14
15
16
17
# File 'lib/structure/builder.rb', line 12

def initialize
  @mappings = {}
  @defaults = {}
  @types = {}
  @optional = Set.new
end

Instance Attribute Details

#after_parse_callbackObject (readonly)

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.



9
10
11
# File 'lib/structure/builder.rb', line 9

def after_parse_callback
  @after_parse_callback
end

#defaultsObject (readonly)

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.



9
10
11
# File 'lib/structure/builder.rb', line 9

def defaults
  @defaults
end

#mappingsObject (readonly)

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.



9
10
11
# File 'lib/structure/builder.rb', line 9

def mappings
  @mappings
end

#typesObject (readonly)

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.



9
10
11
# File 'lib/structure/builder.rb', line 9

def types
  @types
end

Instance Method Details

#after_parse {|instance| ... } ⇒ void

This method returns an undefined value.

Defines a callback to run after parsing

Examples:

Validation

after_parse do |order|
  raise "Invalid order" if order.total < 0
end

Yields:

  • (instance)

    Block that receives the parsed instance



77
78
79
# File 'lib/structure/builder.rb', line 77

def after_parse(&block)
  @after_parse_callback = block
end

#attribute(name, type = nil, from: nil, default: nil) {|value| ... } ⇒ Object

DSL method for defining attributes with optional type coercion

Examples:

With type coercion

attribute :age, Integer

With custom source key

attribute :created_at, Time, from: "CreatedAt"

With transformation block

attribute :price do |value|
  Money.new(value["amount"], value["currency"])
end

Parameters:

  • name (Symbol)

    The attribute name

  • type (Class, Symbol, Array, nil) (defaults to: nil)

    Type for coercion (e.g., String, :boolean, [String])

  • from (String, nil) (defaults to: nil)

    Source key in the data hash (defaults to name.to_s)

  • default (Object, nil) (defaults to: nil)

    Default value if attribute is missing

Yields:

  • (value)

    Block for custom transformation

Raises:

  • (ArgumentError)

    If both type and block are provided



38
39
40
41
42
43
44
45
46
47
# File 'lib/structure/builder.rb', line 38

def attribute(name, type = nil, from: nil, default: nil, &block)
  mappings[name] = from || name.to_s
  defaults[name] = default unless default.nil?

  if type && block
    raise ArgumentError, "Cannot specify both type and block for :#{name}"
  else
    types[name] = type || block
  end
end

#attribute?(name, type = nil, from: nil, default: nil) {|value| ... } ⇒ Boolean

DSL method for defining optional attributes (key can be missing from input hash)

Examples:

Optional attribute

attribute? :age, Integer

Optional with default

attribute? :status, String, default: "pending"

Parameters:

  • name (Symbol)

    The attribute name

  • type (Class, Symbol, Array, nil) (defaults to: nil)

    Type for coercion (e.g., String, :boolean, [String])

  • from (String, nil) (defaults to: nil)

    Source key in the data hash (defaults to name.to_s)

  • default (Object, nil) (defaults to: nil)

    Default value if attribute is missing

Yields:

  • (value)

    Block for custom transformation

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

    If both type and block are provided



63
64
65
66
# File 'lib/structure/builder.rb', line 63

def attribute?(name, type = nil, from: nil, default: nil, &block)
  attribute(name, type, from: from, default: default, &block)
  @optional.add(name)
end

#attributesObject

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.



82
# File 'lib/structure/builder.rb', line 82

def attributes = @mappings.keys

#coercions(context = nil) ⇒ 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.



91
92
93
# File 'lib/structure/builder.rb', line 91

def coercions(context = nil)
  @types.transform_values { |type| Types.coerce(type, context) }
end

#optionalObject

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.



85
# File 'lib/structure/builder.rb', line 85

def optional = @optional.to_a

#predicate_methodsObject

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.



96
97
98
99
100
101
102
# File 'lib/structure/builder.rb', line 96

def predicate_methods
  @types.filter_map do |name, type|
    if type == :boolean
      ["#{name}?".to_sym, name] unless name.to_s.end_with?("?")
    end
  end.to_h
end

#requiredObject

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.



88
# File 'lib/structure/builder.rb', line 88

def required = attributes - optional