Class: Openphar::Repositories::MonographRepository

Inherits:
Object
  • Object
show all
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.

Examples:

Loading and querying monographs

repo = MonographRepository.instance
repo.load_directory("./data/jp/crude-drugs")

# Find by ID
monograph = repo.find("https://www.openphar.org/data/jp/crude-drugs/ginger")

# Find by CAS number
monograph = repo.find_by_cas("50-00-0")

# Get all monographs for a publisher
jp_monographs = repo.all_for_publisher("JP")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMonographRepository

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

#cacheHash<String, Lutaml::Model::Serializable> (readonly)

Returns Cache of id => monograph.

Returns:

  • (Hash<String, Lutaml::Model::Serializable>)

    Cache of id => monograph



34
35
36
# File 'lib/openphar/repositories/monograph_repository.rb', line 34

def cache
  @cache
end

#indexesHash (readonly)

Returns Indexes for fast lookups.

Returns:

  • (Hash)

    Indexes for fast lookups



37
38
39
# File 'lib/openphar/repositories/monograph_repository.rb', line 37

def indexes
  @indexes
end

Instance Method Details

#allArray<Lutaml::Model::Serializable>

All cached monographs.

Returns:

  • (Array<Lutaml::Model::Serializable>)


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.

Parameters:

  • edition_id (String)

    The edition @id

Returns:

  • (Array<Lutaml::Model::Serializable>)

    Array of monographs



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.

Parameters:

  • code (String)

    The publisher code (e.g., "JP", "WHO")

Returns:

  • (Array<Lutaml::Model::Serializable>)

    Array of monographs



153
154
155
# File 'lib/openphar/repositories/monograph_repository.rb', line 153

def all_for_publisher(code)
  @indexes[:publisher][code.to_s.upcase] || []
end

#clearvoid

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

#countInteger

Count of all cached monographs.

Returns:

  • (Integer)


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.

Yields:

  • (Lutaml::Model::Serializable)

    Each monograph

Returns:

  • (Enumerator)

    If no block given



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.

Parameters:

  • id (String)

    The monograph's @id

Returns:

  • (Boolean)


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.

Parameters:

  • id (String)

    The monograph's @id

Returns:

  • (Lutaml::Model::Serializable, nil)

    The monograph or nil if not found



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.

Parameters:

  • cas_number (String)

    The CAS number

Returns:

  • (Lutaml::Model::Serializable, nil)

    The monograph or nil if not found



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.

Parameters:

  • name (String)

    The Japanese name

Returns:

  • (Lutaml::Model::Serializable, nil)

    The monograph or nil if not found



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.

Parameters:

  • name (String)

    The Latin name

Returns:

  • (Lutaml::Model::Serializable, nil)

    The monograph or nil if not found



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.

Parameters:

  • slug (String)

    The URL slug

Returns:

  • (Lutaml::Model::Serializable, nil)

    The monograph or nil if not found



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

#idsArray<String>

Get monograph IDs for fuzzy matching.

Returns:

  • (Array<String>)

    All monograph IDs



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.

Parameters:

  • path (String)

    Directory containing JSON-LD files

  • publisher (String, nil) (defaults to: nil)

    Optional publisher code to filter by

Returns:

  • (Integer)

    Number of monographs loaded



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.

Parameters:

  • path (String)

    Path to the JSON-LD file

  • publisher (String, nil) (defaults to: nil)

    Optional publisher code

Returns:

  • (Lutaml::Model::Serializable, nil)

    The loaded monograph or nil on failure



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.message}"
  nil
rescue StandardError => e
  warn "Warning: Error loading #{path}: #{e.message}"
  nil
end

#register(monograph) ⇒ Lutaml::Model::Serializable

Register a monograph in the cache and indexes.

Parameters:

  • monograph (Lutaml::Model::Serializable)

    The monograph to register

Returns:

  • (Lutaml::Model::Serializable)

    The registered monograph



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.

Yields:

  • (Lutaml::Model::Serializable)

    Each monograph

Returns:

  • (Array<Lutaml::Model::Serializable>)

    Matching monographs



179
180
181
# File 'lib/openphar/repositories/monograph_repository.rb', line 179

def select(&block)
  @cache.values.select(&block)
end