Class: FinchAPI::HashOf 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.

Hash 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 = {}) ⇒ HashOf

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

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?”



861
862
863
# File 'lib/finch-api/base_model.rb', line 861

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?”



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

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)


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

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


748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/finch-api/base_model.rb', line 748

def ===(other)
  type = item_type
  case other
  in Hash
    other.all? do |key, val|
      case [key, val]
      in [Symbol | String, ^type]
        true
      else
        false
      end
    end
  else
    false
  end
end

#coerce(value) ⇒ Hash{Symbol=>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 (Hash{Object=>Object}, Object)

Returns:

  • (Hash{Symbol=>Object}, Object)


775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/finch-api/base_model.rb', line 775

def coerce(value)
  type = item_type
  case value
  in Hash
    value.to_h do |key, val|
      coerced = FinchAPI::Converter.coerce(type, val)
      [key.is_a?(String) ? key.to_sym : key, coerced]
    end
  else
    value
  end
end

#dump(value) ⇒ Hash{Symbol=>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 (Hash{Object=>Object}, Object)

Returns:

  • (Hash{Symbol=>Object}, Object)


793
794
795
796
797
798
799
800
801
802
803
# File 'lib/finch-api/base_model.rb', line 793

def dump(value)
  type = item_type
  case value
  in Hash
    value.transform_values do |val|
      FinchAPI::Converter.dump(type, val)
    end
  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))


810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/finch-api/base_model.rb', line 810

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

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

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