class Yukata::Coercer
The class that coerces objects based on the definitions that are registered with it.
@author Matthew A. Johnston
Public Class Methods
new()
click to toggle source
# File lib/yukata/coercer.rb, line 7 def initialize @coercions = Hash.new do |hash, origin| hash[origin] = Hash.new do |h, target| h[target] = Coercion.new(origin, target) end end @mutex = Mutex.new end
Public Instance Methods
coerce(object, target)
click to toggle source
@param object [Object] the object to coerce @param target [Class] what you want the object to turn in to
# File lib/yukata/coercer.rb, line 40 def coerce(object, target) @mutex.synchronize do @coercions[object.class][target].call(object) end end
register(origin, target, &block)
click to toggle source
Registers a coercion with the yukata library
@param origin [Class] the class to convert @param target [Class] what the origin will be converted to
# File lib/yukata/coercer.rb, line 20 def register(origin, target, &block) raise(ArgumentError, 'block is required') unless block_given? @mutex.synchronize do @coercions[origin][target] = Coercion.new(origin, target, &block) end end
unregister(origin, target)
click to toggle source
Removes a coercion from the library
@param origin [Class] @param target [Class]
# File lib/yukata/coercer.rb, line 32 def unregister(origin, target) @mutex.synchronize do @coercions[origin].delete(target) end end