fhir_scorecard 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f2da74ec899e9c534a4a76c9aaca39fd1299dcaf
4
+ data.tar.gz: 874605dc5bf4f111daacb081c5acecb58b6e2844
5
+ SHA512:
6
+ metadata.gz: acfc9c5150c2e3ff71d928aa16dacdc19a56f978b2e9b3a4ed6ecd218001d165f66fe73d70963ca2ed09970f07c368a08265a1b38108a7f02e878fddf171a3d1
7
+ data.tar.gz: 8997f37fe500f1f4074f136d260c1892dac1c4879361177395e6c351764204df54e8d1e5f5ef48faeeb055163fd72f544a64929467d2c398445cb5511e17b849
data/.gitignore ADDED
@@ -0,0 +1,56 @@
1
+ terminology/*
2
+ logs/*.log*
3
+ **/.DS_Store
4
+
5
+ ### Following content generated by Github ###
6
+
7
+ *.gem
8
+ *.rbc
9
+ /.config
10
+ /coverage/
11
+ /InstalledFiles
12
+ /pkg/
13
+ /spec/reports/
14
+ /spec/examples.txt
15
+ /test/tmp/
16
+ /test/version_tmp/
17
+ /tmp/
18
+
19
+ # Used by dotenv library to load environment variables.
20
+ # .env
21
+
22
+ ## Specific to RubyMotion:
23
+ .dat*
24
+ .repl_history
25
+ build/
26
+ *.bridgesupport
27
+ build-iPhoneOS/
28
+ build-iPhoneSimulator/
29
+
30
+ ## Specific to RubyMotion (use of CocoaPods):
31
+ #
32
+ # We recommend against adding the Pods directory to your .gitignore. However
33
+ # you should judge for yourself, the pros and cons are mentioned at:
34
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
35
+ #
36
+ # vendor/Pods/
37
+
38
+ ## Documentation cache and generated files:
39
+ /.yardoc/
40
+ /_yardoc/
41
+ /doc/
42
+ /rdoc/
43
+
44
+ ## Environment normalization:
45
+ /.bundle/
46
+ /vendor/bundle
47
+ /lib/bundler/man/
48
+
49
+ # for a library or gem, you might want to ignore these files since the code is
50
+ # intended to run in multiple environments; otherwise, check them in:
51
+ # Gemfile.lock
52
+ # .ruby-version
53
+ # .ruby-gemset
54
+
55
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
56
+ .rvmrc
data/.simplecov ADDED
@@ -0,0 +1,16 @@
1
+ SimpleCov.command_name 'Unit Tests'
2
+ SimpleCov.start do
3
+ add_filter "test/"
4
+ add_group "Library", "lib"
5
+ end
6
+
7
+ class SimpleCov::Formatter::QualityFormatter
8
+ def format(result)
9
+ SimpleCov::Formatter::HTMLFormatter.new.format(result)
10
+ File.open("coverage/covered_percent", "w") do |f|
11
+ f.puts result.source_files.covered_percent.to_f
12
+ end
13
+ end
14
+ end
15
+
16
+ SimpleCov.formatter = SimpleCov::Formatter::QualityFormatter
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0.0"
4
+ script:
5
+ - bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ gem 'rake'
5
+ gem 'pry'
6
+ gem 'fhir_models', '~> 1.6'
7
+
8
+ group :test do
9
+ gem 'simplecov', :require => false
10
+ gem 'minitest', '~> 5.3'
11
+ gem 'minitest-reporters'
12
+ gem 'awesome_print', :require => 'ap'
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fhir_scorecard (0.1.0)
5
+ fhir_models
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ansi (1.5.0)
11
+ awesome_print (1.7.0)
12
+ bcp47 (0.3.3)
13
+ i18n
14
+ builder (3.2.2)
15
+ coderay (1.1.1)
16
+ date_time_precision (0.8.1)
17
+ docile (1.1.5)
18
+ fhir_models (1.6.8)
19
+ bcp47 (>= 0.3)
20
+ date_time_precision (>= 0.8)
21
+ mime-types (>= 1.16, < 3)
22
+ nokogiri (>= 1.6)
23
+ i18n (0.7.0)
24
+ json (2.0.2)
25
+ method_source (0.8.2)
26
+ mime-types (2.99.3)
27
+ mini_portile2 (2.1.0)
28
+ minitest (5.9.0)
29
+ minitest-reporters (1.1.11)
30
+ ansi
31
+ builder
32
+ minitest (>= 5.0)
33
+ ruby-progressbar
34
+ nokogiri (1.6.8.1)
35
+ mini_portile2 (~> 2.1.0)
36
+ pry (0.10.4)
37
+ coderay (~> 1.1.0)
38
+ method_source (~> 0.8.1)
39
+ slop (~> 3.4)
40
+ rake (11.3.0)
41
+ ruby-progressbar (1.8.1)
42
+ simplecov (0.12.0)
43
+ docile (~> 1.1.0)
44
+ json (>= 1.8, < 3)
45
+ simplecov-html (~> 0.10.0)
46
+ simplecov-html (0.10.0)
47
+ slop (3.6.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ awesome_print
54
+ fhir_models (~> 1.6)
55
+ fhir_scorecard!
56
+ minitest (~> 5.3)
57
+ minitest-reporters
58
+ pry
59
+ rake
60
+ simplecov
61
+
62
+ BUNDLED WITH
63
+ 1.13.6
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # fhir_scorecard [![Build Status](https://travis-ci.org/fhir-crucible/fhir_scorecard.svg?branch=master)](https://travis-ci.org/fhir-crucible/fhir_scorecard)
2
+
3
+ `fhir_scorecard` provides a scorecard for a FHIR Patient Record (as Bundle). Achieving interoperability
4
+ of a Patient Healthcare Record within FHIR will require agreement on "best practices" beyond the base specification.
5
+ Those agreements could be in the form of an Implementation Guide, with associated Profiles, but `fhir_scorecard`
6
+ provides them in the form of testable rubrics.
7
+
8
+ This project borrows inspiration and rubrics from [ccdaScorecard](https://github.com/chb/ccdaScorecard),
9
+ a scorecard for C-CDA documents.
10
+
11
+ ## Scorecard on the Command Line
12
+
13
+ ```
14
+ $ bundle exec rake fhir:score[fhir_bundle.json]
15
+
16
+ POINTS CATEGORY MESSAGE
17
+ ------ -------- -------
18
+ 10 bundle Patient Record is a FHIR Bundle.
19
+ 10 patient Patient Record contains one FHIR Patient.
20
+ 2 codes_exist 21% (282/1323) of codes, Codings, and CodeableConcepts had values. Maximum of 10 points.
21
+ 4 codes_umls 42% (145/344) of SNOMED, LOINC, RxNorm, ICD9, and ICD10 validated against UMLS. Maximum of 10 points.
22
+ 0 descriptions 7% (26/344) of SNOMED, LOINC, RxNorm, ICD9, and ICD10 codes use preferred descriptions. Maximum of 10 points.
23
+ 17 completeness 85% of REQUIRED Resources and 9% of EXPECTED Resources were present. Maximum of 20 points.
24
+ 10 cvx_immunizations 100% (6/6) of Immunization vaccine codes use CVX. Maximum of 10 points.
25
+ 0 cvx_medications 0% (0/4) of Medication[x] Resource codes use CVX. Maximum of 10 point penalty.
26
+ 3 iso8601_dates 30% (149/494) of date/time/dateTime/instant fields were populated with reasonable iso8601 values. Maximum of 10 points.
27
+ 6 loinc_labs 68% (75/110) of Observations and DiagnosticReports validated against the LOINC Top 2000. Maximum of 10 points.
28
+ 8 ucum_quantities 83% (80/96) of physical quantities used or declared UCUM. Maximum of 10 points.
29
+ 4 references_resolve 43% (326/750) of all possible References resolved locally within the Bundle. Maximum of 10 points.
30
+ 10 rxnorm_meds 100% (4/4) of Medication[x] Resource codes use RxNorm. Maximum of 10 points.
31
+ 0 smoking_status The Patient Record should include Smoking Status (DAF-core-smokingstatus profile on Observation).
32
+ 2 snomed_core 29% (5/17) of Condition Resource codes use the SNOMED Core Subset. Maximum of 10 points.
33
+ 2 vital_signs 22% (2/9) of Vital Signs had at least one recorded Observation. Maximum of 10 points.
34
+ ------
35
+ 88 TOTAL
36
+
37
+
38
+ ```
39
+
40
+ ## Scorecard in Ruby
41
+
42
+ ```ruby
43
+ bundle_json = "{\"entry\":[{\"resource\":{\"resourceType\":\"Patient\"}}],\"resourceType\":\"Bundle\"}"
44
+ scorecard = FHIR::Scorecard.new
45
+ report = scorecard.score(bundle_json)
46
+ => {:bundle=>{:points=>10, :message=>"Patient Record is a FHIR Bundle."},
47
+ :patient=>{:points=>10, :message=>"Patient Record contains one FHIR Patient."},
48
+ :points=>20}
49
+ ```
50
+
51
+ ## Optional Terminology Support
52
+
53
+ The scorecard requires some terminology files to operate several rubrics related
54
+ to codes. Download the files (requires accounts) and place them in `./terminology`
55
+
56
+ - https://www.nlm.nih.gov/research/umls/licensedcontent/umlsknowledgesources.html
57
+ - When installing the metathesaurus, include the following sources:
58
+ `CVX|CVX;
59
+ ICD10CM|ICD10CM;
60
+ ICD10PCS|ICD10PCS;
61
+ ICD9CM|ICD9CM;
62
+ LNC|LNC;
63
+ MTHICD9|ICD9CM;
64
+ RXNORM|RXNORM;
65
+ SNOMEDCT_US|SNOMEDCT`
66
+
67
+ - https://www.nlm.nih.gov/research/umls/Snomed/core_subset.html
68
+ - http://loinc.org/usage/obs/loinc-top-2000-plus-loinc-lab-observations-us.csv
69
+ - http://download.hl7.de/documents/ucum/concepts.tsv
70
+
71
+ After downloading the files, run these rake tasks to post-process each terminology file:
72
+
73
+ ```
74
+ > bundle exec rake fhir:process_umls
75
+ > bundle exec rake fhir:process_snomed
76
+ > bundle exec rake fhir:process_loinc
77
+ > bundle exec rake fhir:process_ucum
78
+ ```
79
+
80
+ # License
81
+
82
+ Copyright 2016 The MITRE Corporation
83
+
84
+ Licensed under the Apache License, Version 2.0 (the "License");
85
+ you may not use this file except in compliance with the License.
86
+ You may obtain a copy of the License at
87
+
88
+ http://www.apache.org/licenses/LICENSE-2.0
89
+
90
+ Unless required by applicable law or agreed to in writing, software
91
+ distributed under the License is distributed on an "AS IS" BASIS,
92
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93
+ See the License for the specific language governing permissions and
94
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'fileutils'
4
+ require 'pry'
5
+
6
+ require_relative 'lib/fhir_scorecard'
7
+
8
+ # Pull in any rake task defined in lib/tasks
9
+ Dir['lib/tasks/**/*.rake'].sort.each do |ext|
10
+ load ext
11
+ end
12
+
13
+ desc "Run basic tests"
14
+ Rake::TestTask.new(:test_unit) do |t|
15
+ t.libs << "test"
16
+ t.test_files = FileList['test/**/*_test.rb']
17
+ t.verbose = true
18
+ t.warning = false
19
+ end
20
+
21
+ task :test => [:test_unit] do
22
+ system("open coverage/index.html")
23
+ end
24
+
25
+ task :default => [:test]
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "fhir_scorecard"
5
+ s.summary = "Scorecard for a FHIR Patient Record (as Bundle)"
6
+ s.description = "A Gem for scoring FHIR Patient Records (as Bundles)"
7
+ s.email = "jwalonoski@mitre.org"
8
+ s.homepage = "https://github.com/fhir-crucible/fhir_scorecard"
9
+ s.authors = ["Jason Walonoski"]
10
+ s.version = '1.0'
11
+
12
+ s.files = s.files = `git ls-files`.split("\n")
13
+
14
+ s.add_runtime_dependency 'fhir_models'
15
+ end
@@ -0,0 +1,15 @@
1
+ # Top level include file that brings in all the necessary code
2
+ require 'bundler/setup'
3
+ require 'fhir_models'
4
+
5
+ root = File.expand_path '..', File.dirname(File.absolute_path(__FILE__))
6
+
7
+ FHIR.logger = Logger.new(File.join(root,'logs','fhir_models.log'),1,1024000)
8
+
9
+ require File.join(root,'lib','scorecard.rb')
10
+ require File.join(root,'lib','terminology.rb')
11
+ require File.join(root,'lib','rubrics.rb')
12
+
13
+ Dir.glob(File.join(root, 'lib','rubrics','**','*.rb')).each do |file|
14
+ require file
15
+ end
@@ -0,0 +1,72 @@
1
+ module FHIR
2
+ class CodesExist < FHIR::Rubrics
3
+
4
+ # Codes are present for all codes, Codings, and CodeableConcepts
5
+ rubric :codes_exist do |record|
6
+ results = {
7
+ :codeable_fields => 0,
8
+ :coded_fields => 0
9
+ }
10
+ resources = record.entry.map{|e|e.resource}
11
+ resources.each do |resource|
12
+ results.merge!(check_metadata(resource)){|k,a,b|a+b}
13
+ end
14
+
15
+ percentage = results[:coded_fields].to_f / results[:codeable_fields].to_f
16
+ percentage = 0.0 if percentage.nan?
17
+ points = 10.0 * percentage
18
+ message = "#{(100 * percentage).to_i}% (#{results[:coded_fields]}/#{results[:codeable_fields]}) of codes, Codings, and CodeableConcepts had values. Maximum of 10 points."
19
+ response(points.to_i,message)
20
+ end
21
+
22
+ def self.check_metadata(fhir_model)
23
+ results = {
24
+ :codeable_fields => 0,
25
+ :coded_fields => 0
26
+ }
27
+ # check for metadata
28
+ # count all codeable fields
29
+ fhir_model.class::METADATA.each do |key, meta|
30
+ field_name = meta['local_name'] || key
31
+ if ['code','Coding','CodeableConcept'].include?(meta['type'])
32
+ results[:codeable_fields] += 1
33
+ value = fhir_model.instance_variable_get("@#{field_name}")
34
+ # check that the field is actually the correct type
35
+ if !value.nil?
36
+ if value.is_a?(Array)
37
+ if !value.empty? # the Array has at least one value
38
+ results[:coded_fields] += 1 if value.any?{|x|type_okay(meta['type'],x)}
39
+ end
40
+ else # not an Array
41
+ results[:coded_fields] += 1 if type_okay(meta['type'],value)
42
+ end
43
+ end
44
+ else
45
+ value = fhir_model.instance_variable_get("@#{field_name}")
46
+ if !value.nil?
47
+ if value.is_a?(Array)
48
+ value.each{|v| results.merge!(check_metadata(v)){|k,a,b|a+b} if v.is_a?(FHIR::Model)}
49
+ else # not an Array
50
+ results.merge!(check_metadata(value)){|k,a,b|a+b} if value.is_a?(FHIR::Model)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ results
56
+ end
57
+
58
+ def self.type_okay(type,item)
59
+ case type
60
+ when 'code'
61
+ item.is_a?(String)
62
+ when 'Coding'
63
+ item.is_a?(FHIR::Coding) && !item.code.nil? && !item.system.nil?
64
+ when 'CodeableConcept'
65
+ item.is_a?(FHIR::CodeableConcept) && item.coding.any?{|c| c.is_a?(FHIR::Coding) && !c.code.nil? && !c.system.nil?}
66
+ else
67
+ false
68
+ end
69
+ end
70
+
71
+ end
72
+ end