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

fixing model V1 reading and localized-concept path in V2 #96

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 28 additions & 2 deletions lib/glossarist/concept_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ConceptManager
# Path to concepts directory.
# @return [String]
attr_accessor :path
attr_accessor :localized_concepts_path

# @param path [String]
# concepts directory path, either absolute or relative to CWD
Expand Down Expand Up @@ -60,8 +61,10 @@ def load_localized_concept(id)
end

def save_concept_to_file(concept)
@localized_concepts_path ||= "localized_concept"
concept_dir = File.join(path, "concept")
localized_concept_dir = File.join(path, "localized_concept")

localized_concept_dir = File.join(path, @localized_concepts_path)

Dir.mkdir(concept_dir) unless Dir.exist?(concept_dir)
Dir.mkdir(localized_concept_dir) unless Dir.exist?(localized_concept_dir)
Expand All @@ -86,7 +89,30 @@ def concepts_glob
end

def localized_concept_path(id)
Dir.glob(File.join(path, "localized_concept", "#{id}.{yaml,yml}"))&.first
localized_concept_possible_dir = {
"localized_concept" => File.join(
path,
"localized_concept",
"#{id}.{yaml,yml}",
),

"localized-concept" => File.join(
path,
"localized-concept",
"#{id}.{yaml,yml}",
),
}

localized_concept_possible_dir.each do |dir_name, file_path|
actual_path = Dir.glob(file_path)&.first

if actual_path
@localized_concepts_path = dir_name
return actual_path
end
end

actual_path
end

def v1_collection?
Expand Down
2 changes: 1 addition & 1 deletion lib/glossarist/v1_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.load_concept_from_file(filename)
end

def load_concept_from_file(filename)
concept_hash = Psych.safe_load(File.read(filename), permitted_classes: [Date])
concept_hash = Psych.safe_load(File.read(filename), permitted_classes: [Date, Time])
Config.class_for(:managed_concept).new(generate_v2_concept_hash(concept_hash))
end

Expand Down
93 changes: 68 additions & 25 deletions spec/features/v2_serialization_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,83 @@
RSpec.describe "Serialization and deserialization" do
let(:concept_folder) { "concept_collection_v2" }
let(:concept_files) { Dir.glob(File.join(fixtures_path(concept_folder), "concept", "*.{yaml,yml}")) }
let(:localized_concepts_folder) { File.join(fixtures_path(concept_folder), "localized_concept") }
context "when localized_concept (with underscore as separator)" do
let(:concept_folder) { "concept_collection_v2" }
let(:concept_files) { Dir.glob(File.join(fixtures_path(concept_folder), "concept", "*.{yaml,yml}")) }
let(:localized_concepts_folder) { File.join(fixtures_path(concept_folder), "localized_concept") }

it "correctly loads concepts from files" do
collection = Glossarist::ManagedConceptCollection.new
collection.load_from_files(fixtures_path(concept_folder))
it "correctly loads concepts from files" do
collection = Glossarist::ManagedConceptCollection.new
collection.load_from_files(fixtures_path(concept_folder))

concept_files.each do |filename|
concept_from_file = load_yaml_file(filename)
concept = collection[concept_from_file["data"]["identifier"]]
concept_files.each do |filename|
concept_from_file = load_yaml_file(filename)
concept = collection[concept_from_file["data"]["identifier"]]

expect(concept.to_h["data"]).to eq(concept_from_file["data"])
expect(concept.to_h["data"]).to eq(concept_from_file["data"])

concept.localized_concepts.each do |lang, id|
localized_concept_path = File.join(localized_concepts_folder, "#{id}.yaml")
localized_concept = load_yaml_file(localized_concept_path)
concept.localized_concepts.each do |lang, id|
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["data"])
end
end

Dir.mktmpdir do |tmp_path|
collection.save_to_files(tmp_path)

# check if concept and localized_concept folder exist
system "diff", fixtures_path(concept_folder), tmp_path
expect($?.exitstatus).to eq(0) # no difference

# check content of conecept folder
system "diff", File.join(fixtures_path(concept_folder), "concept"), File.join(tmp_path, "concept")
expect($?.exitstatus).to eq(0) # no difference

# check content of localized_conecept folder
system "diff", File.join(fixtures_path(concept_folder), "localized_concept"), File.join(tmp_path, "localized_concept")
expect($?.exitstatus).to eq(0) # no difference
end
end
end

context "when localized-concept (with dash as separator)" do
let(:concept_folder) { "concept_collection_v2_dashed" }
let(:concept_files) { Dir.glob(File.join(fixtures_path(concept_folder), "concept", "*.{yaml,yml}")) }
let(:localized_concepts_folder) { File.join(fixtures_path(concept_folder), "localized-concept") }

Dir.mktmpdir do |tmp_path|
collection.save_to_files(tmp_path)
it "correctly loads concepts from files" do
collection = Glossarist::ManagedConceptCollection.new
collection.load_from_files(fixtures_path(concept_folder))

# check if concept and localized_concept folder exist
system "diff", fixtures_path(concept_folder), tmp_path
expect($?.exitstatus).to eq(0) # no difference
concept_files.each do |filename|
concept_from_file = load_yaml_file(filename)
concept = collection[concept_from_file["data"]["identifier"]]

# check content of conecept folder
system "diff", File.join(fixtures_path(concept_folder), "concept"), File.join(tmp_path, "concept")
expect($?.exitstatus).to eq(0) # no difference
expect(concept.to_h["data"]).to eq(concept_from_file["data"])

