Class: OpenEHR::RM::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/openehr/rm/factory.rb

Direct Known Subclasses

CompositionFactory

Constant Summary collapse

NON_POLYMORPHIC_TYPE_FOR_KEY =

Attribute keys whose openEHR RM type is always exactly one concrete class (never actually polymorphic), so a real-world canonical-JSON serializer may legally omit _type for them - e.g. openehr-rails's own CanonicalSerializer does. Consulted only as a fallback when _type is absent; see convert_hash below.

Deliberately does NOT include attributes whose declared type is abstract (e.g. LOCATABLE.uid : UID_BASED_ID, which could be an ObjectVersionID or a HierObjectID; OBJECT_REF.id : OBJECT_ID, similarly abstract) - guessing there would silently build the wrong object instead of failing loudly.

{
  archetype_id: 'ARCHETYPE_ID',
  template_id: 'TEMPLATE_ID',
  terminology_id: 'TERMINOLOGY_ID'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cobject) ⇒ Factory

Returns a new instance of Factory.



23
24
25
# File 'lib/openehr/rm/factory.rb', line 23

def initialize(cobject)
  @cobject = cobject
end

Class Method Details

.create(type, **param) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/openehr/rm/factory.rb', line 28

def create(type, **param)
  if type.include? '_'
    type = type.downcase.camelize
  else
    type = type.capitalize
  end
  class_eval("#{type}Factory").create(params(param))
end

.params(param) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/openehr/rm/factory.rb', line 37

def params(param)
  param.each_with_object({}) do |item, parameters|
    key = item.shift
    value = item.shift
    parameters[key] = convert_value(key, value)
  end
end

Instance Method Details

#buildObject



86
87
88
# File 'lib/openehr/rm/factory.rb', line 86

def build
  Factory.create(type, params)
end