Class: Type
Overview
Base class for Euphoria-like Types.
class KiloType < Type
condition do |x|
x.case? Integer
x.kind_of?(Integer)
x.respond_to?(:succ)
x > 1000
end
end
Becuase the x.something? is so common, TypeCast provides special "magic-dot" method to make defining these conditions more concise.
class KiloType < Type
x.case? Integer
x.kind_of?(Integer)
x.respond_to?(:succ)
x > 1000
end
While TypeCasts are not actual types in the sense they are not actual classes. They can be used for conversion by defining a "from_<class>" class method. In doing so you should make sure the result of the conversion conforms to the typecast. You can use the TypeCast.validate method to make that a bit easier. For instance:
class KiloType
def from_string(str)
validate(str.to_i)
end
end
TypeCast also provides a helper DSL method that handles this for you.
class KiloType
conversion String do |str|
str.to_i
end
end
This will define a method equivalent to the prior example.
Defined Under Namespace
Classes: Conditions
Class Method Summary (collapse)
- + (Object) ===(obj)
-
+ (Object) check(on_or_off = nil)
Activeate/Deactivate type-checking globally (NOT USED YET).
- + (Object) condition(&block)
- + (Object) conditions
- + (Object) conversion(klass, &block)
- + (Object) index
- + (Object) validate(obj)
- + (Object) x
Class Method Details
+ (Object) ===(obj)
125 126 127 128 129 130 131 132 133 |
# File 'lib/platypus/type.rb', line 125 def self.===(obj) #conditions.all? do |s, a, b| # obj.__send__(s, *a, &b) #end instance = new conditions.all? do |method| instance.__send__(method, obj) end end |
+ (Object) check(on_or_off = nil)
Activeate/Deactivate type-checking globally (NOT USED YET).
52 53 54 55 |
# File 'lib/platypus/type.rb', line 52 def self.check(on_or_off=nil) @check = on_or_off unless on_or_off.nil? @check end |
+ (Object) condition(&block)
63 64 65 66 67 68 |
# File 'lib/platypus/type.rb', line 63 def self.condition(&block) @index ||= index @index = @index.succ define_method("condition_#{@index}", &block) #@conditions << block end |
+ (Object) conditions
119 120 121 122 |
# File 'lib/platypus/type.rb', line 119 def self.conditions #x.__conditions__ + (@conditions || []) + (defined?(super) ? super : []) instance_methods.select{ |m| m.to_s =~ /^condition_/ } end |
+ (Object) conversion(klass, &block)
71 72 73 74 75 76 77 78 79 |
# File 'lib/platypus/type.rb', line 71 def self.conversion(klass, &block) name = klass.name.downcase.gsub('::', '_') (class << self; self; end).class_eval do define_method("from_#{name}") do |from| r = block.call(from) validate(r) end end end |
+ (Object) index
113 114 115 116 117 |
# File 'lib/platypus/type.rb', line 113 def self.index methods = instance_methods.select{ |m| m.to_s =~ /^condition_/ } indexes = methods.map{ |m| m.split('_')[1].to_i } indexes.max || 0 end |
+ (Object) validate(obj)
57 58 59 60 |
# File 'lib/platypus/type.rb', line 57 def self.validate(obj) raise TypeError unless self === obj return obj end |
+ (Object) x
82 83 84 |
# File 'lib/platypus/type.rb', line 82 def self.x @x ||= Conditions.new(self) end |