Method: Sinclair::Caster::ClassMethods#cast_with

Defined in:
lib/sinclair/caster/class_methods.rb

#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

Overloads:

  • #cast_with(key, method_name) ⇒ Caster

    Examples:

    Casting from pre registered symbol caster

    class MyCaster < Sinclair::Caster
      cast_with(:json, :to_json)
    end
    
    MyCaster.cast({ key: :value }, :json) # returns '{"key":"value"}'
    

    Parameters:

    • key (Symbol)

      key where the caster will be store.

    • method_name (Symbol)

      method to be called on the value that is being converted

  • #cast_with(key, &block) ⇒ Caster

    Examples:

    Casting from pre registered block caster

    MyCaster.cast_with(:complex) do |hash|
      real = hash[:real]
      imaginary = hash[:imaginary]
    
      "#{real.to_f} + #{imaginary.to_f} i"
    end
    
    value = { real: 10, imaginary: 5 }
    
    MyCaster.cast(value, :complex) # returns '10.0 + 5.0 i'
    

    Parameters:

    • key (Symbol)

      key where the caster will be store.

    • block (Proc)

      block to be used when casting the value.

  • #cast_with(class_key, method_name) ⇒ Caster

    Examples:

    Casting from pre registered class

    class MyCaster < Sinclair::Caster
      cast_with(Numeric, :to_i)
    end
    
    MyCaster.cast('10', Integer) # returns 10
    

    Parameters:

    • class_key (Class)

      class to be used as key. This will be used as parent class when the calling Sinclair::Caster.cast.

    • method_name (Symbol)

      method to be called on the value that is being converted.

  • #cast_with(class_key, &block) ⇒ Caster

    Examples:

    Casting from pre registered block caster from a class

    # hash_model.rb
    
    class HashModel
      def initialize(hash)
        hash.each do |attribute, value|
          method_name = "#{attribute}="
    
          send(method_name, value) if respond_to?(method_name)
        end
      end
    end
    
    # hash_person.rb
    class HashPerson < HashModel
      attr_accessor :name, :age
    end
    
    # caster_config.rb
    Caster.cast_with(HashModel) do |value, klass:|
      klass.new(value)
    end
    
    Caster.cast_with(String, &:to_json)
    
    # main.rb
    values = [
      { klass: String, value: { name: 'john', age: 20, country: 'BR' } },
      { klass: HashPerson, value: { name: 'Mary', age: 22, country: 'IT' } }
    ]
    
    values.map! do |config|
      value = config[:value]
      klass = config[:klass]
    
      Caster.cast(value, klass, klass: klass)
    end
    
    values[0] # returns '{"name":"john","age":20,"country":"BR"}'
    values[1] # returns HashPerson.new(name: 'Mary', age: 22)
    

    Parameters:

    • class_key (Class)

      class to be used as key. This will be used as parent class when the calling Sinclair::Caster.cast.

    • block (Proc)

      block to be used when casting the value.

Returns:

  • (Caster)

    the registered caster

See Also:



16
17
18
19
20
21
22
# File 'lib/sinclair/caster/class_methods.rb', line 16

def cast_with(key, method_name = nil, &)
  caster = instance_for(method_name, &)

  return class_casters[key] = caster if key.is_a?(Class)

  casters[key] = caster
end