This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
generator_base.rb
95 lines (77 loc) · 2.61 KB
/
generator_base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true
require 'erb'
require 'pry'
require 'fileutils'
require 'net/http'
require 'fhir_models'
module Inferno
module Generator
class Base
attr_accessor :path, :extras, :resource_by_path, :resources_by_type,
:claimed_test_id_prefixes
def initialize(path, extras)
@path = path
@extras = extras
@claimed_test_id_prefixes = Set.new
load_resources
end
def load_resources
@resource_by_path = Hash.new {}
@resources_by_type = Hash.new { |h, k| h[k] = [] }
Dir.glob("#{resource_file_path}/**/*.*") do |resource| # note one extra "*"
if File.file?(resource) && resource.end_with?('json', 'xml')
# We should consider using the native Ruby models instead of JSON
# There were problems with round-tripping certain SearchParameters though
begin
new_resource_json = JSON.parse(File.read(resource))
rescue JSON::ParserError # failed to parse json
next
end
new_resource = FHIR.from_contents(File.read(resource))
next unless new_resource.present?
resource_by_path[resource_path(new_resource)] = new_resource_json
type = new_resource.class.name.demodulize
type = 'CapabilityStatement' if type == 'Conformance'
resources_by_type[type].push(new_resource_json)
end
end
end
def ig_resource
resources_by_type['ImplementationGuide'].first
end
def capability_statement(mode = 'server')
resources_by_type['CapabilityStatement'].find do |capability_statement_resource|
capability_statement_resource['rest'].any? { |r| r['mode'] == mode }
end
end
def resource_path(resource)
"#{resource.class.name.demodulize}/#{resource.id}"
end
def format_output
system("rubocop -x --display-only-fail-level-offenses #{sequence_out_path}")
end
def run
generate
format_output
end
# subclass must implement the following
def generate
raise StandardError('Method not implemented.')
end
def sequence_prefix
version = ig_resource['version'].delete('.')
name = ig_metadata['name']
"#{version}#{name}"
end
def module_yml_out_path
File.expand_path('./lib/modules/')
end
def sequence_out_path
File.expand_path("#{module_yml_out_path}/#{path}")
end
def resource_file_path
File.expand_path("./resources/#{path}")
end
end
end
end