Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FI-1427 Add respond_to_missing? #111

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ engines:
enabled: true
rubocop:
enabled: true
channel: rubocop-1-23-0
channel: rubocop-0-80
ratings:
paths:
- "**.inc"
Expand Down
47 changes: 27 additions & 20 deletions lib/fhir_models/bootstrap/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,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]) ||
([email protected]? && [email protected]? && !find_extension(@extension, method_name).first.nil?) ||
([email protected]? && [email protected]? && !find_extension(@modifierExtension, method_name).first.nil?) ||
super
end

# allow two FHIR 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 [email protected]? && [email protected]?
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 [email protected]? && [email protected]?
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
Expand Down Expand Up @@ -325,6 +324,14 @@ 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
14 changes: 14 additions & 0 deletions spec/lib/fhir_models/bootstrap/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,18 @@
expect(range.low.value).to equal(18)
end
end

describe '#respond_to?' 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 '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
end

end
Loading