class Axiom::Types::Hash

Represents a hash type

Public Class Methods

finalize() click to toggle source

Finalize by setting up constraints for the key and value

@return [Class<Axiom::Types::Hash>]

@api private

Calls superclass method
# 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(object) click to toggle source

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

Calls superclass method
# 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

base?() click to toggle source

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_from(key_type, value_type) click to toggle source

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_from_primitive_instance(object) click to toggle source

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
match_primitive?(*) click to toggle source

Test if the type matches a primitive class

@param [Object] object

@return [Boolean]

@api private

Calls superclass method
# File lib/axiom/types/hash.rb, line 57
def self.match_primitive?(*)
  super && key_type.equal?(Object) && value_type.equal?(Object)
end
matches_key_and_value_types() click to toggle source

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
new_from(key_type, value_type) click to toggle source

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