class Axiom::Types::Collection
Represents a collection type
Public Class Methods
Finalize by setting up constraints for the member
@return [Class<Axiom::Types::Collection>]
@api private
# File lib/axiom/types/collection.rb, line 56 def self.finalize return self if frozen? member_type.finalize matches_member_type super end
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
# File lib/axiom/types/collection.rb, line 42 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/collection.rb, line 126 def self.base? # noop end
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 101 def self.infer_from(member_type) self if self.member_type.equal?(member_type) end
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 85 def self.infer_from_primitive_instance(object) member_type = Types.infer(object.first) || Object infer_from(member_type) || new_from(member_type) end
Test if the type matches a primitive class
@param [Object] object
@return [Boolean]
@api private
# File lib/axiom/types/collection.rb, line 70 def self.match_primitive?(*) super && member_type.equal?(Object) end
Add a constraints for the member
@return [undefined]
@api private
# File lib/axiom/types/collection.rb, line 136 def self.matches_member_type constraint do |object| object.all? { |member| member_type.include?(member) } end end
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 116 def self.new_from(member_type) new { member_type(member_type) } if base? end