Class: Clive::Type
- Inherits:
-
Object
- Object
- Clive::Type
- Defined in:
- lib/clive/type.rb,
lib/clive/type/lookup.rb,
lib/clive/type/definitions.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Lookup Classes: Array, Binary, Boolean, Float, Hexadecimal, Integer, Object, Octal, Pathname, Range, Regexp, StrictInteger, String, Symbol, Time
Class Method Summary collapse
-
.cast(sym, *args) ⇒ Object
Shorthand to define a method which is called on the string argument to return the correct type.
-
.find_class(name) ⇒ Object
Find the class for
name
. -
.match(other) ⇒ Object
Shorthand to define #valid? for subclasses of Type, pass a regular expression that should be matched or a symbol for a method which will be called on the argument that returns either
true
(valid) orfalse
(invalid). -
.refute(other) ⇒ Object
Similar to Type.match but opposite, so where Type.match would be valid refute is invalid.
- .typecast(arg) ⇒ Object
-
.valid?(arg) ⇒ Boolean
Checks whether the
arg
passed is valid, if Type.match or Type.refute have been called it uses the Proc created by them otherwise calls #valid?.
Instance Method Summary collapse
Class Method Details
.cast(sym, *args) ⇒ Object
Shorthand to define a method which is called on the string argument to return the correct type.
82 83 84 |
# File 'lib/clive/type.rb', line 82 def cast(sym, *args) @cast = [sym, args] end |
.find_class(name) ⇒ Object
Find the class for name
.
18 19 20 21 |
# File 'lib/clive/type.rb', line 18 def find_class(name) name = name.split('::').last Clive::Type.const_get(name) if Clive::Type.const_defined?(name) end |
.match(other) ⇒ Object
Shorthand to define #valid? for subclasses of Clive::Type, pass a regular expression that should be matched or a symbol for a method which will be called on the argument that returns either true
(valid) or false
(invalid).
50 51 52 53 54 55 56 |
# File 'lib/clive/type.rb', line 50 def match(other) if other.respond_to?(:to_proc) @valid = other.to_proc else @valid = proc {|arg| other =~ arg.to_s } end end |
.refute(other) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/clive/type.rb', line 62 def refute(other) if other.respond_to?(:to_proc) @valid = proc {|arg| !arg.send(other) } else @valid = proc {|arg| other !~ arg.to_s } end end |
.typecast(arg) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/clive/type.rb', line 103 def typecast(arg) if @cast arg.send @cast[0], *@cast[1] else new.typecast arg end end |