Skip to content

Commit

Permalink
feat: support paneron glossarist attributes in localized concept, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldtse committed Nov 18, 2024
1 parent f3a7dbf commit 33e40ba
Show file tree
Hide file tree
Showing 32 changed files with 58 additions and 31 deletions.
13 changes: 10 additions & 3 deletions lib/glossarist/concept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,20 @@ def initialize(*args)
def uuid
@uuid ||= Glossarist::Utilities::UUID.uuid_v5(
Glossarist::Utilities::UUID::OID_NAMESPACE,
to_h.to_yaml,
to_h_no_uuid.to_yaml,
)
end

def id=(id)
# Some of the glossaries that are not generated using glossarist, contains ids that are integers
# Some of the glossaries that are not generated using glossarist, contains ids that are integers
# so adding a temporary check until every glossary is updated using glossarist.
if !id.nil? && (id.is_a?(String) || id.is_a?(Integer))
@id = id
else
raise(Glossarist::Error, "Expect id to be a String or Integer, Got #{id.class} (#{id})")
end
end

alias :termid= :id=
alias :identifier= :id=

Expand Down Expand Up @@ -119,6 +120,7 @@ def designations=(designations)
def preferred_designations
@designations.select(&:preferred?)
end

alias :preferred_terms :preferred_designations

def dates=(dates)
Expand All @@ -141,7 +143,7 @@ def authoritative_source=(sources)
end
end

def to_h
def to_h_no_uuid
{
"data" => {
"dates" => dates&.map(&:to_h),
Expand All @@ -154,10 +156,15 @@ def to_h
"sources" => sources.empty? ? nil : sources&.map(&:to_h),
"terms" => (terms&.map(&:to_h) || []),
"related" => related&.map(&:to_h),
"domain" => domain,
}.compact,
}.compact
end

def to_h
to_h_no_uuid.merge("id" => uuid)
end

# @deprecated For legacy reasons only.
# Implicit conversion (i.e. {#to_hash} alias) will be removed soon.
alias :to_hash :to_h
Expand Down
15 changes: 7 additions & 8 deletions lib/glossarist/localized_concept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class LocalizedConcept < Concept
# Temporary fields
# @todo Need to remove these once the isotc211-glossary is fixed
attr_accessor *%i[
review_date
review_decision_date
review_decision_event
review_type
]
review_date
review_decision_date
review_decision_event
review_type
]

def language_code=(language_code)
if language_code.is_a?(String) && language_code.length == 3
Expand All @@ -41,11 +41,10 @@ def language_code=(language_code)
end
end

def to_h # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def to_h_no_uuid # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
hash = super

