Class: T::Types::TypedHash

Inherits:
TypedEnumerable show all
Defined in:
lib/types/types/typed_hash.rb

Direct Known Subclasses

Untyped

Defined Under Namespace

Classes: Untyped

Instance Method Summary collapse

Methods inherited from TypedEnumerable

#describe_obj

Methods inherited from Base

#==, #describe_obj, #error_message_for_obj, #error_message_for_obj_recursive, #hash, method_added, #subtype_of?, #to_s, #validate!

Constructor Details

#initialize(keys:, values:) ⇒ TypedHash

Returns a new instance of TypedHash.



10
11
12
13
# File 'lib/types/types/typed_hash.rb', line 10

def initialize(keys:, values:)
  @inner_keys = keys
  @inner_values = values
end

Instance Method Details

#build_typeObject



29
30
31
32
33
# File 'lib/types/types/typed_hash.rb', line 29

def build_type
  keys
  values
  super
end

#keysObject

Technically we don’t need this, but it is a nice api



16
17
18
# File 'lib/types/types/typed_hash.rb', line 16

def keys
  @keys ||= T::Utils.coerce(@inner_keys)
end

#nameObject

overrides Base



36
37
38
# File 'lib/types/types/typed_hash.rb', line 36

def name
  "T::Hash[#{keys.name}, #{values.name}]"
end

#new(*args, &blk) ⇒ Object



50
51
52
# File 'lib/types/types/typed_hash.rb', line 50

def new(*args, &blk)
  Hash.new(*T.unsafe(args), &blk)
end

#recursively_valid?(obj) ⇒ Boolean

overrides Base

Returns:



41
42
43
# File 'lib/types/types/typed_hash.rb', line 41

def recursively_valid?(obj)
  obj.is_a?(Hash) && super
end

#typeObject



25
26
27
# File 'lib/types/types/typed_hash.rb', line 25

def type
  @type ||= T::Utils.coerce([keys, values])
end

#underlying_classObject



6
7
8
# File 'lib/types/types/typed_hash.rb', line 6

def underlying_class
  Hash
end

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



46
47
48
# File 'lib/types/types/typed_hash.rb', line 46

def valid?(obj)
  obj.is_a?(Hash)
end

#valuesObject

Technically we don’t need this, but it is a nice api



21
22
23
# File 'lib/types/types/typed_hash.rb', line 21

def values
  @values ||= T::Utils.coerce(@inner_values)
end