Commit 7d2f8062 by liyijie

feat: update try_method & try_chain logic

parent 482a9691
......@@ -195,7 +195,7 @@ module TalltyImportExport
records
end
def get_full_key header
def get_full_key(header)
[header.with_indifferent_access[:json], header.with_indifferent_access[:key]].compact.join('.')
end
......@@ -238,42 +238,23 @@ module TalltyImportExport
''
end
def try_chain(record, arr)
arr.reduce(record) do |r, m|
if r.is_a?(Array)
r.try(m) || r.try(:[], m.to_i)
elsif r.is_a?(Hash)
r.try(:[], m) || r.try(:[], m.to_sym)
else
data = begin
r.try(m)
rescue StandardError
nil
end
if data.nil?
data = begin
r.try(:[], m)
rescue StandardError
nil
end
end
if data.nil?
data = begin
r.try(:[], m.to_sym)
rescue StandardError
nil
end
end
data
end
def try_chain(payload, arr)
arr.reduce(payload) do |r, m|
if m =~ /^\d+$/ && r.is_a?(Array) # If m is an integer string and r is an array
r[m.to_i]
else
r.try(m) || r.try(:[], m)
end
rescue StandardError
nil
end
end
def try_method(record, method, prefix: nil)
prefix_arr = prefix.to_s.split(/\./)
arr = method.to_s.split(/\./)
try_chain record, prefix_arr + arr
end
def try_method(payload, method, prefix: nil)
prefix_arr = prefix.to_s.split(/\./)
arr = method.split('.').flat_map { |m| m.include?('[') ? m.split(/\[\s*|\s*\]/).reject(&:empty?) : m }
try_chain record, prefix_arr + arr
end
# 根据数据类型 attr_type 进行数据的格式化
def handle_format(data, header, **opts)
......
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