Class: T::Types::TypedHash

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

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, #hash, method_added, #subtype_of?, #to_s, #validate!

Constructor Details

#initialize(keys:, values:) ⇒ TypedHash

Returns a new instance of TypedHash.



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

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



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

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

#new(*args, &blk) ⇒ Object

rubocop:disable PrisonGuard/BanBuiltinMethodOverride



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

def new(*args, &blk) # rubocop:disable PrisonGuard/BanBuiltinMethodOverride
  Hash.new(*T.unsafe(args), &blk) # rubocop:disable PrisonGuard/RestrictHashDefaults
end

#valid?(obj) ⇒ Boolean

Returns:



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

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