Skip to content

Commit

Permalink
update exten, localyty, & move flavor attr to ext in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Dec 12, 2024
1 parent 3b7826c commit 96f449a
Show file tree
Hide file tree
Showing 20 changed files with 362 additions and 273 deletions.
7 changes: 6 additions & 1 deletion lib/relaton_bib/bib_item_locality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ def to_xml(builder)

# @returnt [Hash]
def to_hash
{ "locality_stack" => single_element_array(locality) }
hash = Hash.new { |h, k| h[k] = [] }
locality.each_with_object(hash) do |l, obj|
k, v = l.to_hash.first
obj[k] << v
end
{ "locality_stack" => hash }
end

#
Expand Down
37 changes: 20 additions & 17 deletions lib/relaton_bib/bibliographic_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require "relaton_bib/validity"
require "relaton_bib/document_relation"
require "relaton_bib/bib_item_locality"
require_relative "extent"
require "relaton_bib/xml_parser"
require "relaton_bib/bibtex_parser"
require "relaton_bib/biblio_note"
Expand Down Expand Up @@ -105,7 +106,7 @@ class BibliographicItem
# @return [Array<RelatonBib::Place>]
attr_reader :place

# @return [Array<RelatonBib::Locality, RelatonBib::LocalityStack>]
# @return [Array<RelatonBib::Extent>]
attr_reader :extent

# @return [Array<Strig>]
Expand Down Expand Up @@ -154,7 +155,7 @@ class BibliographicItem
# @param series [Array<RelatonBib::Series>]
# @param medium [RelatonBib::Medium, nil]
# @param place [Array<String, RelatonBib::Place>]
# @param extent [Array<RelatonBib::Locality, RelatonBib::LocalityStack>]
# @param extent [Array<RelatonBib::Extent>]
# @param accesslocation [Array<String>]
# @param classification [Array<RelatonBib::Classification>]
# @param validity [RelatonBib:Validity, nil]
Expand Down Expand Up @@ -417,7 +418,7 @@ def to_hash(embedded: false) # rubocop:disable Metrics/AbcSize, Metrics/Cyclomat
hash["series"] = single_element_array(series) if series&.any?
hash["medium"] = medium.to_hash if medium
hash["place"] = single_element_array(place) if place&.any?
hash["extent"] = single_element_array(extent) if extent&.any?
hash["extent"] = extent.map(&:to_hash) if extent&.any?
hash["size"] = size.to_hash if size&.any?
if accesslocation&.any?
hash["accesslocation"] = single_element_array(accesslocation)
Expand All @@ -429,16 +430,15 @@ def to_hash(embedded: false) # rubocop:disable Metrics/AbcSize, Metrics/Cyclomat
hash["fetched"] = fetched.to_s if fetched
hash["keyword"] = single_element_array(keyword) if keyword&.any?
hash["license"] = single_element_array(license) if license&.any?
hash["doctype"] = doctype.to_hash if doctype
hash["subdoctype"] = subdoctype if subdoctype
if editorialgroup&.presence?
hash["editorialgroup"] = editorialgroup.to_hash
if has_ext?
hash["ext"] = {}
hash["ext"]["schema-version"] = ext_schema if !embedded && respond_to?(:ext_schema) && ext_schema
hash["ext"]["doctype"] = doctype.to_hash if doctype
hash["ext"]["subdoctype"] = subdoctype if subdoctype
hash["ext"]["editorialgroup"] = editorialgroup.to_hash if editorialgroup&.presence?
hash["ext"]["ics"] = single_element_array ics if ics.any?
hash["ext"]["structuredidentifier"] = structuredidentifier.to_hash if structuredidentifier&.presence?
end
hash["ics"] = single_element_array ics if ics.any?
if structuredidentifier&.presence?
hash["structuredidentifier"] = structuredidentifier.to_hash
end
hash["ext"] = { "schema-version" => ext_schema } if !embedded && respond_to?(:ext_schema) && ext_schema
hash
end

Expand Down Expand Up @@ -572,7 +572,7 @@ def to_asciibib(prefix = "") # rubocop:disable Metrics/AbcSize, Metrics/Cyclomat
link.each { |l| out += l.to_asciibib prefix, link.size }
out += medium.to_asciibib prefix if medium
place.each { |pl| out += pl.to_asciibib prefix, place.size }
extent.each { |ex| out += ex.to_asciibib "#{pref}extent", extent.size }
extent.each { |ex| out += ex.to_asciibib prefix, extent.size }
out += size.to_asciibib pref if size
accesslocation.each { |al| out += "#{pref}accesslocation:: #{al}\n" }
classification.each do |cl|
Expand Down Expand Up @@ -634,7 +634,7 @@ def render_xml(**opts) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Me
series.each { |s| s.to_xml builder }
medium&.to_xml builder
place.each { |pl| pl.to_xml builder }
extent.each { |e| builder.extent { e.to_xml builder } }
extent.each { |e| e.to_xml builder }
size&.to_xml builder
accesslocation.each { |al| builder.accesslocation al }
license.each { |l| builder.license l }
Expand All @@ -644,10 +644,9 @@ def render_xml(**opts) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Me
kwrd.each { |kw| builder.keyword { kw.to_xml(builder) } }
validity&.to_xml builder
if block_given? then yield builder
elsif opts[:bibdata] && (doctype || editorialgroup || ics&.any? ||
structuredidentifier&.presence?)
elsif opts[:bibdata] && has_ext?
ext = builder.ext do |b|
doctype.to_xml b if doctype
doctype&.to_xml b
b.subdoctype subdoctype if subdoctype
editorialgroup&.to_xml b
ics.each { |i| i.to_xml b }
Expand All @@ -661,5 +660,9 @@ def render_xml(**opts) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Me
xml["schema-version"] = schema unless opts[:embedded]
xml
end

