Class: Opencrx::Model::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/opencrx/model/record.rb,
lib/opencrx/model/record/query.rb,
lib/opencrx/model/record/attributes.rb

Direct Known Subclasses

Account, Address

Defined Under Namespace

Classes: ItemList

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject

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_providerObject



6
7
8
# File 'lib/opencrx/model/record/query.rb', line 6

def base_provider
  "provider/CRX/segment/Standard"
end

.default_query_optionsObject



33
34
35
# File 'lib/opencrx/model/record/query.rb', line 33

def default_query_options
  {}
end

.get(id, options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/opencrx/model/record/query.rb', line 15

def get(id, options = {})
  item_url = "#{query_url}/#{id}"
  response = Opencrx::session.get(item_url, query: merge_options(options))
  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_options(options)
  default_query_options.merge(options)
end

.providerObject



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(options = {})
  response = Opencrx::session.get(query_url, query: merge_options(options))
  ResultSet.new(response)
end

.query_type_optionObject



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_urlObject



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

#compactObject



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

#destroyObject

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, options = {})
  response = Opencrx::session.get(subtype_query_url(subtype), query: options)
  ResultSet.new(response)
end

#saveObject



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(options = {})
  attributes.except('href', 'version').to_xml(options.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