Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to lutaml-model #90

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/relaton/bib/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def initialize(**args) # rubocop:disable Metrics/CyclomaticComplexity
@formatted_address = args[:formatted_address] unless args[:city] && args[:country]
end

def ==(other) # rubocop:disable Metrics/AbcSize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lutaml::Model supports equivalence checks of attributes

street == other.street && city == other.city && state == other.state &&
country == other.country && postcode == other.postcode &&
formatted_address == other.formatted_address
end

# @param doc [Nokogiri::XML::Document]
# def to_xml(doc) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# doc.address do
Expand Down
9 changes: 8 additions & 1 deletion lib/relaton/bib/affiliation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ class Affiliation
# @return [Relaton::Bib::LocalizedString, nil]
attr_accessor :name

attr_writer :description

# @return [Relaton::Bib::Organization]
attr_accessor :organization

# @param organization [Relaton::Bib::Organization, nil]
# @param name [Relaton::Bib::LocalizedString, nil]
# @param description [Array<Relaton::Bib::FormattedString>]
# @param description [Array<Relaton::Bib::LocalizedString>]
def initialize(organization: nil, name: nil, description: [])
@name = name
@organization = organization
@description = description
end

def ==(other)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

name == other.name && organization == other.organization &&
description == other.description
end

# @param opts [Hash]
# @option opts [Nokogiri::XML::Builder] :builder XML builder
# @option opts [String] :lang language
Expand Down
8 changes: 4 additions & 4 deletions lib/relaton/bib/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Date
vote-started vote-ended announced stable-until].freeze

# @return [String]
attr_accessor :type, :text
attr_accessor :type, :text, :format

attr_writer :on, :from, :to

Expand Down Expand Up @@ -41,7 +41,7 @@ def initialize(**args)
# @return [String, Date, nil]
def from(part = nil) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
d = instance_variable_get "@#{__callee__}".to_sym
return d unless part && d
return date_format(d) unless part && d

# date = parse_date(d)
# return date if part == :date
Expand Down Expand Up @@ -102,14 +102,14 @@ def to_asciibib(prefix = "", count = 1)
# @param date [String]
# @param format [Symbol, nil] :full (yyyy-mm-dd), :short (yyyy-mm) or nil
# @return [String]
def date_format(date, format = nil)
def date_format(date)
tmplt = case format
when :short then "%Y-%m"
when :full then "%Y-%m-%d"
else return date
end
d = parse_date(date)
d.is_a?(Date) ? d.strftime(tmplt) : d
d.respond_to?(:strftime) ? d.strftime(tmplt) : d
end

# @param date [String]
Expand Down
1 change: 0 additions & 1 deletion lib/relaton/bib/docidentifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Relaton
module Bib
# Document identifier.
class Docidentifier < DocidentifierType

#
# Add docidentifier xml element
#
Expand Down
4 changes: 4 additions & 0 deletions lib/relaton/bib/docidentifier_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def initialize(**args)
@primary = args[:primary]
end

def content=(value)
@content = value.strip
end

# in docid manipulations, assume ISO as the default: id-part:year
def remove_part
case @type
Expand Down
30 changes: 15 additions & 15 deletions lib/relaton/bib/document_type.rb → lib/relaton/bib/doctype.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module Relaton
module Bib
class DocumentType
attr_reader :type, :abbreviation
class Doctype
attr_accessor :content, :abbreviation

#
# Initialize a DocumentType.
#
# @param [String] type document type
# @param [String] content document type
# @param [String, nil] abbreviation type abbreviation
#
def initialize(type:, abbreviation: nil)
@type = type
def initialize(content: nil, abbreviation: nil)
@content = content
@abbreviation = abbreviation
end

Expand All @@ -19,21 +19,21 @@ def initialize(type:, abbreviation: nil)
#
# @param [Nokogiri::XML::Builder] builder XML builder
#
def to_xml(builder)
xml = builder.doctype @type
xml[:abbreviation] = @abbreviation if @abbreviation
end
# def to_xml(builder)
# xml = builder.doctype @content
# xml[:abbreviation] = @abbreviation if @abbreviation
# end

