Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Oct 2, 2023
1 parent e0560a1 commit 9cb7f2e
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 32 deletions.
52 changes: 35 additions & 17 deletions lib/relaton_bib/contributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,13 @@ class Affiliation
# @return [RelatonBib::LocalizedString, nil]
attr_reader :name

# @return [Array<RelatonBib::FormattedString>]
attr_reader :description

# @return [RelatonBib::Organization]
attr_reader :organization

# @param organization [RelatonBib::Organization]
# @param organization [RelatonBib::Organization, nil]
# @param name [RelatonBib::LocalizedString, nil]
# @param description [Array<RelatonBib::FormattedString>]
def initialize(organization:, name: nil, description: [])
unless organization.is_a? RelatonBib::Organization
raise ArgumentError, "organization should be an instance of RelatonBib::Organization"
end

def initialize(organization: nil, name: nil, description: [])
@name = name
@organization = organization
@description = description
Expand All @@ -145,19 +138,44 @@ def initialize(organization:, name: nil, description: [])
# @param opts [Hash]
# @option opts [Nokogiri::XML::Builder] :builder XML builder
# @option opts [String] :lang language
def to_xml(**opts) # rubocop:disable Metrics/AbcSize
def to_xml(**opts)
return unless organization || name || description&.any?

opts[:builder].affiliation do |builder|
builder.name { name.to_xml builder } if name
desc = description.select { |d| d.language&.include? opts[:lang] }
desc = description unless desc.any?
desc.each { |d| builder.description { d.to_xml builder } }
organization.to_xml(**opts)
name_xml builder
description_xml builder
organization&.to_xml(**opts)
end
end

def name_xml(builder)
builder.name { name.to_xml builder } if name
end

def description_xml(builder)
description.each { |d| builder.description { d.to_xml builder } }
end

#
# Get description.
#
# @param [String, nil] lang language if nil return all description
#
# @return return [Array<RelatonBib::FormattedString>] description
#
def description(lang = nil)
return @description unless lang

@description.select { |d| d.language&.include? lang }
end

#
# Render affiliation as hash.
#
# @return [Hash]
#
def to_hash
hash = organization.to_hash
hash = organization&.to_hash || {}
hash["name"] = name.to_hash if name
if description&.any?
hash["description"] = single_element_array(description)
Expand All @@ -175,7 +193,7 @@ def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize
description.each do |d|
out += d.to_asciibib "#{pref}affiliation.description", description.size
end
out += organization.to_asciibib "#{pref}affiliation.*"
out += organization.to_asciibib "#{pref}affiliation.*" if organization
out
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_bib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RelatonBib
VERSION = "1.16.1".freeze
VERSION = "1.16.2".freeze
end
136 changes: 122 additions & 14 deletions spec/relaton_bib/contributor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,136 @@
end

describe RelatonBib::Affiliation do
it "raises ArgumentError if organization is not provided" do
expect { RelatonBib::Affiliation.new organization: nil }.to raise_error(
ArgumentError, /organization should be an instance of RelatonBib::Organization/
)
let(:org) { RelatonBib::Organization.new(name: "Org") }
let(:name) { RelatonBib::LocalizedString.new("Name", "en") }
let(:desc) { RelatonBib::FormattedString.new(content: "Description", language: "en") }
subject do
description = desc ? [desc] : []
described_class.new(organization: org, name: name, description: description)
end

context "render affiliation" do
let(:org) { RelatonBib::Organization.new(name: "org") }
let(:affiliation) { RelatonBib::Affiliation.new(organization: org) }
context "with all fields" do
it "as XML" do
xml = Nokogiri::XML::Builder.new { |b| subject.to_xml(builder: b) }.doc.root
expect(xml.to_s).to be_equivalent_to <<~XML
<affiliation>
<name language="en">Name</name>
<description format="text/plain" language="en">Description</description>
<organization>
<name>Org</name>
</organization>
</affiliation>
XML
end

it "as XML" do
xml = Nokogiri::XML::Builder.new { |b| affiliation.to_xml(builder: b) }.doc.root
expect(xml.text).to eq "org"
it "as Hash" do
hash = subject.to_hash
expect(hash["organization"]["name"][0]["content"]).to eq "Org"
expect(hash["name"]["content"]).to eq "Name"
expect(hash["name"]["language"]).to eq ["en"]
expect(hash["description"][0]["content"]).to eq "Description"
expect(hash["description"][0]["language"]).to eq ["en"]
end

it "as AsciiBib" do
expect(subject.to_asciibib).to eq <<~ASCIIBIB
affiliation.name.content:: Name
affiliation.name.language:: en
affiliation.description.content:: Description
affiliation.description.language:: en
affiliation.description.format:: text/plain
affiliation.organization.name:: Org
ASCIIBIB
end
end

it "as Hash" do
hash = affiliation.to_hash
expect(hash["organization"]["name"][0]["content"]).to eq "org"
context "without organization" do
let(:org) { nil }

it "as XML" do
xml = Nokogiri::XML::Builder.new { |b| subject.to_xml(builder: b) }.doc.root
expect(xml.to_s).to be_equivalent_to <<~XML
<affiliation>
<name language="en">Name</name>
<description format="text/plain" language="en">Description</description>
</affiliation>
XML
end

it "as Hash" do
hash = subject.to_hash
expect(hash).not_to have_key "organization"
end

it "as AsciiBib" do
expect(subject.to_asciibib).not_to include "affiliation.organization"
end
end

it "as AsciiBib" do
expect(affiliation.to_asciibib).to eq "affiliation.organization.name:: org\n"
context "without name" do
let(:name) { nil }

it "as XML" do
xml = Nokogiri::XML::Builder.new { |b| subject.to_xml(builder: b) }.doc.root
expect(xml.to_s).to be_equivalent_to <<~XML
<affiliation>
<description format="text/plain" language="en">Description</description>
<organization>
<name>Org</name>
</organization>
</affiliation>
XML
end

it "as Hash" do
hash = subject.to_hash
expect(hash).not_to have_key "name"
end

it "as AsciiBib" do
expect(subject.to_asciibib).not_to include "affiliation.name"
end
end

context "without description" do
let(:desc) { nil }

it "as XML" do
xml = Nokogiri::XML::Builder.new { |b| subject.to_xml(builder: b) }.doc.root
expect(xml.to_s).to be_equivalent_to <<~XML
<affiliation>
<name language="en">Name</name>
<organization>
<name>Org</name>
</organization>
</affiliation>
XML
end

it "as Hash" do
expect(subject.to_hash).not_to have_key "description"
end

it "as AsciiBib" do
expect(subject.to_asciibib).not_to include "affiliation.description"
end
end

it "without any fields" do
xml = Nokogiri::XML::Builder.new { |b| described_class.new.to_xml(builder: b) }.doc.root
expect(xml.to_s).to eq ""
end
end

context "desctiption" do
it "returns all descriptions if language is not specified" do
expect(subject.description).to eq [desc]
end

it "returns description with specified language" do
expect(subject.description("en")).to eq [desc]
expect(subject.description("fr")).to be_empty
end
end
end

0 comments on commit 9cb7f2e

Please sign in to comment.