Class: Candid::Internal::Types::Array

Inherits:
Object
  • Object
show all
Includes:
Type
Defined in:
lib/candid/internal/types/array.rb

Overview

An array of a specific type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Type

#strict!, #strict?

Methods included from JSON::Serializable

#dump, #load

Constructor Details

#initialize(type) ⇒ Array

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 Array.



24
25
26
# File 'lib/candid/internal/types/array.rb', line 24

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/candid/internal/types/array.rb', line 10

def type
  @type
end

Class Method Details

.[](type) ⇒ Candid::Internal::Types::Array

Instantiates a new ‘Array` of a given type

Parameters:

  • type (Object)

    The member type of this array

Returns:



18
19
20
# File 'lib/candid/internal/types/array.rb', line 18

def [](type)
  new(type)
end

Instance Method Details

#coerce(value, strict: strict?) ) ⇒ ::Array

Coerces a value into this array

Parameters:

  • value (Object)
  • strict (Hash) (defaults to: strict?) )

    a customizable set of options

Options Hash (strict:):

Returns:

  • (::Array)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/candid/internal/types/array.rb', line 33

def coerce(value, strict: strict?)
  unless value.is_a?(::Array)
    raise Errors::TypeError, "cannot coerce `#{value.class}` to Array<#{type}>" if strict

    return value
  end

  value.map do |element|
    Utils.coerce(type, element, strict: strict)
  end
end