class Axiom::Types::Hash
Represents a hash type
Public Class Methods
Finalize by setting up constraints for the key and value
@return [Class<Axiom::Types::Hash>]
@api private
# File lib/axiom/types/hash.rb, line 42 def self.finalize return self if frozen? key_type.finalize value_type.finalize matches_key_and_value_types super end
Infer the type of the object
@example
type = Axiom::Types.infer(object)
@param [Object] object
@return [Class<Axiom::Types::Hash>]
returned if the type matches
@return [nil]
returned if the type does not match
@api public
# File lib/axiom/types/hash.rb, line 28 def self.infer(object) case object when primitive infer_from_primitive_instance(object) else super end end
Private Class Methods
Test if the type is a base type
@return [Boolean]
@api private
# File lib/axiom/types/hash.rb, line 118 def self.base? equal?(Hash) end
Infer the type from the key_type and value_type
@param [Class<Axiom::Types::Object>] key_type @param [Class<Axiom::Types::Object>] value_type
@return [Class<Axiom::Types::Hash>]
returned if the key_type and value_type match
@return [nil]
returned if the key_type and value_type do not match
@api private
# File lib/axiom/types/hash.rb, line 91 def self.infer_from(key_type, value_type) self if self.key_type.equal?(key_type) && self.value_type.equal?(value_type) end
Infer the type from a primitive instance
@param [Object] object
@return [Class<Axiom::Types::Hash>]
returned if the primitive instance matches
@return [nil]
returned if the primitive instance does not match
@api private
# File lib/axiom/types/hash.rb, line 72 def self.infer_from_primitive_instance(object) key, value = object.first key_type = Types.infer(key) || Object value_type = Types.infer(value) || Object infer_from(key_type, value_type) || new_from(key_type, value_type) end
Test if the type matches a primitive class
@param [Object] object
@return [Boolean]
@api private
# File lib/axiom/types/hash.rb, line 57 def self.match_primitive?(*) super && key_type.equal?(Object) && value_type.equal?(Object) end
Add a constraints for the key and value
@return [undefined]
@api private
# File lib/axiom/types/hash.rb, line 128 def self.matches_key_and_value_types constraint do |object| object.all? do |key, value| key_type.include?(key) && value_type.include?(value) end end end
Instantiate a new type from a base type
@param [Class<Axiom::Types::Object>] key_type @param [Class<Axiom::Types::Object>] value_type
@return [Class<Axiom::Types::Hash>]
returned if a base type
@return [nil]
returned if not a base type
@api private
# File lib/axiom/types/hash.rb, line 108 def self.new_from(key_type, value_type) new { key_type(key_type).value_type(value_type) } if base? end