Class: Openphar::Repositories::MonographRepository
- Inherits:
-
Object
- Object
- Openphar::Repositories::MonographRepository
- Includes:
- Singleton
- Defined in:
- lib/openphar/repositories/monograph_repository.rb
Overview
Central repository for monograph data access.
Provides:
- Caching of loaded monographs
- Multiple indexes for fast lookups
- Query interface for common operations
This replaces scattered Dir.glob and JSON.parse calls throughout the codebase with a single, consistent data access layer.
Instance Attribute Summary collapse
-
#cache ⇒ Hash<String, Lutaml::Model::Serializable>
readonly
Cache of id => monograph.
-
#indexes ⇒ Hash
readonly
Indexes for fast lookups.
Instance Method Summary collapse
-
#all ⇒ Array<Lutaml::Model::Serializable>
All cached monographs.
-
#all_for_edition(edition_id) ⇒ Array<Lutaml::Model::Serializable>
Get all monographs for a specific edition.
-
#all_for_publisher(code) ⇒ Array<Lutaml::Model::Serializable>
Get all monographs for a specific publisher.
-
#clear ⇒ void
Clear all cached data and indexes.
-
#count ⇒ Integer
Count of all cached monographs.
-
#each {|Lutaml::Model::Serializable| ... } ⇒ Enumerator
Iterate over all monographs.
-
#exists?(id) ⇒ Boolean
Check if a monograph exists.
-
#find(id) ⇒ Lutaml::Model::Serializable?
Find a monograph by its ID.
-
#find_by_cas(cas_number) ⇒ Lutaml::Model::Serializable?
Find a monograph by CAS Registry Number.
-
#find_by_japanese_name(name) ⇒ Lutaml::Model::Serializable?
Find a monograph by Japanese name.
-
#find_by_latin_name(name) ⇒ Lutaml::Model::Serializable?
Find a monograph by Latin pharmaceutical name.
-
#find_by_slug(slug) ⇒ Lutaml::Model::Serializable?
Find a monograph by URL slug.
-
#ids ⇒ Array<String>
Get monograph IDs for fuzzy matching.
-
#initialize ⇒ MonographRepository
constructor
A new instance of MonographRepository.
-
#load_directory(path, publisher: nil) ⇒ Integer
Load all monographs from a directory.
-
#load_file(path, publisher: nil) ⇒ Lutaml::Model::Serializable?
Load a single monograph file.
-
#register(monograph) ⇒ Lutaml::Model::Serializable
Register a monograph in the cache and indexes.
-
#select {|Lutaml::Model::Serializable| ... } ⇒ Array<Lutaml::Model::Serializable>
Select monographs matching a condition.
Constructor Details
#initialize ⇒ MonographRepository
Returns a new instance of MonographRepository.
39 40 41 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 39 def initialize clear end |
Instance Attribute Details
#cache ⇒ Hash<String, Lutaml::Model::Serializable> (readonly)
Returns Cache of id => monograph.
34 35 36 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 34 def cache @cache end |
#indexes ⇒ Hash (readonly)
Returns Indexes for fast lookups.
37 38 39 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 37 def indexes @indexes end |
Instance Method Details
#all ⇒ Array<Lutaml::Model::Serializable>
All cached monographs.
193 194 195 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 193 def all @cache.values end |
#all_for_edition(edition_id) ⇒ Array<Lutaml::Model::Serializable>
Get all monographs for a specific edition.
161 162 163 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 161 def all_for_edition(edition_id) @indexes[:edition][edition_id] || [] end |
#all_for_publisher(code) ⇒ Array<Lutaml::Model::Serializable>
Get all monographs for a specific publisher.
153 154 155 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 153 def all_for_publisher(code) @indexes[:publisher][code.to_s.upcase] || [] end |
#clear ⇒ void
This method returns an undefined value.
Clear all cached data and indexes.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 46 def clear @cache = {} @indexes = { cas_number: {}, latin_name: {}, japanese_name: {}, publisher: {}, edition: {}, slug: {} } end |
#count ⇒ Integer
Count of all cached monographs.
186 187 188 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 186 def count @cache.size end |
#each {|Lutaml::Model::Serializable| ... } ⇒ Enumerator
Iterate over all monographs.
169 170 171 172 173 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 169 def each(&block) return @cache.each_value unless block_given? @cache.each_value(&block) end |
#exists?(id) ⇒ Boolean
Check if a monograph exists.
201 202 203 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 201 def exists?(id) @cache.key?(id) end |
#find(id) ⇒ Lutaml::Model::Serializable?
Find a monograph by its ID.
105 106 107 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 105 def find(id) @cache[id] end |
#find_by_cas(cas_number) ⇒ Lutaml::Model::Serializable?
Find a monograph by CAS Registry Number.
113 114 115 116 117 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 113 def find_by_cas(cas_number) return nil unless cas_number @indexes[:cas_number][normalize_cas(cas_number)] end |
#find_by_japanese_name(name) ⇒ Lutaml::Model::Serializable?
Find a monograph by Japanese name.
133 134 135 136 137 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 133 def find_by_japanese_name(name) return nil unless name @indexes[:japanese_name][name.to_s.strip] end |
#find_by_latin_name(name) ⇒ Lutaml::Model::Serializable?
Find a monograph by Latin pharmaceutical name.
123 124 125 126 127 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 123 def find_by_latin_name(name) return nil unless name @indexes[:latin_name][normalize_name(name)] end |
#find_by_slug(slug) ⇒ Lutaml::Model::Serializable?
Find a monograph by URL slug.
143 144 145 146 147 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 143 def find_by_slug(slug) return nil unless slug @indexes[:slug][slug.to_s.downcase.strip] end |
#ids ⇒ Array<String>
Get monograph IDs for fuzzy matching.
208 209 210 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 208 def ids @cache.keys end |
#load_directory(path, publisher: nil) ⇒ Integer
Load all monographs from a directory.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 63 def load_directory(path, publisher: nil) files = Dir.glob(File.join(path, '**/*.jsonld')) count = 0 files.sort.each do |file| count += 1 if load_file(file, publisher: publisher) end count end |
#load_file(path, publisher: nil) ⇒ Lutaml::Model::Serializable?
Load a single monograph file.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 79 def load_file(path, publisher: nil) data = JSON.parse(File.read(path, encoding: 'UTF-8')) monograph = deserialize(data) register(monograph) if monograph rescue JSON::ParserError => e warn "Warning: Could not parse #{path}: #{e.}" nil rescue StandardError => e warn "Warning: Error loading #{path}: #{e.}" nil end |
#register(monograph) ⇒ Lutaml::Model::Serializable
Register a monograph in the cache and indexes.
95 96 97 98 99 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 95 def register(monograph) @cache[monograph.id] = monograph index_monograph(monograph) monograph end |
#select {|Lutaml::Model::Serializable| ... } ⇒ Array<Lutaml::Model::Serializable>
Select monographs matching a condition.
179 180 181 |
# File 'lib/openphar/repositories/monograph_repository.rb', line 179 def select(&block) @cache.values.select(&block) end |