2016-03-18 22:14:41 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
module ObjectTraverser
|
|
|
|
def extract_value(keys, value)
|
|
|
|
key = keys.shift
|
2016-03-19 17:50:32 +00:00
|
|
|
return nil if key.nil? || value.nil?
|
2016-03-18 22:14:41 +00:00
|
|
|
|
|
|
|
if value.is_a?(Array)
|
2017-03-14 16:33:54 +00:00
|
|
|
value = if key.is_a?(Fixnum)
|
|
|
|
value[key]
|
|
|
|
elsif value.respond_to?(key.to_sym)
|
|
|
|
value.send(key.to_sym)
|
|
|
|
end
|
2016-03-18 22:14:41 +00:00
|
|
|
else
|
|
|
|
value = value[key.to_s].nil? ? nil : value[key.to_s]
|
|
|
|
end
|
|
|
|
|
|
|
|
# if there are no more keys, just return the value
|
|
|
|
return value if keys.first.nil?
|
|
|
|
# if there are more keys, extract more
|
|
|
|
extract_value(keys.clone, value)
|
|
|
|
end
|
|
|
|
end
|