Class: Apiwork::Contract::Element
- Defined in:
- lib/apiwork/contract/element.rb
Overview
Block context for defining a single type expression.
Used inside ‘array do` and `variant do` blocks where exactly one element type must be defined.
Instance Attribute Summary
Attributes inherited from Element
#custom_type, #discriminator, #enum, #format, #inner, #max, #min, #shape, #type, #value
Instance Method Summary collapse
-
#initialize(contract_class) ⇒ Element
constructor
A new instance of Element.
-
#of(type, discriminator: nil, enum: nil, format: nil, max: nil, min: nil, value: nil) {|shape| ... } ⇒ void
Defines the element type.
- #reference(type_name) ⇒ Object
Methods inherited from Element
#array, #binary, #boolean, #date, #datetime, #decimal, #integer, #literal, #number, #object, #record, #string, #time, #union, #unknown, #uuid, #validate!
Constructor Details
#initialize(contract_class) ⇒ Element
Returns a new instance of Element.
29 30 31 32 |
# File 'lib/apiwork/contract/element.rb', line 29 def initialize(contract_class) super() @contract_class = contract_class end |
Instance Method Details
#of(type, discriminator: nil, enum: nil, format: nil, max: nil, min: nil, value: nil) {|shape| ... } ⇒ void
This method returns an undefined value.
Defines the element type.
This is the verbose form. Prefer sugar methods (string, integer, etc.) for static definitions. Use ‘of` for dynamic element generation.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/apiwork/contract/element.rb', line 74 def of(type, discriminator: nil, enum: nil, format: nil, max: nil, min: nil, value: nil, &block) resolved_enum = enum.is_a?(Symbol) ? resolve_enum(enum) : enum case type when :string, :integer, :decimal, :boolean, :number, :datetime, :date, :uuid, :time, :binary, :unknown set_type(type, format:, max:, min:, enum: resolved_enum) when :literal @type = :literal @value = value @defined = true when :object @type = :object if block shape = Object.new(@contract_class) block.arity.positive? ? yield(shape) : shape.instance_eval(&block) @shape = shape end @defined = true when :array raise ConfigurationError, 'array requires a block' unless block inner = Element.new(@contract_class) block.arity.positive? ? yield(inner) : inner.instance_eval(&block) inner.validate! @type = :array @inner = inner @shape = inner.shape @defined = true when :record raise ConfigurationError, 'record requires a block' unless block inner = Element.new(@contract_class) block.arity.positive? ? yield(inner) : inner.instance_eval(&block) inner.validate! @type = :record @inner = inner @defined = true when :union raise ConfigurationError, 'union requires a block' unless block shape = Union.new(@contract_class, discriminator:) block.arity.positive? ? yield(shape) : shape.instance_eval(&block) @type = :union @shape = shape @discriminator = discriminator @defined = true else @type = type @custom_type = type @defined = true end end |
#reference(type_name) ⇒ Object
127 128 129 130 131 |
# File 'lib/apiwork/contract/element.rb', line 127 def reference(type_name) @type = type_name @custom_type = type_name @defined = true end |