Module: Sinclair::Caster::ClassMethods
- Included in:
- Sinclair::Caster
- Defined in:
- lib/sinclair/caster/class_methods.rb
Overview
Class methods for Sinclair::Caster
Instance Method Summary collapse
-
#cast(value, key, **opts) ⇒ Object
Cast a value using the registered caster.
-
#cast_with(key, method_name = nil, &block) ⇒ Caster
Register a caster under a key.
-
#caster_for(key) ⇒ Caster
Returns an instance of caster for the provided key.
-
#master_caster! ⇒ TrueClass
Changes the class to be the master caster.
Instance Method Details
#cast(value, key, **opts) ⇒ Object #cast(value, class_key, **opts) ⇒ Object
Cast a value using the registered caster
25 26 27 |
# File 'lib/sinclair/caster/class_methods.rb', line 25 def cast(value, key, **opts) caster_for(key).cast(value, **opts) end |
#cast_with(key, method_name) ⇒ Caster #cast_with(key, &block) ⇒ Caster #cast_with(class_key, method_name) ⇒ Caster #cast_with(class_key, &block) ⇒ Caster
Register a caster under a key
16 17 18 19 20 21 22 |
# File 'lib/sinclair/caster/class_methods.rb', line 16 def cast_with(key, method_name = nil, &block) caster = instance_for(method_name, &block) return class_casters[key] = caster if key.is_a?(Class) casters[key] = caster end |
#caster_for(key) ⇒ Caster #caster_for(class_key) ⇒ Caster
Returns an instance of caster for the provided key
When no registered caster is found one is requested for the parent class. If no caster is found, then a default caster is returned
The default caster performs no casting returning the value itself
30 31 32 33 34 |
# File 'lib/sinclair/caster/class_methods.rb', line 30 def caster_for(key) return casters[key] if casters.key?(key) caster_for_class(key) || superclas_caster_for(key) || Sinclair::Caster.default end |
#master_caster! ⇒ TrueClass
Changes the class to be the master caster
The master caster never checks with its an
11 12 13 |
# File 'lib/sinclair/caster/class_methods.rb', line 11 def master_caster! @master_caster = true end |