Module: OpenehrRails::Storable

Extended by:
ActiveSupport::Concern
Defined in:
lib/openehr_rails/storable.rb

Overview

Persists model data as a canonical openEHR RM Composition document.

Including models are generated by rails g openehr:scaffold and define:

TEMPLATE_ID       - openEHR template id
ROOT_ARCHETYPE_ID - archetype id of the COMPOSITION root
FIELD_MAP         - {attribute_name => field description} where the
                  field description matches Opt::FieldExtractor output

The typed columns stay the source for Rails forms and queries; the rm_composition JSON column is refreshed on every save and is the canonical openEHR representation (used by AQL and the FHIR facade).

Constant Summary collapse

MULTIPLE_ATTRIBUTES =

RM attributes with multiple cardinality become JSON arrays.

%w[content events items activities].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.path_segments(path) ⇒ Object

Splits an RM path into [attribute, node_id] pairs: "/content/data/value" => [["content","x"],["data","at0001"],["value",nil]]



103
104
105
# File 'lib/openehr_rails/storable.rb', line 103

def self.path_segments(path)
  path.scan(%r{/([^/\[]+)(?:\[([^\]]+)\])?}).map { |attr, node| [attr, node] }
end

Instance Method Details

#rm_graphObject

Head version of the persisted RM graph (nil when the RM persistence layer is disabled or its tables are absent).



79
80
81
82
83
# File 'lib/openehr_rails/storable.rb', line 79

def rm_graph
  return nil unless rm_layer_available?

  OpenehrRails::Rm::Composition.latest.find_by(owner_type: self.class.name, owner_id: id)
end

#to_rm_compositionObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/openehr_rails/storable.rb', line 85

def to_rm_composition
  composition = {
    '_type' => 'COMPOSITION',
    'archetype_node_id' => self.class::ROOT_ARCHETYPE_ID,
    'archetype_details' => {
      '_type' => 'ARCHETYPED',
      'archetype_id' => { 'value' => self.class::ROOT_ARCHETYPE_ID },
      'template_id' => { 'value' => self.class::TEMPLATE_ID },
      'rm_version' => '1.0.4'
    },
    'content' => rm_content
  }
  composition['uid'] = { '_type' => 'HIER_OBJECT_ID', 'value' => uid } if respond_to?(:uid) && uid
  composition
end