Class: CMDx::CoercionRegistry

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry = nil) ⇒ CoercionRegistry

Initialize a new coercion registry.

Examples:

registry = CoercionRegistry.new
registry = CoercionRegistry.new(custom: CustomCoercion)

Parameters:

  • registry (Hash{Symbol => Class}, nil) (defaults to: nil)

    optional initial registry hash



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cmdx/coercion_registry.rb', line 30

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 Attribute Details

#registryHash{Symbol => Class} (readonly) Also known as: to_h

Returns the internal registry mapping coercion types to handler classes.

Examples:

registry.registry # => { integer: Coercions::Integer, boolean: Coercions::Boolean }

Returns:

  • (Hash{Symbol => Class})

    Hash of coercion type names to coercion classes



18
19
20
# File 'lib/cmdx/coercion_registry.rb', line 18

def registry
  @registry
end

Instance Method Details

#coerce(type, task, value, options = {}) ⇒ Object

Coerce a value to the specified type using the registered handler.

Examples:

result = registry.coerce(:integer, task, "42")
result = registry.coerce(:boolean, task, "true", strict: true)

Parameters:

  • type (Symbol)

    the type to coerce to

  • task (Object)

    the task context for the coercion

  • value (Object)

    the value to coerce

  • options (Hash) (defaults to: {})

    additional options for the coercion

Options Hash (options):

  • :* (Object)

    Any coercion option key-value pairs

Returns:

  • (Object)

    the coerced value

Raises:

  • (TypeError)

    when the type is not registered



109
110
111
112
113
# File 'lib/cmdx/coercion_registry.rb', line 109

def coerce(type, task, value, options = {})
  raise TypeError, "unknown coercion type #{type.inspect}" unless registry.key?(type)

  Utils::Call.invoke(task, registry[type], value, options)
end

#deregister(name) ⇒ CoercionRegistry

Remove a coercion handler for a type.

Examples:

registry.deregister(:custom_type)
registry.deregister("another_type")

Parameters:

  • name (Symbol, String)

    the type name to deregister

Returns:



87
88
89
90
# File 'lib/cmdx/coercion_registry.rb', line 87

def deregister(name)
  registry.delete(name.to_sym)
  self
end

#dupCoercionRegistry

Create a duplicate of this registry.

Examples:

new_registry = registry.dup

Returns:



55
56
57
# File 'lib/cmdx/coercion_registry.rb', line 55

def dup
  self.class.new(registry.dup)
end

#register(name, coercion) ⇒ CoercionRegistry

Register a new coercion handler for a type.

Examples:

registry.register(:custom_type, CustomCoercion)
registry.register("another_type", AnotherCoercion)

Parameters:

  • name (Symbol, String)

    the type name to register

  • coercion (Class)

    the coercion class to handle this type

Returns:



71
72
73
74
# File 'lib/cmdx/coercion_registry.rb', line 71

def register(name, coercion)
  registry[name.to_sym] = coercion
  self
end