Class: Pangea::Internal::Type::HashOf Abstract Private

Inherits:
Object
  • Object
show all
Includes:
Converter
Defined in:
lib/pangea/internal/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, Pangea::Internal::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?”



148
149
150
151
# File 'lib/pangea/internal/type/hash_of.rb', line 148

def initialize(type_info, spec = {})
  @item_type_fn = Pangea::Internal::Type::Converter.type_info(type_info || spec)
  @nilable = spec.fetch(:nil?, false)
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, Pangea::Internal::Type::Converter, Class)
  • spec (Hash{Symbol=>Object})

    .

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

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :“nil?”



31
# File 'lib/pangea/internal/type/hash_of.rb', line 31

def self.[](...) = new(...)

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:



56
57
58
59
60
# File 'lib/pangea/internal/type/hash_of.rb', line 56

def ==(other)
  # rubocop:disable Layout/LineLength
  other.is_a?(Pangea::Internal::Type::HashOf) && other.nilable? == nilable? && other.item_type == item_type
  # rubocop:enable Layout/LineLength
end

#===(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:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pangea/internal/type/hash_of.rb', line 36

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)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pangea/internal/type/hash_of.rb', line 78

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
          Pangea::Internal::Type::Converter.coerce(target, val, state: state)
        end

      exactness[:no] += 1 unless k.is_a?(Symbol)
      [k, v]
    end
end

#dump(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] :can_retry

Returns:

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


114
115
116
117
118
119
120
121
122
123
# File 'lib/pangea/internal/type/hash_of.rb', line 114

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

#hashInteger

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:

  • (Integer)


63
# File 'lib/pangea/internal/type/hash_of.rb', line 63

def hash = [self.class, item_type].hash

#inspect(depth: 0) ⇒ String

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:

  • depth (Integer) (defaults to: 0)

Returns:

  • (String)


158
159
160
161
162
# File 'lib/pangea/internal/type/hash_of.rb', line 158

def inspect(depth: 0)
  items = Pangea::Internal::Type::Converter.inspect(item_type, depth: depth.succ)

  "#{self.class}[#{[items, nilable? ? 'nil' : nil].compact.join(' | ')}]"
end