module Mspire::Mascot::Dat::Castable

Public Instance Methods

cast!(cast_hash=nil) click to toggle source

expects a hash with the parameter and the way to cast it as a symbol (e.g., :to_f or a lambda). If no hash given, will attempt to retrieve a class constant ‘CAST’ which defines the casts.

# File lib/mspire/mascot/dat/cast.rb, line 16
def cast!(cast_hash=nil)
  hash = cast_hash || self.class.const_get('CAST')
  self.each_pair do |k,v|
    if cast=hash[k]
      apply = cast.is_a?(Symbol) ? cast.to_proc : cast
      self[k] = apply[v] if apply
    end
  end
  self
end