Class: FinchAPI::Type::ArrayOf Abstract Private

Inherits:
Object
  • Object
show all
Includes:
Converter
Defined in:
lib/finch-api/type/array_of.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is abstract.

Array of items of a given type.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Converter

coerce, dump, type_info

Constructor Details

#initialize(type_info, spec = {}) ⇒ ArrayOf

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

Parameters:

  • type_info (Hash{Symbol=>Object}, Proc, FinchAPI::Type::Converter, Class)
  • spec (Hash{Symbol=>Object}) (defaults to: {})

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :“nil?”



104
105
106
107
# File 'lib/finch-api/type/array_of.rb', line 104

def initialize(type_info, spec = {})
  @item_type_fn = FinchAPI::Type::Converter.type_info(type_info || spec)
  @nilable = spec[:nil?]
end

Class Method Details

.[](type_info, spec = {}) ⇒ 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.

Parameters:

  • type_info (Hash{Symbol=>Object}, Proc, FinchAPI::Type::Converter, Class)
  • spec (Hash{Symbol=>Object}) (defaults to: {})

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :“nil?”



24
# File 'lib/finch-api/type/array_of.rb', line 24

def self.[](type_info, spec = {}) = new(type_info, spec)

Instance Method Details

#==(other) ⇒ Boolean

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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


34
# File 'lib/finch-api/type/array_of.rb', line 34

def ==(other) = other.is_a?(FinchAPI::ArrayOf) && other.nilable? == nilable? && other.item_type == item_type

#===(other) ⇒ Boolean

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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


29
# File 'lib/finch-api/type/array_of.rb', line 29

def ===(other) = other.is_a?(Array) && other.all?(item_type)

#coerce(value, state:) ⇒ Array<Object>, 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.

Parameters:

  • value (Enumerable, Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean, :strong] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Integer] :branched

Returns:

  • (Array<Object>, Object)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/finch-api/type/array_of.rb', line 49

def coerce(value, state:)
  exactness = state.fetch(:exactness)

  unless value.is_a?(Array)
    exactness[:no] += 1
    return value
  end

  target = item_type
  exactness[:yes] += 1
  value
    .map do |item|
      case [nilable?, item]
      in [true, nil]
        exactness[:yes] += 1
        nil
      else
        FinchAPI::Type::Converter.coerce(target, item, state: state)
      end
    end
end

#dump(value) ⇒ Array<Object>, 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.

Parameters:

  • value (Enumerable, Object)

Returns:

  • (Array<Object>, Object)


76
77
78
79
# File 'lib/finch-api/type/array_of.rb', line 76

def dump(value)
  target = item_type
  value.is_a?(Array) ? value.map { FinchAPI::Type::Converter.dump(target, _1) } : super
end