Class: FinchAPI::ArrayOf Abstract Private

Inherits:
Object
  • Object
show all
Includes:
Converter
Defined in:
lib/finch-api/base_model.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, try_strict_coerce, 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::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?”



719
720
721
# File 'lib/finch-api/base_model.rb', line 719

def initialize(type_info, spec = {})
  @item_type_fn = FinchAPI::Converter.type_info(type_info || spec)
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::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?”



611
# File 'lib/finch-api/base_model.rb', line 611

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)


631
# File 'lib/finch-api/base_model.rb', line 631

def ==(other) = other.is_a?(FinchAPI::ArrayOf) && 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)


616
617
618
619
620
621
622
623
624
625
626
# File 'lib/finch-api/base_model.rb', line 616

def ===(other)
  type = item_type
  case other
  in Array
    # rubocop:disable Style/CaseEquality
    other.all? { type === _1 }
    # rubocop:enable Style/CaseEquality
  else
    false
  end
end

#coerce(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)


638
639
640
641
642
643
644
645
646
# File 'lib/finch-api/base_model.rb', line 638

def coerce(value)
  type = item_type
  case value
  in Enumerable unless value.is_a?(Hash)
    value.map { FinchAPI::Converter.coerce(type, _1) }
  else
    value
  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)


653
654
655
656
657
658
659
660
661
# File 'lib/finch-api/base_model.rb', line 653

def dump(value)
  type = item_type
  case value
  in Enumerable unless value.is_a?(Hash)
    value.map { FinchAPI::Converter.dump(type, _1) }.to_a
  else
    value
  end
end

#try_strict_coerce(value) ⇒ Array(true, Object, nil), Array(false, Boolean, Integer)

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 (Object)

Returns:

  • (Array(true, Object, nil), Array(false, Boolean, Integer))


668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/finch-api/base_model.rb', line 668

def try_strict_coerce(value)
  case value
  in Array
    type = item_type
    great_success = true
    tally = 0

    mapped =
      value.map do |item|
        case FinchAPI::Converter.try_strict_coerce(type, item)
        in [true, coerced, score]
          tally += score
          coerced
        in [false, true, score]
          great_success = false
          tally += score
          item
        in [false, false, _]
          great_success &&= item.nil?
          item
        end
      end

    if great_success
      [true, mapped, tally]
    else
      [false, true, tally]
    end
  else
    [false, false, 0]
  end
end