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/hash.rb,
lib/mixture/types/time.rb,
lib/mixture/types/type.rb,
lib/mixture/types/array.rb,
lib/mixture/types/class.rb,
lib/mixture/types/float.rb,
lib/mixture/types/range.rb,
lib/mixture/types/access.rb,
lib/mixture/types/object.rb,
lib/mixture/types/string.rb,
lib/mixture/types/symbol.rb,
lib/mixture/types/boolean.rb,
lib/mixture/types/integer.rb,
lib/mixture/types/numeric.rb,
lib/mixture/types/datetime.rb,
lib/mixture/types/rational.rb,
lib/mixture/types/collection.rb,
lib/mixture/types/enumerable.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
-
.infer(object) ⇒ Mixture::Types::Type
Infers an object's type.
-
.infer_class(object) ⇒ Mixture::Types::Type
private
Infers the class of the given object.
-
.infer_type(object) ⇒ Mixture::Types::Type
private
Infers the type of the object.
-
.mappings ⇒ Hash{Symbol => Mixture::Types::Type}
A list of the mappings that all types have.
-
.types ⇒ Array<Class>
A list of all of the types (non-anonymous) that are known.
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.
39 40 41 42 43 44 45 46 47 |
# File 'lib/mixture/types.rb', line 39 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.
62 63 64 65 |
# File 'lib/mixture/types.rb', line 62 def self.infer_class(object) return object if object <= Type types.find { |type| type.[: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.
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.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mixture/types.rb', line 82 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 |
.mappings ⇒ Hash{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.
21 22 23 24 25 26 27 |
# File 'lib/mixture/types.rb', line 21 def self.mappings ::Hash[types.flat_map do |type| type.mappings.map do |name| [name, type] end end] end |