2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
module GetData
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.chain(obj, *methods)
|
|
|
|
methods.each do |m|
|
|
|
|
if m.is_a?(Array)
|
|
|
|
params = m[1..-1]
|
|
|
|
m = m[0]
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
if !obj.nil? && obj.respond_to?(m)
|
|
|
|
obj = obj.send(m, *params)
|
|
|
|
elsif obj.respond_to?(:has_key?) && obj.key?(m)
|
|
|
|
obj = obj[m]
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
obj
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
def self.hash(h, *keys)
|
2019-07-30 21:29:24 +00:00
|
|
|
keys.each do |k|
|
|
|
|
if h.key?(k)
|
|
|
|
h = h[k]
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
h
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|