Module: OpenehrRails::Fhir::ResourceRegistry

Defined in:
lib/openehr_rails/fhir/resource_registry.rb

Overview

Tracks which ActiveRecord models (those including Storable) expose which openEHR archetypes as FHIR resources. Populated automatically by Storable's included hook, so no generated registration code is needed. Model names are stored as strings to survive code reloading in development.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

ARCHETYPE_SYSTEM =
'http://openehr.org/ckm/archetypes'

Class Method Summary collapse

Class Method Details

.discover_from_templatesObject

Models autoload lazily, so the Storable included hook may not have run yet. Seed the model names from the operational templates in the registry and let safe_constantize trigger autoloading.



45
46
47
48
49
50
51
52
53
54
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 45

def discover_from_templates
  registry_model = 'OpenehrTemplate'.safe_constantize
  return unless registry_model.respond_to?(:operational)

  registry_model.operational.each do |template|
    register_model(OpenehrRails::Naming.model_name(template.template_id).camelize)
  end
rescue StandardError
  nil
end

.entriesObject



56
57
58
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 56

def entries
  models.flat_map { |model| entries_for(model) }
end

.entries_for(model) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 60

def entries_for(model)
  return [] unless model.const_defined?(:FIELD_MAP)

  # Generated FIELD_MAPs key fields by name without a :name entry;
  # normalize so every consumer can rely on field[:name].
  fields_with_names = model.const_get(:FIELD_MAP).map do |name, field|
    field.merge(name: name.to_s)
  end
  fields_with_names.group_by { |f| f[:archetype_id] }
                   .map do |archetype_id, fields|
    Entry.new(
      model: model,
      archetype_id: archetype_id,
      rm_type: fields.first[:entry_rm_type],
      resource_type: TypeMap.resource_for_entry(fields.first[:entry_rm_type]),
      fields: fields
    )
  end
end

.find_by_code(archetype_id) ⇒ Object



80
81
82
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 80

def find_by_code(archetype_id)
  entries.find { |entry| entry.archetype_id == archetype_id }
end

.find_by_slug(slug) ⇒ Object



84
85
86
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 84

def find_by_slug(slug)
  entries.find { |entry| entry.slug == slug }
end

.model_namesObject



22
23
24
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 22

def model_names
  @model_names ||= []
end

.modelsObject



37
38
39
40
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 37

def models
  discover_from_templates
  model_names.filter_map { |name| name.safe_constantize }
end

.register_model(model) ⇒ Object



26
27
28
29
30
31
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 26

def register_model(model)
  name = model.is_a?(Class) ? model.name : model.to_s
  return if name.nil? || name.empty?

  model_names << name unless model_names.include?(name)
end

.reset!Object



33
34
35
# File 'lib/openehr_rails/fhir/resource_registry.rb', line 33

def reset!
  @model_names = []
end