def has_ext? # rubocop:disable Metrics/CyclomaticComplexity
doctype || subdoctype || editorialgroup || ics&.any? || structuredidentifier&.presence?
end
end
end
7 changes: 4 additions & 3 deletions lib/relaton_bib/bibtex_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ def fetch_relation(bibtex)
end

# @param bibtex [BibTeX::Entry]
# @return [Array<RelatonBib::BibItemLocality>]
# @return [Array<RelatonBib::Extent>]
def fetch_extent(bibtex) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
bibtex.select do |k, _v|
locs = bibtex.select do |k, _v|
%i[chapter pages volume].include? k
end.reduce([]) do |mem, loc|
if loc[0] == :pages
Expand All @@ -182,8 +182,9 @@ def fetch_extent(bibtex) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
from = loc[1].to_s
to = nil
end
mem << BibItemLocality.new(type, from, to)
mem << Locality.new(type, from, to)
end
[RelatonBib::Extent.new(locs)]
end

# @param bibtex [BibTeX::Entry]
Expand Down
21 changes: 11 additions & 10 deletions lib/relaton_bib/document_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,22 @@ class DocumentRelation
# @param description [RelatonBib::FormattedString, nil]
# @param bibitem [RelatonBib::BibliographicItem,
# RelatonIso::IsoBibliographicItem]
# @param locality [Array<RelatonBib::Locality, RelatonBib::LocalityStack>]
# @param locality [Array<RelatonBib::Locality>]
# @param locality_stack [Array<RelatonBib::LocalityStack>]
# @param source_locality [Array<RelatonBib::SourceLocality,
# RelatonBib::SourceLocalityStack>]
def initialize(type:, bibitem:, description: nil, locality: [],
source_locality: [])
def initialize(type:, bibitem:, **args)
type = "obsoletes" if type == "Now withdrawn"
unless self.class::TYPES.include? type
Util.warn "Invalid relation type: `#{type}`"
end
@type = type
@description = description
@locality = locality
@source_locality = source_locality
@description = args[:description]
@locality = args[:locality] || args[:locality_stack] || []
@source_locality = args[:source_locality] || args[:source_locality_stack] || []
@bibitem = bibitem
end

# rubocop:disable Metrics/AbcSize

# @param builder [Nokogiri::XML::Builder]
def to_xml(builder, **opts)
opts.delete :bibdata
Expand All @@ -68,13 +66,16 @@ def to_xml(builder, **opts)
source_locality.each { |l| l.to_xml builder }
end
end
# rubocop:enable Metrics/AbcSize

# @return [Hash]
def to_hash # rubocop:disable Metrics/AbcSize
hash = { "type" => type, "bibitem" => bibitem.to_hash(embedded: true) }
hash["description"] = description.to_hash if description
hash["locality"] = single_element_array(locality) if locality&.any?
locality.each_with_object(hash) do |l, obj|
k, v = l.to_hash.first
hash[k] ||= []
hash[k] << v
end
if source_locality&.any?
hash["source_locality"] = single_element_array(source_locality)
end
Expand Down
8 changes: 3 additions & 5 deletions lib/relaton_bib/document_relation_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ class DocRelationCollection
# @option relation [String] :type
# @option relation [String] :identifier
# @option relation [String, NIllClass] :url (nil)
# @option relation [Array<RelatonBib::Locality,
# RelatonBib::LocalityStack>] :locality
# @option relation [Array<RelatonBib::SourceLocality,
# RelatonBib::SourceLocalityStack>] :source_locality
# @option relation [RelatonBib::BibliographicItem, NillClass] :bibitem (nil)
# @option relation [Array<RelatonBib::Locality, RelatonBib::LocalityStack>] :locality
# @option relation [Array<RelatonBib::SourceLocality, RelatonBib::SourceLocalityStack>] :source_locality
# @option relation [RelatonBib::BibliographicItem, nil] :bibitem (nil)
def initialize(relation)
@array = relation.map { |r| r.is_a?(Hash) ? DocumentRelation.new(**r) : r }
end
Expand Down
39 changes: 39 additions & 0 deletions lib/relaton_bib/extent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module RelatonBib
class Extent
attr_accessor :locality

#
# @param [Array<RelatonBib::Locality, RelatonBib::LocalityStack>] locality
#
def initialize(locality)
@locality = locality
end

def to_xml(builder)
builder.extent do |b|
locality.each { |l| l.to_xml(b) }
end
end

def to_hash
hash = Hash.new { |h, k| h[k] = [] }
locality.each_with_object(hash) do |l, obj|
k, v = l.to_hash.first
obj[k] << v
end
end

def to_asciibib(prefix = "", count = 1)
pref = prefix.empty? ? "extent" : "#{prefix}.extent"
out = count > 1 ? "#{pref}::\n" : ""
locality.each do |l|
out += l.to_asciibib(pref, locality.size)
end
out
end

def to_bibtex(item)
locality.map { |l| l.to_bibtex(item) }.join
end
end
end
Loading

0 comments on commit 96f449a

Please sign in to comment.