hash["data"].merge!({
"domain" => domain,
"language_code" => language_code,
"entry_status" => entry_status,
"sources" => sources.empty? ? nil : sources&.map(&:to_h),
Expand All @@ -63,7 +62,7 @@ def self.from_h(hash)
terms = hash["terms"]&.map { |h| Designation::Base.from_h(h) } || []
sources = hash["authoritative_source"]&.each { |source| source.merge({ "type" => "authoritative" }) }

super(hash.merge({"terms" => terms, "sources" => sources}))
super(hash.merge({ "terms" => terms, "sources" => sources }))
end

# @deprecated For legacy reasons only.
Expand Down
2 changes: 1 addition & 1 deletion spec/features/v1_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
localized_concept_path = File.join(localized_concepts_folder, "#{id}.yaml")
localized_concept = load_yaml_file(localized_concept_path)

expect(localized_concept["data"]).to eq(concept.localizations[lang].to_h["data"])
expect(localized_concept["data"]).to eq(concept.localizations[lang].to_h_no_uuid["data"])
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/features/v2_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
localized_concept_path = File.join(localized_concepts_folder, "#{id}.yaml")
localized_concept = load_yaml_file(localized_concept_path)

expect(localized_concept["data"]).to eq(concept.localizations[lang].to_h["data"])
expect(localized_concept["data"]).to eq(concept.localizations[lang].to_h_no_uuid["data"])
end
end

Expand Down Expand Up @@ -59,7 +59,7 @@
localized_concept_path = File.join(localized_concepts_folder, "#{id}.yaml")
localized_concept = load_yaml_file(localized_concept_path)

expect(localized_concept["data"]).to eq(concept.localizations[lang].to_h["data"])
expect(localized_concept["data"]).to eq(concept.localizations[lang].to_h_no_uuid["data"])
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ data:
designation: person
language_code: eng
entry_status: valid
id: 9c15f53f-b4b1-5e29-b27b-e971fbe85740
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ data:
designation: biological_entity
language_code: eng
entry_status: valid
id: 9ea138b3-decf-5435-a067-2a581c5c663c
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ data:
designation: material entity
language_code: eng
entry_status: valid
id: c8763b71-b695-5117-9309-c7d998f63ad7
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ data:
designation: entity
language_code: eng
entry_status: valid
id: cb3f57c3-f67f-56b8-a0d2-4d16349ed89b
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ data:
designation: non-biological entity
language_code: eng
entry_status: valid
id: cd5b9b55-4d46-5a17-840d-ac315074c909
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ data:
designation: immaterial_entity
language_code: eng
entry_status: valid
id: d74f5f2e-40f2-5fd2-be7e-42af2891a882
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ data:
normative_status: preferred
designation: دلالة
language_code: ara
id: '081154c5-89d6-5192-8147-373bd6060eaa'
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
- type: expression
designation: ellipsoidal latitude
language_code: eng
id: 27457e38-89b5-5694-8d19-0dd3973ec71d
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ data:
normative_status: preferred
designation: Intension
language_code: deu
id: 527bd617-f471-5523-9b98-59bb181f3df8
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ data:
- type: expression
designation: Sistema de Coordenadas Cartesianas
language_code: spa
id: becf3892-886d-5dab-8a7c-af303e576a8d
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ data:
normative_status: preferred
designation: intension
language_code: eng
id: bf1691ef-6b21-590a-aef1-9e67ad54378e
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
- type: expression
designation: ellipsoidal latitude
language_code: deu
id: c2cc493d-bc21-50a5-96fc-6774f3d53496
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ data:
- type: expression
designation: Cartesian coordinate system
language_code: eng
id: c87dfcd1-c38a-55f9-87bd-9da33bfede80
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
designation: postal address component
domain: postal address
language_code: eng
id: da24b782-1551-5128-a043-ba6135a25acf
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
- type: expression
designation: ellipsoidal latitude
language_code: ara
id: e4ee4f5c-07b0-577e-8bc0-37e0f98d7a2b
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ data:
- type: expression
designation: geodætisk bredde
language_code: dan
id: eaea6d0f-c655-59c9-98f7-9affbdce7612
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ data:
normative_status: preferred
designation: دلالة
language_code: ara
id: '081154c5-89d6-5192-8147-373bd6060eaa'
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
- type: expression
designation: ellipsoidal latitude
language_code: eng
id: 27457e38-89b5-5694-8d19-0dd3973ec71d
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ data:
normative_status: preferred
designation: Intension
language_code: deu
id: 527bd617-f471-5523-9b98-59bb181f3df8
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ data:
- type: expression
designation: Sistema de Coordenadas Cartesianas
language_code: spa
id: becf3892-886d-5dab-8a7c-af303e576a8d
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ data:
normative_status: preferred
designation: intension
language_code: eng
id: bf1691ef-6b21-590a-aef1-9e67ad54378e
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
- type: expression
designation: ellipsoidal latitude
language_code: deu
id: c2cc493d-bc21-50a5-96fc-6774f3d53496
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ data:
- type: expression
designation: Cartesian coordinate system
language_code: eng
id: c87dfcd1-c38a-55f9-87bd-9da33bfede80
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
designation: postal address component
domain: postal address
language_code: eng
id: da24b782-1551-5128-a043-ba6135a25acf
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ data:
- type: expression
designation: ellipsoidal latitude
language_code: ara
id: e4ee4f5c-07b0-577e-8bc0-37e0f98d7a2b
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ data:
- type: expression
designation: geodætisk bredde
language_code: dan
id: eaea6d0f-c655-59c9-98f7-9affbdce7612
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#

require "glossarist"
require "tmpdir"

Bundler.require(:development)

Expand Down
28 changes: 11 additions & 17 deletions spec/unit/concept_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
let(:attrs) { { id: "123" } }

it "accepts strings as ids" do
expect { subject.id = "456" }
.to change { subject.id }.to("456")
expect { subject.id = "456" }.to change { subject.id }.to("456")
end

it "accepts integers as ids" do
expect { subject.id = 456 }
.to change { subject.id }.to(456)
expect { subject.id = 456 }.to change { subject.id }.to(456)
end

describe "#to_h" do
Expand All @@ -27,27 +25,26 @@
content: "Test content",
type: :supersedes,
},
]
],
)

retval = object.to_h["data"]
expect(retval).to be_kind_of(Hash)
expect(retval["id"]).to eq("123")
expect(retval["related"]).to eq([{"content"=>"Test content", "type"=>"supersedes"}])
expect(retval["related"]).to eq([{ "content" => "Test content", "type" => "supersedes" }])
end
end

describe "::new" do
it "accepts a hash of attributes" do
expect { described_class.new(attrs) }
.not_to raise_error
expect { described_class.new(attrs) }.not_to raise_error
end

it "generates a uuid if not given" do
concept = described_class.new(attrs)
uuid = Glossarist::Utilities::UUID.uuid_v5(
Glossarist::Utilities::UUID::OID_NAMESPACE,
concept.to_h.to_yaml
concept.to_h_no_uuid.to_yaml
)

expect(concept.uuid).to eq(uuid)
Expand Down Expand Up @@ -132,7 +129,7 @@
"type" => "authoritative",
"status" => "identical",
"origin" => { "text" => "url" },
}
},
],
"related" => [
{
Expand Down Expand Up @@ -191,8 +188,7 @@
end

it "should return only authoritative_sources" do
expect(subject.authoritative_source.map(&:to_h))
.to eq(authoritative_source)
expect(subject.authoritative_source.map(&:to_h)).to eq(authoritative_source)
end
end

Expand Down Expand Up @@ -229,11 +225,9 @@
end

it "should add to sources hash" do

expect { subject.authoritative_source = [authoritative_source] }
.to change { subject.sources.map(&:to_h) }
.from(sources)
.to(sources + [authoritative_source.merge("type" => "authoritative")])
expect { subject.authoritative_source = [authoritative_source] }.to change { subject.sources.map(&:to_h) }
.from(sources)
.to(sources + [authoritative_source.merge("type" => "authoritative")])
end
end
end

0 comments on commit 33e40ba

Please sign in to comment.