# check content of localized_conecept folder
system "diff", File.join(fixtures_path(concept_folder), "localized_concept"), File.join(tmp_path, "localized_concept")
expect($?.exitstatus).to eq(0) # no difference
concept.localized_concepts.each do |lang, id|
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"])
end
end

Dir.mktmpdir do |tmp_path|
collection.save_to_files(tmp_path)

# check if concept and localized-concept folder exist
system "diff", fixtures_path(concept_folder), tmp_path
expect($?.exitstatus).to eq(0) # no difference

# check content of conecept folder
system "diff", File.join(fixtures_path(concept_folder), "concept"), File.join(tmp_path, "concept")
expect($?.exitstatus).to eq(0) # no difference

# check content of localized-conecept folder
system "diff", File.join(fixtures_path(concept_folder), "localized-concept"), File.join(tmp_path, "localized-concept")
expect($?.exitstatus).to eq(0) # no difference
end
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/concept_collection_v1/concept-3.1.1.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ eng:
process, etc."
language_code: eng
entry_status: valid
date_accepted: 2008-11-15 00:00:00.000000000 +08:00
sources:
- type: authoritative
origin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
data:
identifier: 3.1.1.1
localized_concepts:
eng: ae27e36e-858c-5026-9dc7-fa366c464f01
eng: d7b05924-4158-5341-8167-379f9051fc7f
groups:
- foo
- bar
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
data:
dates: []
dates:
- date: 2008-11-15 00:00:00.000000000 +08:00
type: accepted
definition:
- content: concrete or abstract thing that exists, did exist, or can possibly exist,
including associations among these things
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
data:
identifier: '2119'
localized_concepts:
eng: da24b782-1551-5128-a043-ba6135a25acf
sources:
- origin:
ref: ISO 1087-1:2000
clause: 3.2.9
link: https://www.iso.org/standard/20057.html
type: authoritative
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
data:
identifier: '1659'
localized_concepts:
eng: c87dfcd1-c38a-55f9-87bd-9da33bfede80
spa: becf3892-886d-5dab-8a7c-af303e576a8d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
data:
identifier: '200'
localized_concepts:
ara: e4ee4f5c-07b0-577e-8bc0-37e0f98d7a2b
dan: eaea6d0f-c655-59c9-98f7-9affbdce7612
deu: c2cc493d-bc21-50a5-96fc-6774f3d53496
eng: 27457e38-89b5-5694-8d19-0dd3973ec71d
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
data:
identifier: '888'
localized_concepts:
ara: '081154c5-89d6-5192-8147-373bd6060eaa'
deu: 527bd617-f471-5523-9b98-59bb181f3df8
eng: bf1691ef-6b21-590a-aef1-9e67ad54378e
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
data:
dates:
- date: '2010-11-01T00:00:00.000Z'
type: accepted
definition:
- content: مجموعة من الخصائص التي تكون المفهوم
examples: []
id: '888'
lineage_source_similarity: 1
notes: []
release: '2'
sources:
- origin:
ref: ISO 1087-1:2000
clause: 3.2.9
link: https://www.iso.org/standard/20057.html
type: authoritative
- origin:
ref: ISO 19146:2010(E)
type: lineage
terms:
- type: expression
normative_status: preferred
designation: دلالة
language_code: ara
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
data:
dates:
- date: '2003-02-15T00:00:00.000Z'
type: accepted
definition:
- content: angle from the equatorial plane to the perpendicular to the ellipsoid
through a given point, northwards treated as positive
examples: []
id: '200'
lineage_source_similarity: 1
notes: []
release: '1'
sources:
- origin:
ref: ISO 19111:2019
clause: 3.1.32
link: https://www.iso.org/standard/74039.html
type: authoritative
- origin:
ref: ISO 19111:2003
type: lineage
terms:
- type: abbreviation
designation: j
- type: expression
normative_status: preferred
designation: geodetic latitude
- type: expression
designation: ellipsoidal latitude
language_code: eng
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
data:
dates:
- date: '2010-11-01T00:00:00.000Z'
type: accepted
definition: []
examples: []
id: '888'
lineage_source_similarity: 1
notes: []
release: '2'
sources:
- origin:
ref: ISO 1087-1:2000
clause: 3.2.9
link: https://www.iso.org/standard/20057.html
type: authoritative
- origin:
ref: ISO 19146:2010(E)
type: lineage
terms:
- type: expression
normative_status: preferred
designation: Intension
language_code: deu
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
data:
dates:
- date: '2015-08-15T00:00:00.000Z'
type: accepted
- date: '2019-07-11T00:00:00.000Z'
type: amended
definition:
- content: sistema de coordenadas que proporciona la posición de los puntos en relación
con n ejes mutuamente perpendiculares de curvatura cero
examples: []
id: '1659'
notes:
- content: para los propósitos de esta Norma Internacional n es 2 o 3.
release: "-4"
sources:
- origin:
ref: ISO 19162:2015
clause: 4.1.3
link: https://www.iso.org/standard/63094.html
type: authoritative
terms:
- type: expression
designation: Sistema de Coordenadas Cartesianas
language_code: spa
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
data:
dates:
- date: '2010-11-01T00:00:00.000Z'
type: accepted
definition:
- content: set of characteristics which makes up the concept
examples: []
id: '888'
lineage_source_similarity: 1
notes: []
release: '2'
sources:
- origin:
ref: ISO 1087-1:2000
clause: 3.2.9
link: https://www.iso.org/standard/20057.html
type: authoritative
- origin:
ref: ISO 19146:2010(E)
type: lineage
terms:
- type: expression
normative_status: preferred
designation: intension
language_code: eng
Loading
Loading