#
# Hash representation of the document type.
#
# @return [Hash]
#
def to_hash
hash = { "type" => @type }
hash["abbreviation"] = @abbreviation if @abbreviation
hash
end
# def to_hash
# hash = { "type" => @content }
# hash["abbreviation"] = @abbreviation if @abbreviation
# hash
# end

#
# Asciibib representation of the document type.
Expand All @@ -45,7 +45,7 @@ def to_hash
def to_asciibib(prefix = "")
pref = prefix.empty? ? prefix : "#{prefix}."
pref += "doctype."
out = "#{pref}type:: #{@type}\n"
out = "#{pref}content:: #{@content}\n"
out += "#{pref}abbreviation:: #{@abbreviation}\n" if @abbreviation
out
end
Expand Down
22 changes: 11 additions & 11 deletions lib/relaton/bib/editorial_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ module Bib
class EditorialGroup
# include Relaton

# @return [Array<Relaton::Bib::TechnicalCommittee>]
# @return [Array<Relaton::Bib::WorkGroup>]
attr_accessor :technical_committee

# @param technical_committee [Array<Relaton::Bib::TechnicalCommittee>]
def initialize(technical_committee)
# @param technical_committee [Array<Relaton::Bib::WorkGroup>]
def initialize(technical_committee = [])
@technical_committee = technical_committee
end

# @param builder [Nokogigi::XML::Builder]
def to_xml(builder)
builder.editorialgroup do |b|
technical_committee.each { |tc| tc.to_xml b }
end
end
# def to_xml(builder)
# builder.editorialgroup do |b|
# technical_committee.each { |tc| tc.to_xml b }
# end
# end

# @return [Hash]
def to_hash
single_element_array technical_committee
end
# def to_hash
# single_element_array technical_committee
# end

# @param prefix [String]
# @return [String]
Expand Down
13 changes: 11 additions & 2 deletions lib/relaton/bib/ext.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
require_relative "doctype"
require_relative "editorial_group"
require_relative "ics"
require_relative "structured_identifier"

module Relaton
module Bib
class Ext
attr_reader :editorialgroup, :ics, :structuredidentifier
attr_accessor :doctype, :subdoctype, :flavor, :editorialgroup, :ics

attr_reader :structuredidentifier

# @param doctype [Relaton::Bib::DocumentType]
# @param subdoctype [String]
# @param flavor [String]
# @param editorialgroup [Relaton::Bib::EditorialGroup, nil]
# @param ics [Array<Relaton::Bib::ICS>]
# @param structuredidentifier [Relaton::Bib::StructuredIdentifierCollection]
def initialize(**args)
@editorialgroup = args[:editorialgroup] || []
@doctype = args[:doctype]
@subdoctype = args[:subdoctype]
@flavor = args[:flavor]
@editorialgroup = args[:editorialgroup]
@ics = args[:ics] || []
@structuredidentifier = args[:structuredidentifier] || StructuredIdentifierCollection.new
end
Expand Down
18 changes: 12 additions & 6 deletions lib/relaton/bib/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
require_relative "phone"
require_relative "uri"
require_relative "affiliation"
require_relative "document_type"
require_relative "image"
require_relative "forename"
require_relative "full_name"
Expand Down Expand Up @@ -132,8 +131,6 @@ class Item
# @param keyword [Array<String>]
# @param validity [Relaton::Bib:Validity, nil]
# @param depiction [Relaton::Bib::Depiction, nil]
# @param doctype [Relaton::Bib::DocumentType]
# @param subdoctype [String]
# @param size [Relaton::Bib::Size, nil]
# @param ext [Relaton::Bib::Ext, nil]
def initialize(**args)
Expand All @@ -154,9 +151,18 @@ def relation=(relation)
@relation = relation.is_a?(RelationCollection) ? relation : RelationCollection.new(relation)
end

