Skip to content

Commit

Permalink
Chore: code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
HassanAkbar committed Dec 23, 2024
1 parent 8a8a4c0 commit 624a059
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 66 deletions.
2 changes: 0 additions & 2 deletions lib/glossarist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
require_relative "glossarist/managed_concept_collection"
require_relative "glossarist/non_verb_rep"

require_relative "glossarist/v1_reader"

require_relative "glossarist/collections"

require_relative "glossarist/config"
Expand Down
30 changes: 15 additions & 15 deletions lib/glossarist/citation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,58 +33,58 @@ class Citation < Lutaml::Model::Serializable
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 :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

def ref_from_hash(model, value)
def ref_from_yaml(model, value)
model.ref = value
end

def ref_to_hash(model, doc)
def ref_to_yaml(model, doc)
doc["ref"] = if model.structured?
ref_hash(model)
else
model.text
end
end

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

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

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

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

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

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

def version_from_hash(model, value)
def version_from_yaml(model, value)
model.version = value
end

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

Expand Down
34 changes: 27 additions & 7 deletions lib/glossarist/collection.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
module Glossarist
class Collection < Lutaml::Model::Serializable
# @todo Add support for lazy concept loading.
# @todo Consider extracting persistence backend to a separate class.
class Collection
include Enumerable

attribute :path, :string
attribute :index, :hash, default: -> { {} }

yaml do
map :path, to: :path
map :index, to: :index, render_default: true
def initialize(path: nil)
@path = path
@index = {}
end

def each(&block)
@index.each_value(&block)
end

# Returns concept with given ID, if it is present in collection, or +nil+
# otherwise.
#
# @param id [String]
# Concept ID
# @return [Concept, nil]
def fetch(id)
@index[id]
end
alias :[] :fetch

# If concept with given ID is present in this collection, returns that
# concept. Otherwise, instantiates a new concept, adds it to
# the collection, and returns it.
#
# @param id [String]
# Concept ID
# @return [Concept]
def fetch_or_initialize(id)
fetch(id) or store(Concept.of_yaml({ id: id }))
end

# Adds concept to the collection. If collection contains a concept with
# the same ID already, that concept is replaced.
#
# @param concept [Concept]
# concept about to be added
def store(concept)
@index[concept.id] = concept
end
alias :<< :store

# Reads all concepts from files.
def load_concepts
Dir.glob(concepts_glob) do |filename|
store(load_concept_from_file(filename))
Expand All @@ -41,6 +59,8 @@ def save_concepts

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

def save_concept_to_file(concept)
Expand Down
14 changes: 7 additions & 7 deletions lib/glossarist/concept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Concept < Lutaml::Model::Serializable
map :extension_attributes, to: :extension_attributes

map :date_accepted, with: { from: :date_accepted_from_yaml, to: :date_accepted_to_yaml }
map :uuid, to: :uuid, with: { to: :uuid_to_hash, from: :uuid_from_hash }
map :id, to: :id, with: { to: :id_to_hash, from: :id_from_hash }
map :identifier, to: :id, with: { to: :id_to_hash, from: :id_from_hash }
map :uuid, to: :uuid, with: { to: :uuid_to_yaml, from: :uuid_from_yaml }
map :id, to: :id, with: { to: :id_to_yaml, from: :id_from_yaml }
map :identifier, to: :id, with: { to: :id_to_yaml, from: :id_from_yaml }
end

def designations
Expand Down Expand Up @@ -65,18 +65,18 @@ def authoritative_source
data.authoritative_source
end

def uuid_to_hash(model, doc)
def uuid_to_yaml(model, doc)
doc["id"] = model.uuid if model.uuid
end

def uuid_from_hash(model, value)
def uuid_from_yaml(model, value)
model.uuid = value
end

def id_to_hash(model, doc)
def id_to_yaml(model, doc)
end

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

Expand Down
6 changes: 3 additions & 3 deletions lib/glossarist/concept_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ class ConceptData < Lutaml::Model::Serializable
map :notes, to: :notes, render_nil: true
map :release, to: :release
map :sources, to: :sources
map :terms, to: :terms, with: { from: :terms_from_hash, to: :terms_to_hash }
map :terms, to: :terms, with: { from: :terms_from_yaml, to: :terms_to_yaml }
map :related, to: :related
map :domain, to: :domain
map :language_code, to: :language_code
map :entry_status, to: :entry_status
end

def terms_from_hash(model, value)
def terms_from_yaml(model, value)
model.terms = value.map { |v| Designation::Base.of_yaml(v) }
end

def terms_to_hash(model, doc)
def terms_to_yaml(model, doc)
doc["terms"] = model.terms.map(&:to_yaml_hash)
end

Expand Down
8 changes: 4 additions & 4 deletions lib/glossarist/error.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require_relative "error/invalid_type_error"
require_relative "error/invalid_language_code_error"
require_relative "error/parse_error"

module Glossarist
class Error < StandardError
end
end

require_relative "error/invalid_type_error"
require_relative "error/invalid_language_code_error"
require_relative "error/parse_error"
28 changes: 0 additions & 28 deletions lib/glossarist/v1_reader.rb

This file was deleted.

0 comments on commit 624a059

Please sign in to comment.