Class: FinchAPI::Type::HashOf Abstract Private

Inherits:
Object
  • Object
show all
Includes:
Converter
Defined in:
lib/finch-api/type/hash_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.

Hash 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 = {}) ⇒ 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::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?”



130
131
132
133
# File 'lib/finch-api/type/hash_of.rb', line 130

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/hash_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)


49
# File 'lib/finch-api/type/hash_of.rb', line 49

def ==(other) = other.is_a?(FinchAPI::HashOf) && 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/finch-api/type/hash_of.rb', line 29

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, state:) ⇒ 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)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean, :strong] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Integer] :branched

Returns:

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


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/finch-api/type/hash_of.rb', line 64

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

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

  target = item_type
  exactness[:yes] += 1
  value
    .to_h do |key, val|
      k = key.is_a?(String) ? key.to_sym : key
      v =
        case [nilable?, val]
        in [true, nil]
          exactness[:yes] += 1
          nil
        else
          FinchAPI::Type::Converter.coerce(target, val, state: state)
        end

      exactness[:no] += 1 unless k.is_a?(Symbol)
      [k, v]
    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)


96
97
98
99
100
101
102
103
104
105
# File 'lib/finch-api/type/hash_of.rb', line 96

def dump(value)
  target = item_type
  if value.is_a?(Hash)
    value.transform_values do
      FinchAPI::Type::Converter.dump(target, _1)
    end
  else
    super
  end
end