Commit 7d2f8062 by liyijie

feat: update try_method & try_chain logic

parent 482a9691
...@@ -195,7 +195,7 @@ module TalltyImportExport ...@@ -195,7 +195,7 @@ module TalltyImportExport
records records
end end
def get_full_key header def get_full_key(header)
[header.with_indifferent_access[:json], header.with_indifferent_access[:key]].compact.join('.') [header.with_indifferent_access[:json], header.with_indifferent_access[:key]].compact.join('.')
end end
...@@ -238,40 +238,21 @@ module TalltyImportExport ...@@ -238,40 +238,21 @@ module TalltyImportExport
'' ''
end end
def try_chain(record, arr) def try_chain(payload, arr)
arr.reduce(record) do |r, m| arr.reduce(payload) do |r, m|
if r.is_a?(Array) if m =~ /^\d+$/ && r.is_a?(Array) # If m is an integer string and r is an array
r.try(m) || r.try(:[], m.to_i) r[m.to_i]
elsif r.is_a?(Hash)
r.try(:[], m) || r.try(:[], m.to_sym)
else else
data = begin r.try(m) || r.try(:[], m)
r.try(m)
rescue StandardError
nil
end
if data.nil?
data = begin
r.try(:[], m)
rescue StandardError
nil
end end
end
if data.nil?
data = begin
r.try(:[], m.to_sym)
rescue StandardError rescue StandardError
nil nil
end end
end end
data
end
end
end
def try_method(record, method, prefix: nil) def try_method(payload, method, prefix: nil)
prefix_arr = prefix.to_s.split(/\./) prefix_arr = prefix.to_s.split(/\./)
arr = method.to_s.split(/\./) arr = method.split('.').flat_map { |m| m.include?('[') ? m.split(/\[\s*|\s*\]/).reject(&:empty?) : m }
try_chain record, prefix_arr + arr try_chain record, prefix_arr + arr
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment