Class: Types::Hash

Inherits:
Object
  • Object
show all
Includes:
Generic
Defined in:
lib/types/hash.rb

Overview

Represents a hash type with key and value types.

type = Types::Hash.new(Types::String, Types::Integer)
type.parse({"foo" => "42"}) # => {"foo" => 42}

Instance Method Summary collapse

Methods included from Generic

#|

Constructor Details

#initialize(key_type, value_type) ⇒ Hash

Returns a new instance of Hash.



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

def initialize(key_type, value_type)
	@key_type = key_type
	@value_type = value_type
end

Instance Method Details

#composite? ⇒ Boolean

Returns:



27
28
29
# File 'lib/types/hash.rb', line 27

def composite?
	true
end

#parse(input) ⇒ Object

Parses the input as a hash with the specified key and value types.



35
36
37
38
39
40
41
42
43
44
# File 'lib/types/hash.rb', line 35

def parse(input)
	case input
	when ::String
		return parse_values(parse_string(input))
	when ::Hash
		return parse_values(input)
	else
		raise ArgumentError, "Cannot coerce #{input.inspect} into Hash!"
	end
end

#resolve ⇒ Object

Resolves to the actual Ruby Hash class.



58
59
60
# File 'lib/types/hash.rb', line 58

def resolve
	::Hash
end

#to_rbs ⇒ Object



52
53
54
# File 'lib/types/hash.rb', line 52

def to_rbs
	"Hash[#{@key_type.to_rbs}, #{@value_type.to_rbs}]"
end

#to_s ⇒ Object



47
48
49
# File 'lib/types/hash.rb', line 47

def to_s
	"Hash(#{@key_type}, #{@value_type})"
end