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

feat: add lutaml model #118

Merged
merged 8 commits into from
Dec 26, 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
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ There are two ways to initialize and populate a concept date
[,ruby]
----
concept_date = Glossarist::ConceptDate.new({
date: "2010-11-01T00:00:00.000Z",
date: "2010-11-01T00:00:00+00:00",
type: :accepted,
})
----
Expand All @@ -221,7 +221,7 @@ concept_date = Glossarist::ConceptDate.new({
----
concept_date = Glossarist::ConceptDate.new
concept_date.type = :accepted
concept_date.date = "2010-11-01T00:00:00.000Z"
concept_date.date = "2010-11-01T00:00:00+00:00"
----

[[id,detailed-definition]]
Expand Down
1 change: 1 addition & 0 deletions glossarist.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "lutaml-model", "~> 0.4.0"
spec.add_dependency "relaton", "~> 1.19"
spec.add_dependency "thor"
end
19 changes: 10 additions & 9 deletions lib/glossarist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@

require "psych"
require "thor"
require "lutaml/model"

require_relative "glossarist/utilities"
require_relative "glossarist/version"
require_relative "glossarist/glossary_definition"

require_relative "glossarist/designation"
require_relative "glossarist/asset"
require_relative "glossarist/model"
require_relative "glossarist/concept_date"
require_relative "glossarist/detailed_definition"
require_relative "glossarist/related_concept"
require_relative "glossarist/citation"
require_relative "glossarist/collection"
require_relative "glossarist/concept_date"
require_relative "glossarist/concept_manager"
require_relative "glossarist/concept_set"
require_relative "glossarist/concept_source"
require_relative "glossarist/collection"
require_relative "glossarist/designation"
require_relative "glossarist/detailed_definition"
require_relative "glossarist/related_concept"
require_relative "glossarist/concept_data"
require_relative "glossarist/concept"
require_relative "glossarist/localized_concept"
require_relative "glossarist/managed_concept_collection"
require_relative "glossarist/concept_manager"
require_relative "glossarist/managed_concept_data"
require_relative "glossarist/managed_concept"
require_relative "glossarist/managed_concept_collection"
require_relative "glossarist/non_verb_rep"
require_relative "glossarist/v1_reader"

require_relative "glossarist/collections"

Expand Down
13 changes: 4 additions & 9 deletions lib/glossarist/asset.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# frozen_string_literal: true

# (c) Copyright 2021 Ribose Inc.
#

module Glossarist
class Asset
attr_accessor :path
class Asset < Lutaml::Model::Serializable
attribute :path, :string

def initialize(path)
@path = path
yaml do
map :path, to: :path
end

def eql?(asset)
Expand Down
112 changes: 71 additions & 41 deletions lib/glossarist/citation.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,99 @@
# frozen_string_literal: true

# (c) Copyright 2021 Ribose Inc.
#

module Glossarist
class Citation < Model
class Citation < Lutaml::Model::Serializable
# Unstructured (plain text) reference.
# @return [String]
attr_accessor :text
attribute :text, :string

# Source in structured reference.
# @return [String]
attr_accessor :source
attribute :source, :string

# Document ID in structured reference.
# @return [String]
attr_accessor :id
attribute :id, :string

# Document version in structured reference.
# @return [String]
attr_accessor :version
attribute :version, :string

# @return [String]
# Referred clause of the document.
attr_accessor :clause
attribute :clause, :string

# Link to document.
# @return [String]
attr_accessor :link
attribute :link, :string

# Original ref text before parsing.
# @return [String]
# @note This attribute is likely to be removed or reworked in future.
# It is arguably not relevant to Glossarist itself.
attr_accessor :original
attribute :original, :string

# Whether it is a plain text ref.
# @return [Boolean]
def plain?
(source && id && version).nil?
attribute :ref, :string

yaml do
map :id, to: :id, with: { from: :id_from_yaml, to: :id_to_yaml }
map :text, to: :text, with: { from: :text_from_yaml, to: :text_to_yaml }
map :source, to: :source, with: { from: :source_from_yaml, to: :source_to_yaml }
map :version, to: :version, with: { from: :version_from_yaml, to: :version_to_yaml }
map :ref, to: :ref, with: { from: :ref_from_yaml, to: :ref_to_yaml }

map :clause, to: :clause
map :link, to: :link
map :original, to: :original
end

# Whether it is a structured ref.
# @return [Boolean]
def structured?
!plain?
def ref_from_yaml(model, value)
model.ref = value
end

def to_h
{
"ref" => ref_to_h,
"clause" => clause,
"link" => link,
"original" => original,
}.compact
def ref_to_yaml(model, doc)
doc["ref"] = if model.structured?
ref_hash(model)
else
model.text
end
end

def id_from_yaml(model, value)
model.id = value
end

def id_to_yaml(_model, _doc)
# skip, will be handled in ref
end

def text_from_yaml(model, value)
model.text = value
end

def self.from_h(hash)
hash = hash.dup
def text_to_yaml(_model, _doc)
# skip, will be handled in ref
end

def source_from_yaml(model, value)
model.source = value
end

def source_to_yaml(_model, _doc)
# skip, will be handled in ref
end

ref_val = hash.delete("ref")
hash.merge!(Hash === ref_val ? ref_val : {"text" => ref_val})
hash.compact!
def version_from_yaml(model, value)
model.version = value
end

def version_to_yaml(_model, _doc)
# skip, will be handled in ref
end

super(hash)
def ref_hash(model = self)
{
"source" => model.source,
"id" => model.id,
"version" => model.version
}.compact
end

def ref=(ref)
Expand All @@ -76,14 +106,14 @@ def ref=(ref)
end
end

private
def plain?
(source && id && version).nil?
end

def ref_to_h
if structured?
{ "source" => source, "id" => id, "version" => version }.compact
else
text
end
# Whether it is a structured ref.
# @return [Boolean]
def structured?
!plain?
end
end
end
13 changes: 2 additions & 11 deletions lib/glossarist/collection.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
# frozen_string_literal: true

# (c) Copyright 2021 Ribose Inc.
#

module Glossarist
# @todo Add support for lazy concept loading.
# @todo Consider extracting persistence backend to a separate class.
Expand Down Expand Up @@ -33,7 +28,6 @@ def each(&block)
def fetch(id)
@index[id]
end

alias :[] :fetch

# If concept with given ID is present in this collection, returns that
Expand All @@ -44,7 +38,7 @@ def fetch(id)
# Concept ID
# @return [Concept]
def fetch_or_initialize(id)
fetch(id) or store(Concept.new(id: id))
fetch(id) or store(Concept.of_yaml({ id: id }))
end

# Adds concept to the collection. If collection contains a concept with
Expand All @@ -55,7 +49,6 @@ def fetch_or_initialize(id)
def store(concept)
@index[concept.id] = concept
end

alias :<< :store

# Reads all concepts from files.
Expand All @@ -70,10 +63,8 @@ def save_concepts
@index.each_value &method(:save_concept_to_file)
end

private

def load_concept_from_file(filename)
Concept.from_h(Psych.safe_load(File.read(filename)))
Concept.from_yaml(File.read(filename))
rescue Psych::SyntaxError => e
raise Glossarist::ParseError.new(filename: filename, line: e.line)
end
Expand Down
Loading
Loading