Class: Apiwork::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeElement

Returns a new instance of Element.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/apiwork/element.rb', line 66

def initialize
  @custom_type = nil
  @defined = false
  @discriminator = nil
  @enum = nil
  @format = nil
  @inner = nil
  @max = nil
  @min = nil
  @shape = nil
  @type = nil
  @value = nil
end

Instance Attribute Details

#custom_typeSymbol? (readonly)

The custom type name for this element.

Returns:

  • (Symbol, nil)


55
56
57
# File 'lib/apiwork/element.rb', line 55

def custom_type
  @custom_type
end

#discriminatorSymbol? (readonly)

The discriminator for this element.

Returns:

  • (Symbol, nil)


55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#enumArray, ... (readonly)

The allowed values for this element.

Returns:

  • (Array, Symbol, nil)


55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#formatSymbol? (readonly)

The format hint for this element.

Returns:

  • (Symbol, nil)


55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#innerElement? (readonly)

The inner element for nested arrays.

Returns:



55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#maxNumeric? (readonly)

The maximum constraint for this element.

Returns:

  • (Numeric, nil)


55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#minNumeric? (readonly)

The minimum constraint for this element.

Returns:

  • (Numeric, nil)


55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#shapeObject? (readonly)

The shape for this element.

Returns:



55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#typeSymbol? (readonly)

The type for this element.

Returns:

  • (Symbol, nil)


55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

#valueObject (readonly)

Returns the value of attribute value.



55
56
57
58
59
60
61
62
63
64
# File 'lib/apiwork/element.rb', line 55

attr_reader :custom_type,
:discriminator,
:enum,
:format,
:inner,
:max,
:min,
:shape,
:type,
:value

Instance Method Details

#array {|element| ... } ⇒ void

This method returns an undefined value.

Defines an array.

Examples:

instance_eval style

array :matrix do
  array do
    integer
  end
end

yield style

array :matrix do |element|
  element.array do |inner|
    inner.integer
  end
end

Yields:

  • block defining element type

Yield Parameters:



312
313
314
# File 'lib/apiwork/element.rb', line 312

def array(&block)
  of(:array, &block)
end

#binaryvoid

This method returns an undefined value.

Defines a binary.

Examples:

array :blobs do
  binary
end


249
250
251
# File 'lib/apiwork/element.rb', line 249

def binary
  of(:binary)
end

#booleanvoid

This method returns an undefined value.

Defines a boolean.

Examples:

array :flags do
  boolean
end


184
185
186
# File 'lib/apiwork/element.rb', line 184

def boolean
  of(:boolean)
end

#datevoid

This method returns an undefined value.

Defines a date.

Examples:

array :dates do
  date
end


210
211
212
# File 'lib/apiwork/element.rb', line 210

def date
  of(:date)
end

#datetimevoid

This method returns an undefined value.

Defines a datetime.

Examples:

array :timestamps do
  datetime
end


197
198
199
# File 'lib/apiwork/element.rb', line 197

def datetime
  of(:datetime)
end

#decimal(max: nil, min: nil) ⇒ void

This method returns an undefined value.

Defines a decimal.

Examples:

Basic decimal

array :amounts do
  decimal
end

With range constraints

array :prices do
  decimal min: 0
end

Parameters:

  • max (Numeric, nil) (defaults to: nil)

    (nil) The maximum value.

  • min (Numeric, nil) (defaults to: nil)

    (nil) The minimum value.



149
150
151
# File 'lib/apiwork/element.rb', line 149

def decimal(max: nil, min: nil)
  of(:decimal, max:, min:)
end

#integer(enum: nil, max: nil, min: nil) ⇒ void

This method returns an undefined value.

Defines an integer.

Examples:

Basic integer

array :counts do
  integer
end

With range constraints

array :scores do
  integer min: 0, max: 100
end

Parameters:

  • enum (Array, Symbol, nil) (defaults to: nil)

    (nil) The allowed values.

  • max (Integer, nil) (defaults to: nil)

    (nil) The maximum value.

  • min (Integer, nil) (defaults to: nil)

    (nil) The minimum value.



127
128
129
# File 'lib/apiwork/element.rb', line 127

def integer(enum: nil, max: nil, min: nil)
  of(:integer, enum:, max:, min:)
end

#literal(value:) ⇒ void

This method returns an undefined value.

Defines a literal value.

Examples:

variant tag: 'card' do
  literal value: 'card'
end

Parameters:

  • value (Object)

    The literal value.



381
382
383
# File 'lib/apiwork/element.rb', line 381

def literal(value:)
  of(:literal, value:)
end

