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.
-
.register ⇒ void
Registers 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.
92 93 94 |
# File 'lib/mixture/types/type.rb', line 92 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.
43 44 45 |
# File 'lib/mixture/types/type.rb', line 43 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.
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/mixture/types/type.rb', line 140 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.
23 24 25 |
# File 'lib/mixture/types/type.rb', line 23 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.
82 83 84 |
# File 'lib/mixture/types/type.rb', line 82 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.
52 53 54 55 |
# File 'lib/mixture/types/type.rb', line 52 def self.inherited(sub) 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.
101 102 103 |
# File 'lib/mixture/types/type.rb', line 101 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.
32 33 34 |
# File 'lib/mixture/types/type.rb', line 32 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.
71 72 73 74 75 |
# File 'lib/mixture/types/type.rb', line 71 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.
15 16 17 |
# File 'lib/mixture/types/type.rb', line 15 def self. @options ||= ThreadSafe::Hash.new end |
.register ⇒ void
This method returns an undefined value.
Registers the type. This shouldn't be called on anonymous classes.
61 62 63 |
# File 'lib/mixture/types/type.rb', line 61 def self.register Types.types << self 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.
106 107 108 109 110 111 112 |
# File 'lib/mixture/types/type.rb', line 106 def self.to_s if anonymous? && .key?(:name) [:name] else super end end |