Skip to content

Commit

Permalink
Merge branch 'Vermonster-add-resolve-helper'
Browse files Browse the repository at this point in the history
  • Loading branch information
jawalonoski committed Oct 10, 2016
2 parents 572a2c4 + f25273d commit 1d2a7a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/fhir_client/ext/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def destroy
nil
end

def resolve(reference)
if reference.contained?
contained.detect { |c| c.id == reference.id }
else
reference.read
end
end

private

def self.handle_response(response)
Expand Down
27 changes: 22 additions & 5 deletions lib/fhir_client/ext/reference.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
module FHIR
class Reference
def contained?
reference.to_s.start_with?('#')
end

def id
if contained?
reference.to_s[1..-1]
else
reference.to_s.split('/').last
end
end

def type
reference.to_s.split('/').first unless contained?
end

def resource_class
"FHIR::#{type}".constantize unless contained?
end

def read
# TODO: how to follow contained references?
type, id = reference.to_s.split('/')
return unless [type, id].all?(&:present?)
klass = "FHIR::#{type}".constantize
klass.read(id, client)
return if contained? || type.blank? || id.blank?
resource_class.read(id, client)
end
end
end

0 comments on commit 1d2a7a5

Please sign in to comment.