-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5844a6b
feat: add lutaml model
HassanAkbar 2ef5bb6
chore: updated namespace structure
HassanAkbar 8a8a4c0
chore: code refactored and unused code removed
HassanAkbar 624a059
Chore: code refactored
HassanAkbar 11b30cc
chore: unused code reverted
HassanAkbar d86824f
chore: removed unused code
HassanAkbar c3dcec2
added lutaml-model version
HassanAkbar 2e63816
updated relaton cache
HassanAkbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Glossarist | ||
module LutamlModel | ||
class Asset < Lutaml::Model::Serializable | ||
attribute :path, :string | ||
|
||
yaml do | ||
map :path, to: :path | ||
end | ||
|
||
def eql?(asset) | ||
path == asset.path | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
module Glossarist | ||
module LutamlModel | ||
class Citation < Lutaml::Model::Serializable | ||
# Unstructured (plain text) reference. | ||
# @return [String] | ||
attribute :text, :string | ||
|
||
# Source in structured reference. | ||
# @return [String] | ||
attribute :source, :string | ||
|
||
# Document ID in structured reference. | ||
# @return [String] | ||
attribute :id, :string | ||
|
||
# Document version in structured reference. | ||
# @return [String] | ||
attribute :version, :string | ||
|
||
# @return [String] | ||
# Referred clause of the document. | ||
attribute :clause, :string | ||
|
||
# Link to document. | ||
# @return [String] | ||
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. | ||
attribute :original, :string | ||
|
||
attribute :ref, :string | ||
|
||
yaml do | ||
map :id, to: :id, with: { from: :id_from_hash, to: :id_to_hash } | ||
map :text, to: :text, with: { from: :text_from_hash, to: :text_to_hash } | ||
map :source, to: :source, with: { from: :source_from_hash, to: :source_to_hash } | ||
map :version, to: :version, with: { from: :version_from_hash, to: :version_to_hash } | ||
map :ref, to: :ref, with: { from: :ref_from_hash, to: :ref_to_hash } | ||
|
||
map :clause, to: :clause | ||
map :link, to: :link | ||
map :original, to: :original | ||
end | ||
|
||
def ref_from_hash(model, value) | ||
model.ref = value | ||
end | ||
|
||
def ref_to_hash(model, doc) | ||
doc["ref"] = if model.structured? | ||
ref_hash(model) | ||
else | ||
model.text | ||
end | ||
end | ||
|
||
def id_from_hash(model, value) | ||
model.id = value | ||
end | ||
|
||
def id_to_hash(_model, _doc) | ||
# skip, will be handled in ref | ||
end | ||
|
||
def text_from_hash(model, value) | ||
model.text = value | ||
end | ||
|
||
def text_to_hash(_model, _doc) | ||
# skip, will be handled in ref | ||
end | ||
|
||
def source_from_hash(model, value) | ||
model.source = value | ||
end | ||
|
||
def source_to_hash(_model, _doc) | ||
# skip, will be handled in ref | ||
end | ||
|
||
def version_from_hash(model, value) | ||
model.version = value | ||
end | ||
|
||
def version_to_hash(_model, _doc) | ||
# skip, will be handled in ref | ||
end | ||
|
||
def ref_hash(model = self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need a special method to convert into hash? We should just use the default lutaml-model methods. |
||
{ | ||
"source" => model.source, | ||
"id" => model.id, | ||
"version" => model.version | ||
}.compact | ||
end | ||
|
||
def ref=(ref) | ||
if ref.is_a?(Hash) | ||
@source = ref["source"] | ||
@id = ref["id"] | ||
@version = ref["version"] | ||
else | ||
@text = ref | ||
end | ||
end | ||
|
||
def plain? | ||
(source && id && version).nil? | ||
end | ||
|
||
# Whether it is a structured ref. | ||
# @return [Boolean] | ||
def structured? | ||
!plain? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Glossarist | ||
module LutamlModel | ||
class Collection < Lutaml::Model::Serializable | ||
include Enumerable | ||
|
||
attribute :path, :string | ||
attribute :index, :hash, default: -> { {} } | ||
|
||
yaml do | ||
map :path, to: :path | ||
map :index, to: :index, render_default: true | ||
end | ||
|
||
def each(&block) | ||
@index.each_value(&block) | ||
end | ||
|
||
def fetch(id) | ||
@index[id] | ||
end | ||
alias :[] :fetch | ||
|
||
def fetch_or_initialize(id) | ||
fetch(id) or store(Concept.of_yaml({ id: id })) | ||
end | ||
|
||
def store(concept) | ||
@index[concept.id] = concept | ||
end | ||
alias :<< :store | ||
|
||
def load_concepts | ||
Dir.glob(concepts_glob) do |filename| | ||
store(load_concept_from_file(filename)) | ||
end | ||
end | ||
|
||
# Writes all concepts to files. | ||
def save_concepts | ||
@index.each_value &method(:save_concept_to_file) | ||
end | ||
|
||
def load_concept_from_file(filename) | ||
Concept.from_yaml(File.read(filename)) | ||
end | ||
|
||
def save_concept_to_file(concept) | ||
filename = File.join(path, "concept-#{concept.id}.yaml") | ||
File.write(filename, Psych.dump(concept.to_h)) | ||
end | ||
|
||
def concepts_glob | ||
File.join(path, "concept-*.{yaml,yml}") | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HassanAkbar why do we have to use a special namespace for these? Why not just replace the classes in the original namespace?