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 Attribute Summary collapse

Attributes inherited from TypedEnumerable

#type

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.



13
14
15
16
17
# File 'lib/types/types/typed_hash.rb', line 13

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

Instance Attribute Details

#keysObject (readonly)

Technically we don’t need these, but they are a nice api



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

def keys
  @keys
end

#valuesObject (readonly)

Technically we don’t need these, but they are a nice api



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

def values
  @values
end

Instance Method Details

#nameObject

overrides Base



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

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

#new(*args, &blk) ⇒ Object



34
35
36
# File 'lib/types/types/typed_hash.rb', line 34

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

#recursively_valid?(obj) ⇒ Boolean

overrides Base

Returns:



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

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

#underlying_classObject



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

def underlying_class
  Hash
end

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



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

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