Skip to content

Commit

Permalink
WIP create Bibitem::Model classes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Dec 31, 2024
1 parent b8cce92 commit 511cf82
Show file tree
Hide file tree
Showing 126 changed files with 1,259 additions and 955 deletions.
2 changes: 2 additions & 0 deletions lib/relaton/bib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "forwardable"
require "yaml"
require "htmlentities"
require "bibtex"
require "iso639"
# require_relative "deep_dup"
require_relative "bib/config"
require_relative "bib/util"
Expand Down
20 changes: 10 additions & 10 deletions lib/relaton/bib/bib_item_locality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class BibItemLocality
# @param type [String]
# @param referenceFrom [String]
# @param referenceTo [String, nil]
def initialize(type:, reference_from:, reference_to: nil)
type_ptrn = %r{section|clause|part|paragraph|chapter|page|title|line|
whole|table|annex|figure|note|list|example|volume|issue|time|anchor|
locality:[a-zA-Z0-9_]+}x
unless type&.match? type_ptrn
Util.warn "Invalid locality type: `#{type}`"
end
def initialize(**args)
# type_ptrn = %r{section|clause|part|paragraph|chapter|page|title|line|
# whole|table|annex|figure|note|list|example|volume|issue|time|anchor|
# locality:[a-zA-Z0-9_]+}x
# unless type&.match? type_ptrn
# Util.warn "Invalid locality type: `#{type}`"
# end

@type = type
@reference_from = reference_from
@reference_to = reference_to
@type = args[:type]
@reference_from = args[:reference_from]
@reference_to = args[:reference_to]
end

# @param builder [Nokogiri::XML::Builder]
Expand Down
14 changes: 7 additions & 7 deletions lib/relaton/bib/contributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Role
distributor realizer owner authorizer enabler subject].freeze

# @return [Array<Relaton::Bib::FormattedString>]
attr_reader :description
attr_accessor :description

# @return [Strig]
attr_reader :type
attr_accessor :type

# @param type [String] allowed types "author", "editor",
# "cartographer", "publisher"
Expand Down Expand Up @@ -72,25 +72,25 @@ def to_asciibib(prefix = "", count = 1)

# @param entity [Relaton::Bib::ContributionInfo]
# @param role [Array<Relaton::Bib::Contributor::Role>]
def initialize(entity:, role: [])
@entity = entity
@role = role
def initialize(**args)
@entity = args[:entity]
@role = args[:role] || []
end

def person
entity if entity.is_a? Person
end

def person=(person)
@entity = person
@entity = person if person
end

def organization
entity if entity.is_a? Organization
end

def organization=(organization)
@entity = organization
@entity = organization if organization
end

# @param opts [Hash]
Expand Down
16 changes: 8 additions & 8 deletions lib/relaton/bib/copyright.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class Copyright
# @param from [String] date
# @param to [String, nil] date
# @param scope [String, nil]
def initialize(owner:, from:, to: nil, scope: nil)
unless owner.any?
raise ArgumentError, "at least one owner should exist."
end
def initialize(**args)
# unless owner.any?
# raise ArgumentError, "at least one owner should exist."
# end

@owner = owner
@from = from
@to = to
@scope = scope
@owner = args[:owner]
@from = args[:rom]
@to = args[:o]
@scope = args[:cope]
end

# @param opts [Hash]
Expand Down
6 changes: 3 additions & 3 deletions lib/relaton/bib/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def date_format(date, format = nil)
# @return [Date]
def parse_date(date)
case date
when /^\d{4}-\d{1,2}-\d{1,2}/ then Date.parse(date) # 2012-02-11
when /^\d{4}-\d{1,2}/ then Date.strptime(date, "%Y-%m") # 2012-02
when /^\d{4}/ then Date.strptime(date, "%Y") # 2012
when /^\d{4}-\d{1,2}-\d{1,2}/ then ::Date.parse(date) # 2012-02-11
when /^\d{4}-\d{1,2}/ then ::Date.strptime(date, "%Y-%m") # 2012-02
when /^\d{4}/ then ::Date.strptime(date, "%Y") # 2012
else date
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/relaton/bib/depiction.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Relaton
module Bib
class Depiction
attr_accessor :scope, :image

