Class: OpenEHR::RM::Common::Archetyped::Pathable

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ Pathable

Returns a new instance of Pathable.



37
38
39
# File 'lib/openehr/rm/common/archetyped.rb', line 37

def initialize(args = { })
  self.parent = args[:parent]
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



22
23
24
# File 'lib/openehr/rm/common/archetyped.rb', line 22

def parent
  @parent
end

Class Method Details

.normalize_path(path) ⇒ Object



89
90
91
# File 'lib/openehr/rm/common/archetyped.rb', line 89

def self.normalize_path(path)
  path.is_a?(OpenEHR::Path) ? path : OpenEHR::Path.parse(path)
end

.path_attribute(*attr_names) ⇒ Object

Declares which of this class's attributes are exposed to path navigation, e.g. path_attribute :data, :state. Declarations accumulate down the inheritance chain.



27
28
29
30
# File 'lib/openehr/rm/common/archetyped.rb', line 27

def self.path_attribute(*attr_names)
  @path_attributes ||= []
  @path_attributes.concat(attr_names.map(&:to_sym))
end

.path_attributesObject



32
33
34
35
# File 'lib/openehr/rm/common/archetyped.rb', line 32

def self.path_attributes
  own = @path_attributes || []
  self == Pathable ? own : superclass.path_attributes + own
end

.select_by_predicate(children, segment) ⇒ Object

Filters children matched by a path segment's predicate: a node_id predicate keeps only children whose archetype_node_id matches (non-Locatable children never match a node_id predicate); a name predicate further narrows to the child whose name.value matches.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/openehr/rm/common/archetyped.rb', line 98

def self.select_by_predicate(children, segment)
  return children unless segment.predicate?

  matched = children.select do |child|
    child.respond_to?(:archetype_node_id) &&
      child.archetype_node_id == segment.archetype_node_id
  end
  return matched if segment.name.nil?

  matched.select do |child|
    child.respond_to?(:name) && child.name.respond_to?(:value) &&
      child.name.value == segment.name
  end
end

Instance Method Details

#item_at_path(path) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/openehr/rm/common/archetyped.rb', line 52

def item_at_path(path)
  matches = items_at_path(path)
  case matches.size
  when 0 then nil
  when 1 then matches.first
  else raise PathNotUniqueError, "path #{path} matches #{matches.size} items"
  end
end

#items_at_path(path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/openehr/rm/common/archetyped.rb', line 61

def items_at_path(path)
  path = self.class.normalize_path(path)
  return [self] if path.root?

  segment, rest = path.descend
  children = path_children[segment.attribute]
  return [] if children.nil?

  matched = self.class.select_by_predicate(Array(children), segment)
  return matched if rest.root?

  matched.flat_map do |child|
    child.is_a?(Pathable) ? child.items_at_path(rest) : []
  end
end

#path_childrenObject

The one-level-deep path-navigable children of this node, as => value. Attributes whose value is nil are omitted; a value may itself be an Array (e.g. a CLUSTER's items).



45
46
47
48
49
50
# File 'lib/openehr/rm/common/archetyped.rb', line 45

def path_children
  self.class.path_attributes.each_with_object({}) do |attr, hash|
    value = send(attr)
    hash[attr.to_s] = value unless value.nil?
  end
end

#path_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/openehr/rm/common/archetyped.rb', line 77

def path_exists?(path)
  !items_at_path(path).empty?
end

#path_of_item(item) ⇒ Object



81
82
83
# File 'lib/openehr/rm/common/archetyped.rb', line 81

def path_of_item(item)
  find_path_to(item, OpenEHR::Path.new([]))
end

#path_unique?(path) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/openehr/rm/common/archetyped.rb', line 85

def path_unique?(path)
  items_at_path(path).size == 1
end