class Garcon::Coercer
Public Class Methods
new()
click to toggle source
Coerces objects based on the definitions that are registered.
# File lib/garcon/chef/coerce/coercer.rb, line 35 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 [Class] target
What you want the object to turn in to.
# File lib/garcon/chef/coerce/coercer.rb, line 78 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 Garcon
library.
@param [Class] origin
The class to convert.
@param [Class] target
What the origin will be converted to.
# File lib/garcon/chef/coerce/coercer.rb, line 52 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 [Class] origin
@param [Class] target
# File lib/garcon/chef/coerce/coercer.rb, line 66 def unregister(origin, target) @mutex.synchronize do @coercions[origin].delete(target) end end