Class: Metanorma::Standoc::Cleanup::MergeBibitems
- Inherits:
-
Object
- Object
- Metanorma::Standoc::Cleanup::MergeBibitems
- Defined in:
- lib/metanorma/standoc/merge_bibitems.rb
Instance Method Summary collapse
- #array_to_hash(array, attributes) ⇒ Object
-
#initialize(old, new) ⇒ MergeBibitems
constructor
A new instance of MergeBibitems.
- #load_bibitem(item) ⇒ Object
- #merge ⇒ Object
- #merge1(old, new) ⇒ Object
- #merge_by_type(old, new, field, attributes, opt = {}) ⇒ Object
- #merge_by_type1(old, new, field, attributes, opt) ⇒ Object
- #merge_contributor(old, new) ⇒ Object
-
#merge_extent(old, new) ⇒ Object
ensure return value goes into extent.
- #merge_relations(old, new) ⇒ Object
- #merge_simple(old, new, field) ⇒ Object
- #to_noko ⇒ Object
Constructor Details
#initialize(old, new) ⇒ MergeBibitems
Returns a new instance of MergeBibitems.
16 17 18 19 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 16 def initialize(old, new) @old = load_bibitem(old) @new = load_bibitem(new) end |
Instance Method Details
#array_to_hash(array, attributes) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 96 def array_to_hash(array, attributes) array.each_with_object({}) do |k, m| m[k.dig(*Array(attributes))] ||= [] m[k.dig(*Array(attributes))] << k end end |
#load_bibitem(item) ⇒ Object
21 22 23 24 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 21 def load_bibitem(item) ret = RelatonBib::XMLParser.from_xml(item) ret.to_hash.symbolize_all_keys end |
#merge ⇒ Object
31 32 33 34 35 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 31 def merge @old.delete(:formattedref) merge1(@old, @new) self end |
#merge1(old, new) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 37 def merge1(old, new) %i(link docid date title series biblionote).each do |k| merge_by_type(old, new, k, :type) end merge_extent(old, new) merge_contributor(old, new) %i(place version edition).each do |k| merge_simple(old, new, k) end merge_relations(old, new) end |
#merge_by_type(old, new, field, attributes, opt = {}) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 79 def merge_by_type(old, new, field, attributes, opt = {}) new.nil? || new[field].nil? || new[field].empty? and return old.nil? and return new[field] !old[field].is_a?(::Array) || old[field].empty? and return old[field] = new[field] old[field] = merge_by_type1(old, new, field, attributes, opt) end |
#merge_by_type1(old, new, field, attributes, opt) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 87 def merge_by_type1(old, new, field, attributes, opt) old1 = array_to_hash(old[field], attributes) new1 = array_to_hash(new[field], attributes) out = opt[:recurse] ? old1.deep_merge(new1) : old1.merge(new1) out.each_value.with_object([]) do |v, m| v.each { |v1| m << v1 } end end |
#merge_contributor(old, new) ⇒ Object
69 70 71 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 69 def merge_contributor(old, new) merge_by_type(old, new, :contributor, [:role, 0, :type]) end |
#merge_extent(old, new) ⇒ Object
ensure return value goes into extent
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 55 def merge_extent(old, new) old.dig(:extent, 0, :locality) and old[:extent] = [{ locality_stack: old[:extent] }] new.dig(:extent, 0, :locality) and new[:extent] = [{ locality_stack: new[:extent] }] ret = merge_by_type(old.dig(:extent, 0), new.dig(:extent, 0), :locality_stack, %i[locality type]) (ret && !old.dig(:extent, 0)) or return old[:extent] ||= [] old[:extent][0] ||= {} old[:extent][0][:locality_stack] = ret end |
#merge_relations(old, new) ⇒ Object
73 74 75 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 73 def merge_relations(old, new) merge_by_type(old, new, :relation, :type, recurse: true) end |
#merge_simple(old, new, field) ⇒ Object
49 50 51 52 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 49 def merge_simple(old, new, field) (new[field].nil? || new[field].empty?) and return old[field] = new[field] end |
#to_noko ⇒ Object
26 27 28 29 |
# File 'lib/metanorma/standoc/merge_bibitems.rb', line 26 def to_noko out = RelatonBib::HashConverter.hash_to_bib(@old) Nokogiri::XML(RelatonBib::BibliographicItem.new(**out).to_xml).root end |