module EasyMapper
Constants
- VERSION
Attributes
after[RW]
output[RW]
target_map[RW]
Public Class Methods
extended(base)
click to toggle source
# File lib/EasyMapper.rb, line 4 def self.extended(base) base.class_eval do self.target_map = nil end end
Public Instance Methods
after_normalize(&block)
click to toggle source
# File lib/EasyMapper.rb, line 14 def after_normalize(&block) self.after = block end
find_input_value(obj, key)
click to toggle source
# File lib/EasyMapper.rb, line 46 def find_input_value(obj, key) return recurse_dig(key.split('#'), obj) if key.split('#').count > 1 if obj.respond_to?(:key?) && obj.key?(key) obj[key] elsif obj.respond_to?(:each) r = nil obj.find{ |*a| r = find_input_value(a.last, key) } r end end
handle_array(input, target_map, key, output)
click to toggle source
# File lib/EasyMapper.rb, line 42 def handle_array(input, target_map, key, output) output[key] = input.map {|i| normalize_hash(target_map, i)} end
map(input_map)
click to toggle source
# File lib/EasyMapper.rb, line 10 def map(input_map) self.target_map = input_map end
normalize(input_hash)
click to toggle source
# File lib/EasyMapper.rb, line 18 def normalize(input_hash) output = normalize_hash(self.target_map, input_hash) output = after.call(input_hash, output) if after output end
normalize_hash(target_map, input, buried_keys = [], output = {})
click to toggle source
# File lib/EasyMapper.rb, line 24 def normalize_hash(target_map, input, buried_keys = [], output = {}) target_map.keys.each do |key| if key.include?('*') keys = key.split('*') handle_array(input[keys[1]], target_map[key], keys[0], output) else buried_keys.push(key) if target_map[key].is_a?(Hash) normalize_hash(target_map[key], input, buried_keys, output) else output.bury(buried_keys, find_input_value(input, target_map[key])) end buried_keys = [] end end output end
recurse_dig(keys, obj)
click to toggle source
# File lib/EasyMapper.rb, line 57 def recurse_dig(keys, obj) if keys.count > 1 key = keys.shift recurse_dig(keys, obj.dig(key)) else return obj.dig(keys.shift) end end