Class: Sinclair::Caster
- Extended by:
- ClassMethods
- Defined in:
- lib/sinclair/caster.rb,
lib/sinclair/caster/class_methods.rb
Overview
Class responsible for defining how to and casting values
First the class needs to be configured using Caster.cast_with and later a value can be cast by using Caster.cast or Caster.caster_for
Inheritance grants the hability to have different casting for different purposes / applications / gems
Direct Known Subclasses
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.cast ⇒ Object
Cast a value using the registered caster.
-
.cast_with ⇒ Caster
Register a caster under a key.
-
.caster_for ⇒ Caster
Returns an instance of caster for the provided key.
-
.master_caster! ⇒ TrueClass
Changes the class to be the master caster.
Instance Method Summary collapse
-
#cast(value, **opts) ⇒ Object
Cast a value using the given the set
block
. -
#initialize(&block) ⇒ Caster
constructor
A new instance of Caster.
Methods included from ClassMethods
cast_with, caster_for, master_caster!
Constructor Details
#initialize(&block) ⇒ Caster
Returns a new instance of Caster.
319 320 321 |
# File 'lib/sinclair/caster.rb', line 319 def initialize(&block) @block = block&.to_proc end |
Class Method Details
.cast(value, key, **opts) ⇒ Object .cast(value, class_key, **opts) ⇒ Object
Cast a value using the registered caster
|
# File 'lib/sinclair/caster.rb', line 142
|
.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
|
# File 'lib/sinclair/caster.rb', line 44
|
.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
|
# File 'lib/sinclair/caster.rb', line 240
|
.master_caster! ⇒ TrueClass
Changes the class to be the master caster
The master caster never checks with its an
|
# File 'lib/sinclair/caster.rb', line 19
|
Instance Method Details
#cast(value, **opts) ⇒ Object
Cast a value using the given the set block
When the block does not accept options, those are not passed
350 351 352 353 354 355 356 357 358 |
# File 'lib/sinclair/caster.rb', line 350 def cast(value, **opts) return value unless block = opts.select do |key, _| .include?(key) end block.call(value, **) end |