class TenderHash::Rule

Public Class Methods

new(old_key, new_key=nil, options={}) click to toggle source
# File lib/tender_hash/rule.rb, line 4
def initialize(old_key, new_key=nil, options={})
  @old_key = old_key
  @new_key = new_key || old_key
  @caster  = Caster.for(options[:cast_to])
  @default = options[:default]
end

Public Instance Methods

apply(old_hash, new_hash) click to toggle source
# File lib/tender_hash/rule.rb, line 11
def apply(old_hash, new_hash)
  new_value = cast_value(old_hash[@old_key])
  new_hash[@new_key] = new_value.nil? ? @default : new_value
end

Private Instance Methods

cast_value(val) click to toggle source
# File lib/tender_hash/rule.rb, line 18
def cast_value(val)
  @caster.call(val) unless val.nil?
end