Class: Mixture::Types::Type
- Inherits:
-
Object
- Object
- Mixture::Types::Type
- Defined in:
- lib/mixture/types/type.rb
Overview
A single type. A type is never instantized; it is used as a class to represent a type of value.
Direct Known Subclasses
Class Method Summary collapse
-
.anonymous? ⇒ Boolean
If this class is anonymous.
-
.as(*names) ⇒ void
Sets some names that this type can go under.
-
.constraint(value = Undefined, &block) ⇒ Object
This is used to determine if a specific object is this type.
-
.constraints ⇒ Array<Proc{(Object) => Boolean}>
Constraints on the type.
-
.inheritable ⇒ Array<Class>
A list of types that this type can inherit coercion behavior from.
-
.inherited(sub) ⇒ void
Called by ruby when a class inherits this class.
-
.inspect ⇒ String
Inspects the class.
-
.mappings ⇒ Array<Symbol, Object>
Returns all of the names that this type can go under.
-
.matches?(value) ⇒ Boolean
Checks if the given value passes all of the constraints defined on this type.
-
.options ⇒ Hash{Symbol => Object}
The options for the type.
-
.to_s ⇒ String
Inspects the class.
Class Method Details
.anonymous? ⇒ Boolean
If this class is anonymous. This is counting on the fact that the name of an anonymous class is nil; however, assigning a class to a constant on initialization of a class will make that class non-anonymous.
84 85 86 |
# File 'lib/mixture/types/type.rb', line 84 def self.anonymous? name.nil? end |
.as(*names) ⇒ void
This method returns an undefined value.
Sets some names that this type can go under. This is used for mappings and for inference.
42 43 44 |
# File 'lib/mixture/types/type.rb', line 42 def self.as(*names) mappings.concat(names) end |
.self.constraint(value) ⇒ void .self.constraint {|value| ... } ⇒ void
Constraints are not meant for validation. Constraints are purely meant for identification, and should be used as such.
This is used to determine if a specific object is this type. There can be many constraints, and they're all used to check the given object.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mixture/types/type.rb', line 132 def self.constraint(value = Undefined, &block) if block_given? constraints << block elsif value != Undefined constraints << value else fail ArgumentError, "Expected an argument or a block, " \ "got neither" end end |
.constraints ⇒ Array<Proc{(Object) => Boolean}>
Constraints on the type. A value will not match the type if any of these constraints fail. This is inherited by subtypes.
22 23 24 |
# File 'lib/mixture/types/type.rb', line 22 def self.constraints @constraints ||= ThreadSafe::Array.new end |
.inheritable ⇒ Array<Class>
A list of types that this type can inherit coercion behavior from. For example, a collection can be coerced into an array or a set.
74 75 76 |
# File 'lib/mixture/types/type.rb', line 74 def self.inheritable ancestors - Type.ancestors end |
.inherited(sub) ⇒ void
This method returns an undefined value.
Called by ruby when a class inherits this class. This just propogates the options and constraints to the new subclass.
51 52 53 54 55 |
# File 'lib/mixture/types/type.rb', line 51 def self.inherited(sub) Types.types << sub unless sub.anonymous? sub..merge!() sub.constraints.concat(constraints) end |
.inspect ⇒ String
Inspects the class. If the class is anonymous, it uses the
:name value in the options if it exists. Otherwise, it
passes it up the chain.
93 94 95 |
# File 'lib/mixture/types/type.rb', line 93 def self.inspect to_s end |
.mappings ⇒ Array<Symbol, Object>
Returns all of the names that this type can go under. This is used for Mixture::Types.mappings and for inference.
31 32 33 |
# File 'lib/mixture/types/type.rb', line 31 def self.mappings @mappings ||= ThreadSafe::Array.new end |
.matches?(value) ⇒ Boolean
Checks if the given value passes all of the constraints defined on this type. Each constraint is executed within the context of the class, to provide access to options.
63 64 65 66 67 |
# File 'lib/mixture/types/type.rb', line 63 def self.matches?(value) constraints.all? do |constraint| class_exec(value, &constraint) end end |
.options ⇒ Hash{Symbol => Object}
The options for the type. This is inherited by subtypes.
14 15 16 |
# File 'lib/mixture/types/type.rb', line 14 def self. ||= ThreadSafe::Hash.new end |
.to_s ⇒ String
Inspects the class. If the class is anonymous, it uses the
:name value in the options if it exists. Otherwise, it
passes it up the chain.
98 99 100 101 102 103 104 |
# File 'lib/mixture/types/type.rb', line 98 def self.to_s if anonymous? && .key?(:name) [:name] else super end end |