#number(max: nil, min: nil) ⇒ void

This method returns an undefined value.

Defines a number.

Examples:

Basic number

array :coordinates do
  number
end

With range constraints

array :latitudes do
  number min: -90, max: 90
end

Parameters:

  • max (Numeric, nil) (defaults to: nil)

    (nil) The maximum value.

  • min (Numeric, nil) (defaults to: nil)

    (nil) The minimum value.



171
172
173
# File 'lib/apiwork/element.rb', line 171

def number(max: nil, min: nil)
  of(:number, max:, min:)
end

#object {|object| ... } ⇒ void

This method returns an undefined value.

Defines an object.

Examples:

instance_eval style

array :items do
  object do
    string :name
    decimal :price
  end
end

yield style

array :items do |element|
  element.object do |object|
    object.string :name
    object.decimal :price
  end
end

Yields:

  • block defining nested structure

Yield Parameters:



288
289
290
# File 'lib/apiwork/element.rb', line 288

def object(&block)
  of(:object, &block)
end

#of(type, **options, &block) ⇒ Object

Raises:

  • (NotImplementedError)


404
405
406
# File 'lib/apiwork/element.rb', line 404

def of(type, **options, &block)
  raise NotImplementedError, "#{self.class} must implement #of"
end

#record {|element| ... } ⇒ void

This method returns an undefined value.

Defines a record.

Examples:

instance_eval style

record :scores do
  integer
end

yield style

record :scores do |element|
  element.integer
end

Yields:

  • block defining value type

Yield Parameters:



332
333
334
# File 'lib/apiwork/element.rb', line 332

def record(&block)
  of(:record, &block)
end

#reference(type_name) ⇒ void

This method returns an undefined value.

Defines a reference to a named type.

Examples:

array :items do
  reference :item
end

Parameters:

  • type_name (Symbol)

    The type to reference.



396
397
398
# File 'lib/apiwork/element.rb', line 396

def reference(type_name)
  of(type_name)
end

#string(enum: nil, format: nil, max: nil, min: nil) ⇒ void

This method returns an undefined value.

Defines a string.

Examples:

Basic string

array :tags do
  string
end

With length constraints

array :tags do
  string min: 1, max: 50
end

Parameters:

  • enum (Array, Symbol, nil) (defaults to: nil)

    (nil) The allowed values.

  • format (Symbol, nil) (defaults to: nil)

    (nil) [:date, :datetime, :email, :hostname, :ipv4, :ipv6, :password, :text, :url, :uuid] Format hint for exports. Does not change the type, but exports may add validation or documentation based on it. Valid formats by type: ‘:string`.

  • max (Integer, nil) (defaults to: nil)

    (nil) The maximum length.

  • min (Integer, nil) (defaults to: nil)

    (nil) The minimum length.



103
104
105
# File 'lib/apiwork/element.rb', line 103

def string(enum: nil, format: nil, max: nil, min: nil)
  of(:string, enum:, format:, max:, min:)
end

#timevoid

This method returns an undefined value.

Defines a time.

Examples:

array :times do
  time
end


236
237
238
# File 'lib/apiwork/element.rb', line 236

def time
  of(:time)
end

#union(discriminator: nil) {|union| ... } ⇒ void

This method returns an undefined value.

Defines a union.

Examples:

instance_eval style

array :payments do
  union discriminator: :type do
    variant tag: 'card' do
      object do
        string :last_four
      end
    end
  end
end

yield style

array :payments do |element|
  element.union discriminator: :type do |union|
    union.variant tag: 'card' do |variant|
      variant.object do |object|
        object.string :last_four
      end
    end
  end
end

Parameters:

  • discriminator (Symbol, nil) (defaults to: nil)

    (nil) The discriminator field name.

Yields:

  • block defining union variants

Yield Parameters:



366
367
368
# File 'lib/apiwork/element.rb', line 366

def union(discriminator: nil, &block)
  of(:union, discriminator:, &block)
end

#unknownvoid

This method returns an undefined value.

Defines an unknown.

Examples:

array :values do
  unknown
end


262
263
264
# File 'lib/apiwork/element.rb', line 262

def unknown
  of(:unknown)
end

#uuidvoid

This method returns an undefined value.

Defines a UUID.

Examples:

array :ids do
  uuid
end


223
224
225
# File 'lib/apiwork/element.rb', line 223

def uuid
  of(:uuid)
end

#validate!Object

Raises:

  • (ArgumentError)


400
401
402
# File 'lib/apiwork/element.rb', line 400

def validate!
  raise ArgumentError, 'must define exactly one type' unless @defined
end