diff --git a/lib/fhir_dstu2_models/bootstrap/model.rb b/lib/fhir_dstu2_models/bootstrap/model.rb index 3558506..f884e56 100644 --- a/lib/fhir_dstu2_models/bootstrap/model.rb +++ b/lib/fhir_dstu2_models/bootstrap/model.rb @@ -23,40 +23,39 @@ 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]) || + (!@extension.nil? && !@extension.empty? && !find_extension(@extension, method_name).first.nil?) || + (!@modifierExtension.nil? && !@modifierExtension.empty? && !find_extension(@modifierExtension, method_name).first.nil?) || + super + end + # allow two FHIR::DSTU2 models to be compared for equality def ==(other) self.class == other.class && to_hash == other.to_hash 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 @@ -329,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 2a918fc..3447f88 100644 --- a/spec/lib/fhir_models/bootstrap/model_spec.rb +++ b/spec/lib/fhir_models/bootstrap/model_spec.rb @@ -82,4 +82,30 @@ 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 + + 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