作者:董白菜 | 来源:互联网 | 2023-10-13 12:06
篇首语:本文由编程笔记#小编为大家整理,主要介绍了ruby哈希差异到HTMLhttps://github.com/liufengyun/hashdiff相关的知识,希望对你有一定的参考价值。
篇首语:本文由编程笔记#小编为大家整理,主要介绍了ruby 哈希差异到HTML https://github.com/liufengyun/hashdiff相关的知识,希望对你有一定的参考价值。
def parse_diff(changes)
# [["~", "name", "original name", "updated name"]] => {:name => ['original_name', 'updated_name', 'Modify']}
parsed_diff = {}
changes.each do |change|
unless change[1].match(/updated_at/) || change[1].match(/.id/) || change[1].match(/.created_at/)
if change[1].match(/line_items/) || change[1].match(/site_lists/) || change[1].match(/zip_code_lists/)
change[1].gsub!(/\d+/, Random.rand(1000).to_s)
end
type = if change[0] == "+"
"Add"
elsif change[0] == "-"
"Remove"
else
"Modify"
end
parsed_diff[change[1]] = [change[2], change[3], type]
end
end
styled_parsed_diff = ""
parsed_diff.each do |key, value|
nested_items = ""
if value.first.is_a?(Hash)
nested_items <<"#{value.last} #{key.titleize} - "
value.first.each do |k, v|
unless !v.present? || k.match(/created_at/) || k.match(/id/) || k.match(/updated_at/)
nested_items <<"#{k} : #{v}; "
end
end
nested_items <<"
"
else
nested_items <<"- #{value.last} #{key.titleize}: #{value.first} - #{value[1]}
"
end
styled_parsed_diff < end
styled_parsed_diff <<"
"
styled_parsed_diff
end