From 527900c4b3723f8a1741a71bbeeeaba8eac32a44 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Mon, 27 Nov 2023 14:02:17 -0500 Subject: [PATCH 1/8] respond_to_missing? implemented for value[x] elements --- lib/fhir_dstu2_models/bootstrap/model.rb | 4 ++++ spec/lib/fhir_models/bootstrap/model_spec.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/fhir_dstu2_models/bootstrap/model.rb b/lib/fhir_dstu2_models/bootstrap/model.rb index 3558506..0ac59ca 100644 --- a/lib/fhir_dstu2_models/bootstrap/model.rb +++ b/lib/fhir_dstu2_models/bootstrap/model.rb @@ -23,6 +23,10 @@ def hash to_hash.hash end + def respond_to_missing?(method_name, *) + defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s] || super + end + # allow two FHIR::DSTU2 models to be compared for equality def ==(other) self.class == other.class && to_hash == other.to_hash diff --git a/spec/lib/fhir_models/bootstrap/model_spec.rb b/spec/lib/fhir_models/bootstrap/model_spec.rb index 2a918fc..083ed63 100644 --- a/spec/lib/fhir_models/bootstrap/model_spec.rb +++ b/spec/lib/fhir_models/bootstrap/model_spec.rb @@ -82,4 +82,17 @@ end end + + describe '#respond_to?' do + it 'yields true for :value on choice elements' do + extension = FHIR::Extension.new(valueInteger: 5) + expect(extension.value).to eq(5) + expect(extension.respond_to? :value).to be_truthy + end + + it 'yields false for :value on nonchoice elements' do + range = FHIR::Range.new(low: FHIR::Quantity.new(value: 18)) + expect(range.respond_to? :value).to eq(false) + end + end end From f3f39a6e48fc64006797035aa243f7b366299383 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Wed, 27 Dec 2023 09:27:04 -0500 Subject: [PATCH 2/8] Add other branches to respond_to_missing --- lib/fhir_dstu2_models/bootstrap/model.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/fhir_dstu2_models/bootstrap/model.rb b/lib/fhir_dstu2_models/bootstrap/model.rb index 0ac59ca..f381a8f 100644 --- a/lib/fhir_dstu2_models/bootstrap/model.rb +++ b/lib/fhir_dstu2_models/bootstrap/model.rb @@ -24,7 +24,10 @@ def hash end def respond_to_missing?(method_name, *) - defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s] || super + defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s] || + !@extension.nil? && !@extension.empty? || + !@modifierExtension.nil? && !@modifierExtension.empty? || + super end # allow two FHIR::DSTU2 models to be compared for equality From 7c145880be7fa84029f86b2cf82647d15620fcf8 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Wed, 27 Dec 2023 09:27:36 -0500 Subject: [PATCH 3/8] Change spec test description --- spec/lib/fhir_models/bootstrap/model_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/lib/fhir_models/bootstrap/model_spec.rb b/spec/lib/fhir_models/bootstrap/model_spec.rb index 083ed63..0c36e37 100644 --- a/spec/lib/fhir_models/bootstrap/model_spec.rb +++ b/spec/lib/fhir_models/bootstrap/model_spec.rb @@ -84,13 +84,13 @@ end describe '#respond_to?' do - it 'yields true for :value on choice elements' do + it 'returns true for :value on choice elements' do extension = FHIR::Extension.new(valueInteger: 5) expect(extension.value).to eq(5) expect(extension.respond_to? :value).to be_truthy end - it 'yields false for :value on nonchoice elements' do + it 'returns false for :value on nonchoice elements' do range = FHIR::Range.new(low: FHIR::Quantity.new(value: 18)) expect(range.respond_to? :value).to eq(false) end From 0b9b35c2951e92d34f3ea259a308fea06c2e4ba4 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Wed, 27 Dec 2023 09:35:16 -0500 Subject: [PATCH 4/8] Lint --- lib/fhir_dstu2_models/bootstrap/model.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fhir_dstu2_models/bootstrap/model.rb b/lib/fhir_dstu2_models/bootstrap/model.rb index f381a8f..bd11f83 100644 --- a/lib/fhir_dstu2_models/bootstrap/model.rb +++ b/lib/fhir_dstu2_models/bootstrap/model.rb @@ -25,9 +25,9 @@ def hash def respond_to_missing?(method_name, *) defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s] || - !@extension.nil? && !@extension.empty? || - !@modifierExtension.nil? && !@modifierExtension.empty? || - super + !@extension.nil? && !@extension.empty? || + !@modifierExtension.nil? && !@modifierExtension.empty? || + super end # allow two FHIR::DSTU2 models to be compared for equality From c8a978f58348ac7fffe9aee73cfe2255198ab9e5 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Wed, 27 Dec 2023 13:10:07 -0500 Subject: [PATCH 5/8] Adjust spec test --- spec/lib/fhir_models/bootstrap/model_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/lib/fhir_models/bootstrap/model_spec.rb b/spec/lib/fhir_models/bootstrap/model_spec.rb index 0c36e37..4dd9e80 100644 --- a/spec/lib/fhir_models/bootstrap/model_spec.rb +++ b/spec/lib/fhir_models/bootstrap/model_spec.rb @@ -85,13 +85,13 @@ describe '#respond_to?' do it 'returns true for :value on choice elements' do - extension = FHIR::Extension.new(valueInteger: 5) + extension = FHIR::DSTU2::Extension.new(valueInteger: 5) expect(extension.value).to eq(5) expect(extension.respond_to? :value).to be_truthy end it 'returns false for :value on nonchoice elements' do - range = FHIR::Range.new(low: FHIR::Quantity.new(value: 18)) + range = FHIR::DSTU2::Range.new(low: {value: 18}) expect(range.respond_to? :value).to eq(false) end end From f28aed8a117b1ce9cb5f8445f4ace61d1b53c7bf Mon Sep 17 00:00:00 2001 From: 360dgries Date: Wed, 27 Dec 2023 13:48:02 -0500 Subject: [PATCH 6/8] Add guard against to_ary --- lib/fhir_dstu2_models/bootstrap/model.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fhir_dstu2_models/bootstrap/model.rb b/lib/fhir_dstu2_models/bootstrap/model.rb index bd11f83..a4a6a96 100644 --- a/lib/fhir_dstu2_models/bootstrap/model.rb +++ b/lib/fhir_dstu2_models/bootstrap/model.rb @@ -24,9 +24,9 @@ def hash end def respond_to_missing?(method_name, *) - defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s] || - !@extension.nil? && !@extension.empty? || - !@modifierExtension.nil? && !@modifierExtension.empty? || + (defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s]) || + (!@extension.nil? && !@extension.empty? && !@extension.respond_to?(:to_ary)) || + (!@modifierExtension.nil? && !@modifierExtension.empty? && !@modifierExtension.respond_to?(:to_ary)) || super end From 126edd203af11c35f8ca48b3bfa5b1f6f3b03920 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Wed, 7 Feb 2024 09:17:25 -0500 Subject: [PATCH 7/8] add respond_to_missing? --- lib/fhir_dstu2_models/bootstrap/model.rb | 40 ++++++++++---------- spec/lib/fhir_models/bootstrap/model_spec.rb | 13 +++++++ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/lib/fhir_dstu2_models/bootstrap/model.rb b/lib/fhir_dstu2_models/bootstrap/model.rb index a4a6a96..0bee98e 100644 --- a/lib/fhir_dstu2_models/bootstrap/model.rb +++ b/lib/fhir_dstu2_models/bootstrap/model.rb @@ -36,34 +36,26 @@ def ==(other) end alias eql? == - def method_missing(method, *_args, &_block) - if defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method.to_s] - self.class::MULTIPLE_TYPES[method.to_s].each do |type| + def method_missing(method_name, *_args, &_block) + if defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s] + self.class::MULTIPLE_TYPES[method_name.to_s].each do |type| type[0] = type[0].upcase - value = send("#{method}#{type}".to_sym) + value = send("#{method_name}#{type}".to_sym) return value unless value.nil? end return nil elsif !@extension.nil? && !@extension.empty? - ext = @extension.select do |x| - name = x.url.tr('-', '_').split('/').last - anchor = name.split('#').last - (method.to_s == name || method.to_s == anchor) - end - unless ext.first.nil? - return ext.first.value.nil? ? ext.first : ext.first.value + desired_extension = find_extension(@extension, method_name) + unless desired_extension.first.nil? + return desired_extension.first.value.nil? ? desired_extension.first : desired_extension.first.value end elsif !@modifierExtension.nil? && !@modifierExtension.empty? - ext = @modifierExtension.select do |x| - name = x.url.tr('-', '_').split('/').last - anchor = name.split('#').last - (method.to_s == name || method.to_s == anchor) - end - unless ext.first.nil? - return ext.first.value.nil? ? ext.first : ext.first.value + desired_extension = find_extension(@modifierExtension, method_name) + unless desired_extension.first.nil? + return desired_extension.first.value.nil? ? desired_extension.first : desired_extension.first.value end end - raise NoMethodError.new("undefined method `#{method}' for #{self.class.name}", method) + raise NoMethodError.new("undefined method `#{method_name}' for #{self.class.name}", method_name) end def to_reference @@ -336,7 +328,15 @@ def each_element(path = nil, &block) self end - private :validate_reference_type, :check_binding_uri, :validate_field + def find_extension(extension_source, method_name) + extension_source.select do |extension| + name = extension.url.tr('-', '_').split('/').last + anchor = name.split('#').last + (method_name.to_s == name || method_name.to_s == anchor) + end + end + + private :validate_reference_type, :check_binding_uri, :validate_field, :find_extension end end end diff --git a/spec/lib/fhir_models/bootstrap/model_spec.rb b/spec/lib/fhir_models/bootstrap/model_spec.rb index 4dd9e80..3447f88 100644 --- a/spec/lib/fhir_models/bootstrap/model_spec.rb +++ b/spec/lib/fhir_models/bootstrap/model_spec.rb @@ -95,4 +95,17 @@ expect(range.respond_to? :value).to eq(false) end end + + describe '#respond_to?' do + it 'returns true for :value on choice elements' do + extension = FHIR::DSTU2::Extension.new(valueInteger: 5) + expect(extension.value).to eq(5) + expect(extension.respond_to? :value).to be_truthy + end + + it 'returns false for :value on nonchoice elements' do + range = FHIR::DSTU2::Range.new(low: {value: 18}) + expect(range.respond_to? :value).to eq(false) + end + end end From f0140625bbe3230c31c88d54fdfe43a546926669 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Wed, 7 Feb 2024 09:32:16 -0500 Subject: [PATCH 8/8] update respond_to_missing? --- lib/fhir_dstu2_models/bootstrap/model.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fhir_dstu2_models/bootstrap/model.rb b/lib/fhir_dstu2_models/bootstrap/model.rb index 0bee98e..f884e56 100644 --- a/lib/fhir_dstu2_models/bootstrap/model.rb +++ b/lib/fhir_dstu2_models/bootstrap/model.rb @@ -25,8 +25,8 @@ def hash def respond_to_missing?(method_name, *) (defined?(self.class::MULTIPLE_TYPES) && self.class::MULTIPLE_TYPES[method_name.to_s]) || - (!@extension.nil? && !@extension.empty? && !@extension.respond_to?(:to_ary)) || - (!@modifierExtension.nil? && !@modifierExtension.empty? && !@modifierExtension.respond_to?(:to_ary)) || + (!@extension.nil? && !@extension.empty? && !find_extension(@extension, method_name).first.nil?) || + (!@modifierExtension.nil? && !@modifierExtension.empty? && !find_extension(@modifierExtension, method_name).first.nil?) || super end