class Axiom::Types::Collection

Represents a collection type

Public Class Methods

finalize() click to toggle source

Finalize by setting up constraints for the member

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

@api private

Calls superclass method
# File lib/axiom/types/collection.rb, line 57
def self.finalize
  return self if frozen?
  member_type.finalize
  matches_member_type
  super
end
infer(object) click to toggle source

Infer the type of the object

@example with a type

Axiom::Types::Array.infer(Axiom::Types::Array)
# => Axiom::Types::Array

@example with a primitive class

Axiom::Types::Collection.infer(::Array)
# => Axiom::Types::Array

@example with a primitive instance

Axiom::Types::Array.infer(Array[])
# => Axiom::Types::Array

@example with a primitive instance and a member type

Axiom::Types::Collection.infer(Array[Axiom::Types::String])
# => Axiom::Types::Array subclass w/String member type

@example with a primitive instance and a member primitive

Axiom::Types::Collection.infer(Array[String])
# => Axiom::Types::Array subclass w/String member type

@param [Object] object

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

returned if the type matches

@return [nil]

returned if the type does not match

@api public

Calls superclass method
# File lib/axiom/types/collection.rb, line 43
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/collection.rb, line 127
def self.base?
  # noop
end
infer_from(member_type) click to toggle source

Infer the type from the member_type

@param [Class<Axiom::Types::Object>] member_type

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

returned if the member_type matches

@return [nil]

returned if the member_type does not match

@api private

# File lib/axiom/types/collection.rb, line 102
def self.infer_from(member_type)
  self if self.member_type.equal?(member_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::Collection>]

returned if the primitive instance matches

@return [nil]

returned if the primitive instance does not match

@api private

# File lib/axiom/types/collection.rb, line 86
def self.infer_from_primitive_instance(object)
  member_type = Types.infer(object.first) || Object
  infer_from(member_type) || new_from(member_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/collection.rb, line 71
def self.match_primitive?(*)
  super && member_type.equal?(Object)
end
matches_member_type() click to toggle source

Add a constraints for the member

@return [undefined]

@api private

# File lib/axiom/types/collection.rb, line 137
def self.matches_member_type
  constraint do |object|
    object.all? { |member| member_type.include?(member) }
  end
end
new_from(member_type) click to toggle source

Instantiate a new type from a base type

@param [Class<Axiom::Types::Object>] member_type

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

returned if a base type

@return [nil]

returned if not a base type

@api private

# File lib/axiom/types/collection.rb, line 117
def self.new_from(member_type)
  new { member_type(member_type) } if base?
end