Module: OpenehrRails::Fhir::ProfileRepository

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

Overview

Resolves StructureDefinition profiles for the FHIR endpoint. Prefers files written by --fhir under app/fhir/profiles, and otherwise builds them on the fly from the registered operational templates.

Class Method Summary collapse

Class Method Details

.all(root: ::Rails.root) ⇒ Object



11
12
13
# File 'lib/openehr_rails/fhir/profile_repository.rb', line 11

def all(root: ::Rails.root)
  from_files(root).presence || from_registry
end

.find(id, root: ::Rails.root) ⇒ Object



15
16
17
18
19
20
# File 'lib/openehr_rails/fhir/profile_repository.rb', line 15

def find(id, root: ::Rails.root)
  file = profiles_dir(root).join("#{id}.json")
  return JSON.parse(File.read(file)) if File.exist?(file)

  all(root: root).find { |profile| profile['id'] == id || profile[:id] == id }
end

.from_files(root) ⇒ Object



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

def from_files(root)
  Dir.glob(profiles_dir(root).join('*.json')).map { |path| JSON.parse(File.read(path)) }
end

.from_registryObject



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

def from_registry
  return [] unless defined?(::OpenehrTemplate)

  ::OpenehrTemplate.operational.flat_map do |template|
    opt = OpenehrRails::Opt.parse(template.content)
    ProfileGenerator.new(opt).profiles
  end
end

.profiles_dir(root) ⇒ Object



35
36
37
# File 'lib/openehr_rails/fhir/profile_repository.rb', line 35

def profiles_dir(root)
  Pathname.new(root).join('app/fhir/profiles')
end