Class: Yukata::Coercer
- Inherits:
-
Object
- Object
- Yukata::Coercer
- Defined in:
- lib/yukata/coercer.rb
Overview
The class that coerces objects based on the definitions that are registered with it.
Instance Method Summary collapse
- #coerce(object, target) ⇒ Object
-
#initialize ⇒ Coercer
constructor
A new instance of Coercer.
-
#register(origin, target, &block) ⇒ Object
Registers a coercion with the yukata library.
-
#unregister(origin, target) ⇒ Object
Removes a coercion from the library.
Constructor Details
Instance Method Details
#coerce(object, target) ⇒ Object
40 41 42 43 44 |
# 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) ⇒ Object
Registers a coercion with the yukata library
20 21 22 23 24 25 26 |
# 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) ⇒ Object
Removes a coercion from the library
32 33 34 35 36 |
# File 'lib/yukata/coercer.rb', line 32 def unregister(origin, target) @mutex.synchronize do @coercions[origin].delete(target) end end |