Class: Croesus::Coercer
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 Croesus library.
-
#unregister(origin, target) ⇒ Object
Removes a coercion from the library.
Constructor Details
Instance Method Details
#coerce(object, target) ⇒ Object
57 58 59 60 61 |
# File 'lib/croesus/coerce.rb', line 57 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 Croesus library
37 38 39 40 41 42 43 |
# File 'lib/croesus/coerce.rb', line 37 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
49 50 51 52 53 |
# File 'lib/croesus/coerce.rb', line 49 def unregister(origin, target) @mutex.synchronize do @coercions[origin].delete(target) end end |