#
# @param [String] scope Description of what is being depicted
# @param [Array<Relaton::Bib::Image>] image A visual depiction of the bibliographic item
Expand Down
10 changes: 5 additions & 5 deletions lib/relaton/bib/edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ module Relaton
module Bib
class Edition
# @return [String] edition
attr_reader :content
attr_accessor :content

# @return [String, nil] number
attr_reader :number
attr_accessor :number

#
# Initialize edition.
#
# @param [String, Integer, Float] content edition
# @param [String, Integer, Float, nil] number number
#
def initialize(content:, number: nil)
@content = content.to_s
@number = number&.to_s
def initialize(**args)
@content = args[:content].to_s
@number = args[:number]&.to_s
end

#
Expand Down
43 changes: 43 additions & 0 deletions lib/relaton/bib/extent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Relaton
module Bib
class Extent
attr_accessor :locality, :locality_stack

#
# @param [Array<RelatonBib::Locality>] locality
# @param [ArrayRelatonBib::LocalityStack>] locality_stack
#
def initialize(**args)
@locality = args[:locality] || []
@locality_stack = args[:locality_stack] || []
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
end
6 changes: 3 additions & 3 deletions lib/relaton/bib/forename.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class Forename < LocalizedString
# Initialize Forename instance
#
# @param [String] content content of forename, can be empty
# @param [Array<String>] language languages, `en`, `fr`, `de` etc.
# @param [Array<String>] script scripts `Latn`, `Cyrl` etc.
# @param [String] language languages, `en`, `fr`, `de` etc.
# @param [String] script scripts `Latn`, `Cyrl` etc.
# @param [String, nil] initial initial of forename
#
def initialize(content: nil, language: [], script: [], initial: nil)
@initial = initial
super content, language, script
super # content, language, script
end

def to_s
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton/bib/formatted_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(content: "", language: nil, script: nil, format: "text/plain")
# end

@format = format
super(content, language, script)
super(content: content, language: language, script: script)
end

# @param builder [Nokogiri::XML::Builder]
Expand Down
31 changes: 24 additions & 7 deletions lib/relaton/bib/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
require_relative "bversion"
require_relative "place"
require_relative "price"
require_relative "extent"
require_relative "size"
require_relative "structured_identifier"
require_relative "editorial_group"
Expand All @@ -57,38 +58,39 @@ class Item
schema_version: nil,
fetched: nil,
formattedref: nil,
title: -> { TitleCollection.new },
source: -> { [] },
docidentifier: -> { [] },
docnumber: nil,
date: -> { [] },
contributor: -> { [] },
edition: nil,
version: -> { [] },
biblionote: -> { [] },
note: -> { [] },
language: -> { [] },
locale: -> { [] },
script: -> { [] },
abstract: -> { [] },
status: nil,
copyright: -> { [] },
relation: -> { RelationCollection.new },
series: -> { [] },
medium: nil,
place: -> { [] },
price: -> { [] },
extent: -> { [] },
bibliographic_size: nil,
size: nil,
accesslocation: -> { [] },
license: -> { [] },
classification: -> { [] },
keyword: -> { [] },
validity: nil,
depiction: nil,
editorialgroup: nil,
}.freeze

ATTRS.each_key { |k| attr_accessor k }

attr_reader :title, :relation

# @param id [String, nil]
# @param type [String, nil]
# @param schema_version [String, nil]
Expand Down Expand Up @@ -129,13 +131,24 @@ class Item
# @param structuredidentifier [Relaton::Bib::StructuredIdentifierCollection]
# @param size [Relaton::Bib::Size, nil]
def initialize(**args)
@title = args[:title] || [] # TitleCollection.new
@relation = args[:relation] || [] # RelationCollection.new

