Class: Class
Instance Method Summary collapse
-
#from(source, specifics = {}) ⇒ Object
Convert
source
to an instance of the class. -
#typecast(target_class, *specifics, &block) ⇒ Object
Define a type cast.
- #typecasts ⇒ Object
Instance Method Details
#from(source, specifics = {}) ⇒ Object
Convert source
to an instance of the class.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/platypus/typecast.rb', line 17 def from(source, specifics={}) set = (specifics.empty? ? nil : Set.new(specifics.keys)) base = ancestors.find{ |anc| source.class.typecasts.key?(anc) } if base cast = source.class.typecasts[base] if block = cast[set] if block.arity == 1 return block.call(source) else return block.call(source, specifics) end end end raise TypeError end |
#typecast(target_class, *specifics, &block) ⇒ Object
Define a type cast.
10 11 12 13 14 |
# File 'lib/platypus/typecast.rb', line 10 def typecast(target_class, *specifics, &block) set = (specifics.empty? ? nil : Set.new(specifics)) typecasts[target_class] ||= {} typecasts[target_class][set] = block end |
#typecasts ⇒ Object
5 6 7 |
# File 'lib/platypus/typecast.rb', line 5 def typecasts @typecasts ||= {} end |