Skip to content

Commit

Permalink
lazy load fhir packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammjammjamm committed Oct 11, 2024
1 parent 07db9c0 commit a437e97
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions lib/fhir_models/bootstrap/common_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,30 @@ def self.included(base)
module ClassMethods
extend FHIR::Deprecate

def ig_resources
@ig_resource ||= @ig_loader.load
end

def cache
@cache ||= {}
end

# This method actually just sets up lazy loading to avoid having to load
# everything at start time.
def load_igs(ig_file_name)
ig_loader = FHIR::Generator::IGLoader.new(ig_file_name)
@ig_resources = ig_loader.load
@cache = {}
@ig_loader = FHIR::Generator::IGLoader.new(ig_file_name)
end

def find_structure_definition(structure_defs, target_name)
return nil if target_name.nil?
return @cache[target_name] if @cache[target_name]
return cache[target_name] if cache[target_name]

definition = structure_defs.find do |sd|
sd['id'] == target_name || sd['name'] == target_name || sd['url'] == target_name
end

@cache[target_name] = create_structure_definition(definition) if definition
@cache[target_name]
cache[target_name] = create_structure_definition(definition) if definition
cache[target_name]
end

def create_structure_definition(definition)
Expand All @@ -39,12 +47,12 @@ def create_structure_definition(definition)
# # ----------------------------------------------------------------

def primitive_types
@ig_resources.primitive_types
ig_resources.primitive_types
end
deprecate :get_primitive_types, :primitive_types

def complex_types
@ig_resources.complex_types
ig_resources.complex_types
end
deprecate :get_complex_types, :complex_types

Expand All @@ -58,7 +66,7 @@ def type_definition(type_name)
# ----------------------------------------------------------------

def resource_definitions
@ig_resources.resource_definitions
ig_resources.resource_definitions
end
deprecate :get_resource_definitions, :resource_definitions

Expand All @@ -68,15 +76,15 @@ def resource_definition(resource_name)
deprecate :get_resource_definition, :resource_definition

def extension_definition(extension_name)
find_structure_definition(@ig_resources.extension_definitions, extension_name)
find_structure_definition(ig_resources.extension_definitions, extension_name)
end
deprecate :get_extension_definition, :extension_definition

# Get the basetype (String) for a given profile or extension.
def basetype(uri)
return nil if uri.nil?

defn = @ig_resources.profiles.detect { |x| x['url'] == uri } || @ig_resources.extension_definitions.detect { |x| x['url'] == uri }
defn = ig_resources.profiles.detect { |x| x['url'] == uri } || ig_resources.extension_definitions.detect { |x| x['url'] == uri }
return nil if defn.nil?

defn['type']
Expand All @@ -85,14 +93,14 @@ def basetype(uri)

# Get the StructureDefinition for a given profile.
def profile(profile_url)
find_structure_definition(@ig_resources.profiles, profile_url)
find_structure_definition(ig_resources.profiles, profile_url)
end
deprecate :get_profile, :profile

def profiles_for_resource(resource_name)
return nil if resource_name.nil?

@ig_resources.profiles.select { |x| x['type'] == resource_name }
ig_resources.profiles.select { |x| x['type'] == resource_name }
end
deprecate :get_profiles_for_resource, :profile_for_resource

Expand All @@ -101,18 +109,18 @@ def profiles_for_resource(resource_name)
# ----------------------------------------------------------------

def valuesets
@ig_resources.get_value_sets
ig_resources.get_value_sets
end

def get_codes(url)
@ig_resources.get_codes(url)
ig_resources.get_codes(url)
end

# Why do we have this function?
def get_display(system, code)
return nil if system.nil? || code.nil?

@ig_resources.get_value_sets.each do |value_set|
ig_resources.get_value_sets.each do |value_set|
if value_set['expansion'] && value_set['expansion']['contains']
value_set['expansion']['contains'].each do |contain|
return contain['display'] if contain['system'] == system && contain['code'] == code
Expand All @@ -132,7 +140,7 @@ def get_display(system, code)
# ----------------------------------------------------------------

def self.search_parameters(type_name)
@ig_resources.get_search_parameters(type_name)
ig_resources.get_search_parameters(type_name)
end
deprecate :get_search_parameters, :search_parameters
end
Expand Down

0 comments on commit a437e97

Please sign in to comment.