Module: Mixture::Types

Defined in:
lib/mixture/types.rb,
lib/mixture/types/nil.rb,
lib/mixture/types/set.rb,
lib/mixture/types/date.rb,
lib/mixture/types/type.rb,
lib/mixture/types/time.rb,
lib/mixture/types/hash.rb,
lib/mixture/types/class.rb,
lib/mixture/types/array.rb,
lib/mixture/types/float.rb,
lib/mixture/types/range.rb,
lib/mixture/types/object.rb,
lib/mixture/types/string.rb,
lib/mixture/types/symbol.rb,
lib/mixture/types/access.rb,
lib/mixture/types/numeric.rb,
lib/mixture/types/integer.rb,
lib/mixture/types/boolean.rb,
lib/mixture/types/rational.rb,
lib/mixture/types/datetime.rb,
lib/mixture/types/enumerable.rb,
lib/mixture/types/collection.rb

Overview

Contains information about types.

Defined Under Namespace

Modules: Access Classes: Array, Boolean, Class, Collection, Date, DateTime, Enumerable, Float, Hash, Integer, Nil, Numeric, Object, Range, Rational, Set, String, Symbol, Time, Type

Class Method Summary collapse

Class Method Details

.infer(object) ⇒ Mixture::Types::Type

Infers an object's type. It first checks the mappings to see if the object given is in the mappings; if it's not, it checks if it is a class. If it is a class, it passes it over to infer_class; otherwise, it passes it over to infer_type.

Parameters:

  • object (Object)

    The object to infer the type of.

Returns:

See Also:


38
39
40
41
42
43
44
45
46
# File 'lib/mixture/types.rb', line 38

def self.infer(object)
  mappings.fetch(object) do
    if object.is_a?(::Class)
      infer_class(object)
    else
      infer_type(object)
    end
  end
end

.infer_class(object) ⇒ Mixture::Types::Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Infers the class of the given object. If the object is a type, it just returns the object. Otherwise, it searches types for a primitive that matches the object. If one is found, it is returned; otherwise, a new Class type is created using Mixture::Types::Access#[]. This is primarily used for user-defined classes.

Examples:

Creating a type for a user-defined class.

class MyClass; end
Mixture::Types.infer(MyClass) # => Mixture::Types::Class[MyClass]

Parameters:

  • object (Class)

    The object to infer the type of.

Returns:

See Also:


61
62
63
64
# File 'lib/mixture/types.rb', line 61

def self.infer_class(object)
  return object if object <= Type
  types.find { |type| type.options[:primitive] == object } || Class[object]
end

.infer_type(object) ⇒ Mixture::Types::Type

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This may not return a type if the object is a BasicObject. This is because of the constraints on the Object type.

Infers the type of the object. If the object is an array or set, it returns an Array or Set type with the object's first element's type as the member type. Otherwise, it tries to find a type that matches the object using Mixture::Types::Type.matches?. This will almost always return a type, if not Object.

Examples:

Infers the type of an array of elements.

Mixture::Types.infer([1])
# => Mixture::Types::Array[Mixture::Types::Integer]

Parameters:

  • object (Object)

    The object to infer.

Returns:


81
82
83
84
85
86
87
88
89
90
# File 'lib/mixture/types.rb', line 81

def self.infer_type(object)
  case object
  when ::Array then Array[object.first]
  when ::Set   then Set[object.first]
  when ::Hash  then Hash[object.keys.first => object.values.first]
  else
    types.find { |type| type.matches?(object) } ||
      infer_class(object.class)
  end
end

.mappingsHash{Symbol => Mixture::Types::Type}

A list of the mappings that all types have. This is used primarily to map a type's symbol name to its type (e.g. :string to String). This is also used for boolean mapping.


20
21
22
23
24
25
26
# File 'lib/mixture/types.rb', line 20

def self.mappings
  ::Hash[types.flat_map do |type|
    type.mappings.map do |name|
      [name, type]
    end
  end]
end

.typesArray<Class>

A list of all of the types (non-anonymous) that are known.

Returns:


11
12
13
# File 'lib/mixture/types.rb', line 11

def self.types
  @types ||= ThreadSafe::Array.new
end