Class: CMDx::CoercionRegistry
- Inherits:
-
Object
- Object
- CMDx::CoercionRegistry
- Defined in:
- lib/cmdx/coercion_registry.rb
Overview
Registry for managing type coercion handlers.
Provides a centralized way to register, deregister, and execute type coercions for various data types including arrays, numbers, dates, and other primitives.
Supports copy-on-write semantics: a duped registry shares the parent’s data until a write operation triggers materialization.
Instance Method Summary collapse
-
#coerce(type, task, value, options = EMPTY_HASH) ⇒ Object
Coerce a value to the specified type using the registered handler.
-
#deregister(name) ⇒ CoercionRegistry
Remove a coercion handler for a type.
-
#initialize(registry = nil) ⇒ CoercionRegistry
constructor
Initialize a new coercion registry.
-
#initialize_dup(source) ⇒ Object
Sets up copy-on-write state when duplicated via dup.
-
#register(name, coercion) ⇒ CoercionRegistry
Register a new coercion handler for a type.
-
#registry ⇒ Hash{Symbol => Class}
(also: #to_h)
Returns the internal registry mapping coercion types to handler classes.
Constructor Details
#initialize(registry = nil) ⇒ CoercionRegistry
Initialize a new coercion registry.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cmdx/coercion_registry.rb', line 22 def initialize(registry = nil) @registry = registry || { array: Coercions::Array, big_decimal: Coercions::BigDecimal, boolean: Coercions::Boolean, complex: Coercions::Complex, date: Coercions::Date, datetime: Coercions::DateTime, float: Coercions::Float, hash: Coercions::Hash, integer: Coercions::Integer, rational: Coercions::Rational, string: Coercions::String, time: Coercions::Time } end |
Instance Method Details
#coerce(type, task, value, options = EMPTY_HASH) ⇒ Object
Coerce a value to the specified type using the registered handler.
118 119 120 121 122 |
# File 'lib/cmdx/coercion_registry.rb', line 118 def coerce(type, task, value, = EMPTY_HASH) raise TypeError, "unknown coercion type #{type.inspect}" unless registry.key?(type) Utils::Call.invoke(task, registry[type], value, ) end |
#deregister(name) ⇒ CoercionRegistry
Remove a coercion handler for a type.
94 95 96 97 98 99 |
# File 'lib/cmdx/coercion_registry.rb', line 94 def deregister(name) materialize! @registry.delete(name.to_sym) self end |
#initialize_dup(source) ⇒ Object
Sets up copy-on-write state when duplicated via dup.
44 45 46 47 48 |
# File 'lib/cmdx/coercion_registry.rb', line 44 def initialize_dup(source) @parent = source @registry = nil super end |
#register(name, coercion) ⇒ CoercionRegistry
Register a new coercion handler for a type.
76 77 78 79 80 81 |
# File 'lib/cmdx/coercion_registry.rb', line 76 def register(name, coercion) materialize! @registry[name.to_sym] = coercion self end |
#registry ⇒ Hash{Symbol => Class} Also known as: to_h
Returns the internal registry mapping coercion types to handler classes. Delegates to the parent registry when not yet materialized.
59 60 61 |
# File 'lib/cmdx/coercion_registry.rb', line 59 def registry @registry || @parent.registry end |