class HashMap::Mapper
Attributes
hash_map[R]
original[R]
Public Class Methods
new(original, hash_map)
click to toggle source
# File lib/hash_map/mapper.rb, line 4 def initialize(original, hash_map) @original = Fusu::HashWithIndifferentAccess.new(original) @hash_map = hash_map end
Public Instance Methods
output()
click to toggle source
# File lib/hash_map/mapper.rb, line 9 def output new_hash = Fusu::HashWithIndifferentAccess.new hash_map.class.attributes.each do |struc| value = get_value(struc) Fusu::Hash.deep_merge!(new_hash, build_keys(struc[:key], value)) end new_hash end
Private Instance Methods
after_each_callbacks()
click to toggle source
# File lib/hash_map/mapper.rb, line 39 def after_each_callbacks hash_map.class.dsl.after_each rescue [] end
after_each_middleware(value, struct)
click to toggle source
# File lib/hash_map/mapper.rb, line 32 def after_each_middleware(value, struct) contx = AfterEachContext.new(original, struct, value) after_each_callbacks.inject(value) do |output, middle| middle.call(output, contx) end end
build_keys(ary, value)
click to toggle source
# File lib/hash_map/mapper.rb, line 70 def build_keys(ary, value) ary.reverse.inject(value) do |a, n| Fusu::HashWithIndifferentAccess.new(n => a) end end
execute_block(struct)
click to toggle source
# File lib/hash_map/mapper.rb, line 59 def execute_block(struct) block = struct[:proc] value = if struct[:from_child] nested = get_value_from_key(struct, :from_child) hash_map.instance_exec nested, original, &block else hash_map.instance_exec original, original, &block end after_each_middleware(value, struct) end
get_value(struct)
click to toggle source
# File lib/hash_map/mapper.rb, line 20 def get_value(struct) value = if struct[:is_collection] map_collection(struct) elsif struct[:proc] execute_block(struct) elsif struct[:from] get_value_from_key(struct) end nil_to_default(value, struct) end
get_value_from_key(struct, from = :from)
click to toggle source
# File lib/hash_map/mapper.rb, line 51 def get_value_from_key(struct, from = :from) value = struct[from].inject(original) do |output, k| break unless output.respond_to?(:[]) output.send(:[], k) end after_each_middleware(value, struct) end
map_collection(struct)
click to toggle source
# File lib/hash_map/mapper.rb, line 45 def map_collection(struct) value = get_value_from_key(struct) value = Fusu::Array.wrap(value) value.map { |elem| struct[:mapper].call(elem, hash_map.options) } end
nil_to_default(value, struct)
click to toggle source
# File lib/hash_map/mapper.rb, line 76 def nil_to_default(value, struct) value.nil? ? struct[:default] : value end