ATTRS.each do |k, v|
val = args[k] || v&.call
instance_variable_set "@#{k}", val
end
@schema_version = schema
end

def title=(title)
@title = title.is_a?(TitleCollection) ? title : TitleCollection.new(title)
end

def relation=(relation)
@relation = relation.is_a?(RelationCollection) ? relation : RelationCollection.new(relation: relation)
end

# def title_to_xml(mode, doc, builder)
# doc
# end
Expand Down Expand Up @@ -193,7 +206,7 @@ def makeid(identifier, attribute)
identifier ||= @docidentifier.reject { |i| i.type == "DOI" }[0]
return unless identifier

idstr = identifier.id.gsub(/[:\/]/, "-").gsub(/[\s\(\)]/, "")
idstr = identifier.content.gsub(/[:\/]/, "-").gsub(/[\s\(\)]/, "")
idstr.strip
end

Expand Down Expand Up @@ -228,6 +241,10 @@ def shortref(identifier, **opts) # rubocop:disable Metrics/CyclomaticComplexity,
# end
# end

def to_xml(bibdata: false)
bibdata ? Model::Bibdata.to_xml(self) : Model::Bibitem.to_xml(self)
end

#
# Render BibXML (RFC)
#
Expand Down Expand Up @@ -336,7 +353,7 @@ def disable_id_attribute
def to_all_parts # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength,Metrics/PerceivedComplexity
me = deep_clone
me.disable_id_attribute
me.relation << DocumentRelation.new(type: "instanceOf", bibitem: self)
me.relation << Relation.new(type: "instanceOf", bibitem: self)
me.language.each do |l|
me.title.delete_title_part!
ttl = me.title.select do |t|
Expand Down Expand Up @@ -367,7 +384,7 @@ def to_all_parts # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
def to_most_recent_reference
me = deep_clone
disable_id_attribute
me.relation << DocumentRelation.new(type: "instanceOf", bibitem: self)
me.relation << Relation.new(type: "instanceOf", bibitem: self)
me.abstract = []
me.date = []
me.docidentifier.each &:remove_date
Expand Down
20 changes: 10 additions & 10 deletions lib/relaton/bib/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ class Organization
# Organization identifier.
class Identifier
# @return [String]
attr_accessor :type, :value
attr_accessor :type, :content

# @param type [String]
# @param value [String]
def initialize(type, value)
@type = type
@value = value
# @param content [String]
def initialize(**args)
@type = args[:type]
@content = args[:content]
end

# @param builder [Nokogiri::XML::Builder]
# def to_xml(builder)
# builder.identifier(value, type: type)
# builder.identifier(content, type: type)
# end

# @return [Hash]
# def to_hash
# { "type" => type, "id" => value }
# { "type" => type, "id" => content }
# end

# @param prefix [String]
Expand All @@ -33,7 +33,7 @@ def to_asciibib(prefix = "", count = 1)
pref = prefix.empty? ? prefix : "#{prefix}."
out = count > 1 ? "#{pref}identifier::\n" : ""
out += "#{pref}identifier.type:: #{type}\n"
out += "#{pref}identifier.value:: #{value}\n"
out += "#{pref}identifier.content:: #{content}\n"
out
end
end
Expand Down Expand Up @@ -71,8 +71,8 @@ def initialize(**args)
# @param identifier [Array<Relaton::Bib::Organization::Identifier>]
# @param contact [Array<Relaton::Bib::Address, Relaton::Bib::Contact>]
# @param logo [Relaton::Bib::Image, nil]
def initialize(name:, **args)
@name = name
def initialize(**args)
@name = args[:name]
@abbreviation = args[:abbreviation]
@subdivision = args[:subdivision] || []
@identifier = args[:identifier] || []
Expand Down
Loading

0 comments on commit 511cf82

Please sign in to comment.