Class: Apiwork::API::Element
- Defined in:
- lib/apiwork/api/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
-
#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, #initialize, #integer, #literal, #number, #object, #record, #string, #time, #union, #unknown, #uuid, #validate!
Constructor Details
This class inherits a constructor from Apiwork::Element
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.
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 126 127 128 129 130 131 132 |
# File 'lib/apiwork/api/element.rb', line 83 def of(type, discriminator: nil, enum: nil, format: nil, max: nil, min: nil, value: nil, &block) case type when :string, :integer, :decimal, :boolean, :number, :datetime, :date, :uuid, :time, :binary, :unknown set_type(type, enum:, format:, max:, min:) when :literal @type = :literal @value = value @defined = true when :object @type = :object if block shape = Object.new 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 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 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(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
134 135 136 137 138 |
# File 'lib/apiwork/api/element.rb', line 134 def reference(type_name) @type = type_name @custom_type = type_name @defined = true end |