Class: Philosophal::Types::HashOfType

Inherits:
Object
  • Object
show all
Defined in:
lib/philosophal/types/hash_of_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subtype) ⇒ HashOfType

Returns a new instance of HashOfType.



19
20
21
22
# File 'lib/philosophal/types/hash_of_type.rb', line 19

def initialize(subtype)
  @subtype = subtype.freeze
  self.class.memorization[Marshal.dump(subtype)] = self
end

Instance Attribute Details

#subtypeObject (readonly)

Returns the value of attribute subtype.



17
18
19
# File 'lib/philosophal/types/hash_of_type.rb', line 17

def subtype
  @subtype
end

Class Method Details

.instance(key_type, value_type) ⇒ Object



6
7
8
9
# File 'lib/philosophal/types/hash_of_type.rb', line 6

def self.instance(key_type, value_type)
  subtype = { key_type:, value_type: }
  memorization.fetch(Marshal.dump(subtype), new(subtype).freeze)
end

.memorizationObject



11
12
13
14
15
# File 'lib/philosophal/types/hash_of_type.rb', line 11

def self.memorization
  return @memorization if defined?(@memorization)

  @memorization = {}
end

Instance Method Details

#===(hash) ⇒ Object



24
25
26
27
28
# File 'lib/philosophal/types/hash_of_type.rb', line 24

def ===(hash)
  hash.is_a?(Hash) &&
    !hash.keys.find { |key| !key.is_a?(@subtype[:key_type]) } &&
    !hash.values.find { |value| !value.is_a?(@subtype[:value_type]) }
end