# def title_to_xml(mode, doc, builder)
# doc
# end
#
# <Description>
#
# @param [Symbol, nil] format format of date output (:short, :full)
#
# @return [Realton::Bib::Item]
#
def date_format(format = nil)
item = deep_clone
item.date.each { |d| d.format = format }
item
end

#
# Fetch schema version
Expand Down
4 changes: 2 additions & 2 deletions lib/relaton/bib/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def to_asciibib(prefix = "", count = 1)
# end
# end

# @return [Array<Relaton::Bib::Organization::Name>]
# @return [Array<Relaton::Bib::TypedLocalizedString>]
attr_accessor :name

# @return [Relaton::Bib::LocalizedString, nil]
Expand Down Expand Up @@ -123,7 +123,7 @@ def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize,Metric
out += sd.to_asciibib "#{pref}.subdivision"
end
identifier.each { |n| out += n.to_asciibib pref, identifier.size }
out += super pref
# out += super pref
out += logo.to_asciibib "#{pref}.logo" if logo
out
end
Expand Down
12 changes: 6 additions & 6 deletions lib/relaton/bib/technical_committee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def initialize(workgroup)
end

# @param builder [Nokogiri::XML::Builder]
def to_xml(builder)
builder.send(:"technical-committee") { |b| workgroup.to_xml b }
end
# def to_xml(builder)
# builder.send(:"technical-committee") { |b| workgroup.to_xml b }
# end

# @return [Hash]
def to_hash
workgroup.to_hash
end
# def to_hash
# workgroup.to_hash
# end

# @param prefix [String]
# @param count [Integer] number of technical committees
Expand Down
44 changes: 22 additions & 22 deletions lib/relaton/bib/workgroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,53 @@ module Relaton
module Bib
class WorkGroup
# @return [String]
attr_reader :name
attr_accessor :content

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

# @return [String, nil]
attr_reader :identifier, :prefix, :type
attr_accessor :identifier, :prefix, :type

# @param name [String]
# @param content [String]
# @param identifier [String, nil]
# @param prefix [String, nil]
# @param number [Integer, nil]
# @param type [String, nil]
def initialize(name:, identifier: nil, prefix: nil, number: nil, type: nil)
def initialize(content: nil, identifier: nil, prefix: nil, number: nil, type: nil)
@identifier = identifier
@prefix = prefix
@name = name
@content = content
@number = number
@type = type
end

# @param builder [Nokogiri::XML::Builder]
def to_xml(builder) # rubocop:disable Metrics/AbcSize
builder.text name
builder.parent[:number] = number if number
builder.parent[:type] = type if type
builder.parent[:identifier] = identifier if identifier
builder.parent[:prefix] = prefix if prefix
end
# def to_xml(builder) # rubocop:disable Metrics/AbcSize
# builder.text name
# builder.parent[:number] = number if number
# builder.parent[:type] = type if type
# builder.parent[:identifier] = identifier if identifier
# builder.parent[:prefix] = prefix if prefix
# end

# @return [Hash]
def to_hash
hash = { "name" => name }
hash["number"] = number if number
hash["type"] = type if type
hash["identifier"] = identifier if identifier
hash["prefix"] = prefix if prefix
hash
end
# def to_hash
# hash = { "name" => name }
# hash["number"] = number if number
# hash["type"] = type if type
# hash["identifier"] = identifier if identifier
# hash["prefix"] = prefix if prefix
# hash
# end

# @param prfx [String]
# @param count [Integer]
# @return [String]
def to_asciibib(prfx = "", count = 1) # rubocop:disable Metrics/CyclomaticComplexity
pref = prfx.empty? ? prfx : "#{prfx}."
out = count > 1 ? "#{pref}::\n" : ""
out += "#{pref}name:: #{name}\n"
out += "#{pref}content:: #{content}\n"
out += "#{pref}number:: #{number}\n" if number
out += "#{pref}type:: #{type}\n" if type
out += "#{pref}identifier:: #{identifier}\n" if identifier
Expand Down
Loading
Loading