lib/hashify.rb in hashify-0.0.2 vs lib/hashify.rb in hashify-0.0.3

- old
+ new

@@ -23,13 +23,21 @@ def self.from_hash(map) convert_map = assembled_hash_convert instance = new map.each do |name, val| - convert_map[name.to_sym] ? - instance.send((name.to_s + '=').to_sym, convert_map[name.to_sym].last.call(val)) : + if converter = convert_map[name.to_sym] + converted_val = case converter.last.arity + when 1: convert_map[name.to_sym].last.call(val) + when 2: convert_map[name.to_sym].last.call(val, instance) + else + raise 'arity must be 1 or 2' + end + instance.send((name.to_s + '=').to_sym, converted_val) + else instance.send((name.to_s + '=').to_sym, val) + end end instance end def self.assembled_hash_convert @@ -44,12 +52,19 @@ CLASS_DOC end def to_hash self.class.assembled_hash_convert.inject({}) do |hash, (name, converter)| - converter ? - hash[name] = converter.first.call(self.send(name)) : + if converter + hash[name] = case converter.first.arity + when 1: converter.first.call(self.send(name)) + when 2: converter.first.call(self.send(name), self) + else + raise 'arity must be 1 or 2' + end + else hash[name] = self.send(name) + end hash end end end