Class: Opencrx::Model::Record
- Inherits:
-
Object
- Object
- Opencrx::Model::Record
- Defined in:
- lib/opencrx/model/record.rb,
lib/opencrx/model/record/query.rb,
lib/opencrx/model/record/attributes.rb
Defined Under Namespace
Classes: ItemList
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
Class Method Summary collapse
- .base_provider ⇒ Object
- .default_query_options ⇒ Object
- .get(id, options = {}) ⇒ Object
- .have_array_attributes(*args) ⇒ Object
- .have_attributes(*args) ⇒ Object
- .merge_options(options) ⇒ Object
- .provider ⇒ Object
- .query(options = {}) ⇒ Object
- .query_type_option ⇒ Object
- .query_url ⇒ Object
Instance Method Summary collapse
- #attribute(key) ⇒ Object
- #compact ⇒ Object
-
#destroy ⇒ Object
TODO implement delete.
-
#initialize(attributes = {}) ⇒ Record
constructor
A new instance of Record.
-
#query(subtype, options = {}) ⇒ Object
children query.
- #save ⇒ Object
- #subtype_query_url(subtype) ⇒ Object
- #to_xml(options = {}) ⇒ Object
-
#write_attribute(key, value) ⇒ Object
store only simple values, or simple arrays.
Constructor Details
#initialize(attributes = {}) ⇒ Record
35 36 37 |
# File 'lib/opencrx/model/record/attributes.rb', line 35 def initialize(attributes = {}) self.attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
33 34 35 |
# File 'lib/opencrx/model/record/attributes.rb', line 33 def attributes @attributes end |
Class Method Details
.base_provider ⇒ Object
6 7 8 |
# File 'lib/opencrx/model/record/query.rb', line 6 def base_provider "provider/CRX/segment/Standard" end |
.default_query_options ⇒ Object
33 34 35 |
# File 'lib/opencrx/model/record/query.rb', line 33 def {} end |
.get(id, options = {}) ⇒ Object
15 16 17 18 19 |
# File 'lib/opencrx/model/record/query.rb', line 15 def get(id, = {}) item_url = "#{query_url}/#{id}" response = Opencrx::session.get(item_url, query: ()) Result.parse(response) end |
.have_array_attributes(*args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/opencrx/model/record/attributes.rb', line 16 def self.have_array_attributes(*args) args.each do |arg| define_method(arg) do Array.wrap(attribute(arg)) end define_method("#{arg}=") do |value| write_attribute(arg, Array.wrap(value)) end end end |
.have_attributes(*args) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/opencrx/model/record/attributes.rb', line 5 def self.have_attributes(*args) args.each do |arg| define_method(arg) do attribute(arg) end define_method("#{arg}=") do |value| write_attribute(arg, value) end end end |
.merge_options(options) ⇒ Object
29 30 31 |
# File 'lib/opencrx/model/record/query.rb', line 29 def () .merge() end |
.provider ⇒ Object
25 26 27 |
# File 'lib/opencrx/model/record/query.rb', line 25 def provider base_provider end |
.query(options = {}) ⇒ Object
10 11 12 13 |
# File 'lib/opencrx/model/record/query.rb', line 10 def query( = {}) response = Opencrx::session.get(query_url, query: ()) ResultSet.new(response) end |
.query_type_option ⇒ Object
37 38 39 40 41 |
# File 'lib/opencrx/model/record/query.rb', line 37 def query_type_option @query_type_option ||= { queryType: Map.model_to_opencrx_query(self) } end |
.query_url ⇒ Object
21 22 23 |
# File 'lib/opencrx/model/record/query.rb', line 21 def query_url "/#{BASE_KEY}/#{provider}" end |
Instance Method Details
#attribute(key) ⇒ Object
66 67 68 |
# File 'lib/opencrx/model/record/attributes.rb', line 66 def attribute(key) attributes[key.to_s] end |
#compact ⇒ Object
70 71 72 73 74 75 |
# File 'lib/opencrx/model/record/attributes.rb', line 70 def compact attributes.inject({}) do |memo, (key, value)| memo[key] = value if value.present? && value != '0' memo end end |
#destroy ⇒ Object
TODO implement delete
20 21 22 23 24 25 26 |
# File 'lib/opencrx/model/record.rb', line 20 def destroy href = attributes['href'] # this errors with No active unit of work #Opencrx::session.delete(href, body: to_xml) # this errors with Premature end of file. #Opencrx::session.delete(href) end |
#query(subtype, options = {}) ⇒ Object
children query
45 46 47 48 |
# File 'lib/opencrx/model/record/query.rb', line 45 def query(subtype, = {}) response = Opencrx::session.get(subtype_query_url(subtype), query: ) ResultSet.new(response) end |
#save ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/opencrx/model/record.rb', line 9 def save href = attributes['href'] || self.class.query_url response = if attributes['identity'] Opencrx::session.put(href, body: to_xml) else Opencrx::session.post(href, body: to_xml) end Result.parse(response) end |
#subtype_query_url(subtype) ⇒ Object
50 51 52 |
# File 'lib/opencrx/model/record/query.rb', line 50 def subtype_query_url(subtype) "#{attributes['href']}/#{subtype}" end |
#to_xml(options = {}) ⇒ Object
77 78 79 |
# File 'lib/opencrx/model/record/attributes.rb', line 77 def to_xml( = {}) attributes.except('href', 'version').to_xml(.merge(root: Map.model_to_opencrx_key(self.class))) end |
#write_attribute(key, value) ⇒ Object
store only simple values, or simple arrays. arrays from opencrx are encoded as a hash values under key ‘_item’
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/opencrx/model/record/attributes.rb', line 48 def write_attribute(key, value) key = key.to_s case value # incoming from opencrx when Hash items = Array.wrap(value['_item']) if items.first.is_a?(String) @attributes[key] = ItemList.new(items) end # incoming from our side when Array @attributes[key] = ItemList.new(value) else @attributes[key] = value end @attributes[key] end |