Module: Taipo::Check
- Defined in:
- lib/taipo/check.rb
Overview
A simple DSL for declaring type checks to run against specified variables
Check works by:
-
extracting the values of the arguments to be checked from a Binding;
-
transforming the type definitions provided as Strings into an array of TypeElement instances; and
-
checking whether the argument’s value matches any of the instances of TypeElement in the array.
As syntactic sugar, the Check module will by default alias Kernel#binding with the keyword types
. This allows the user to call #check by writing check types (with a similar syntax for #review). If the user does not want to alias, they can set alias= to false
before including or extending Check.
Class Method Summary collapse
-
.extended(extender) ⇒ Object
private
Perform operations if this module is extended.
-
.included(includer) ⇒ Object
private
Perform operations if this module is included.
Instance Method Summary collapse
-
#check(context, collect_invalids = false, **checks) ⇒ Array
Check whether the given arguments match the given type definition in the given context.
-
#review(context, **checks) ⇒ Array
Review whether the given arguments match the given type definition in the given context.
Class Method Details
.extended(extender) ⇒ Object
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.
Perform operations if this module is extended
This is the callback called by Ruby when a module is included. In this case, the callback will alias the method __types__
as types
if Taipo.alias? returns true. @@alias is reset to true at the end of this method.
124 125 126 127 128 |
# File 'lib/taipo/check.rb', line 124 def self.extended(extender) extender.singleton_class.send(:alias_method, :types, :__types__) if Taipo.alias? Taipo.alias = true end |
.included(includer) ⇒ Object
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.
Perform operations if this module is included
This is the callback called by Ruby when a module is included. In this case, the callback will alias the method __types__
as types
if Taipo.alias? returns true. @@alias is reset to true at the end of this method.
141 142 143 144 |
# File 'lib/taipo/check.rb', line 141 def self.included(includer) includer.send(:alias_method, :types, :__types__) if Taipo.alias? Taipo.alias = true end |
Instance Method Details
#check(context, collect_invalids = false, **checks) ⇒ Array
Check whether the given arguments match the given type definition in the given context
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/taipo/check.rb', line 76 def check(context, collect_invalids = false, **checks) msg = "The first argument to this method must be of type Binding." raise ::TypeError, msg unless context.is_a? Binding checks.reduce(Array.new) do |memo,(k,v)| arg = Taipo::Utilities.extract_variable(name: k, object: self, context: context) is_match = Taipo::Utilities.match? object: arg, definition: v unless collect_invalids || is_match Taipo::Utilities.throw_error object: arg, name: k, definition: v end (is_match) ? memo : memo.push(k) end end |
#review(context, **checks) ⇒ Array
Review whether the given arguments match the given type definition in the given context
This is a convenience method for calling #check with collect_invalids
set to true.
109 110 111 |
# File 'lib/taipo/check.rb', line 109 def review(context, **checks) self.check(context, true, checks) end |