Class: Statinize::Caster

Inherits:
Object show all
Defined in:
lib/statinize/caster.rb

Constant Summary collapse

TYPE_CASTINGS =
{
  Integer => :to_i,
  String => :to_s,
  Float => :to_f,
  Complex => :to_c,
  Rational => :to_r,
  Symbol => :to_sym,
  Enumerator => :to_enum,
  Proc => :to_proc,
  Array => :to_a,
  BigDecimal => :to_d,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(instance, attr, option) ⇒ Caster

Returns a new instance of Caster.



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

def initialize(instance, attr, option)
  @instance = instance
  @attr_name = attr.name
  @attr_value = instance.public_send(attr_name)
  @validator_value = option[:type]
end

Instance Method Details

#castObject



23
24
25
26
27
28
# File 'lib/statinize/caster.rb', line 23

def cast
  return false unless castable?

  instance.public_send("#{attr_name}=", casted)
  true
end