Class: OpenEHR::Path

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

Defined Under Namespace

Classes: InvalidPathError, Segment

Constant Summary collapse

AT_CODE =
/\Aat\d+(\.\d+)*\z/
ARCHETYPE_ID =
/\A[a-zA-Z]\w+-[a-zA-Z]\w+-[a-zA-Z]\w+\.[a-zA-Z]\w+(-[a-zA-Z]\w+)?\.v\d+\z/
ATTRIBUTE =
/\A[a-z][a-zA-Z0-9_]*\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments) ⇒ Path

Returns a new instance of Path.



146
147
148
149
# File 'lib/openehr/path.rb', line 146

def initialize(segments)
  @segments = segments.freeze
  freeze
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



144
145
146
# File 'lib/openehr/path.rb', line 144

def segments
  @segments
end

Class Method Details

.parse(str) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
82
83
# File 'lib/openehr/path.rb', line 74

def self.parse(str)
  raise InvalidPathError, 'path must be a String' unless str.is_a?(String)
  raise InvalidPathError, "path must start with '/': #{str.inspect}" unless str.start_with?('/')
  return new([]) if str == '/'

  body = str[1..-1]
  raise InvalidPathError, "path must not end with '/': #{str.inspect}" if body.end_with?('/')

  new(split_segments(body).map { |token| parse_segment(token) })
end

.valid?(str) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/openehr/path.rb', line 85

def self.valid?(str)
  parse(str)
  true
rescue InvalidPathError
  false
end

Instance Method Details

#+(other) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/openehr/path.rb', line 176

def +(other)
  case other
  when Segment
    self.class.new(segments + [other])
  when Path
    self.class.new(segments + other.segments)
  when String
    self.class.new(segments + [Segment.new(other)])
  else
    raise ArgumentError, "cannot append #{other.class} to a Path"
  end
end

#==(other) ⇒ Object Also known as: eql?



161
162
163
# File 'lib/openehr/path.rb', line 161

def ==(other)
  other.is_a?(Path) && segments == other.segments
end

#descendObject



189
190
191
192
193
# File 'lib/openehr/path.rb', line 189

def descend
  return [nil, self] if root?

  [segments.first, self.class.new(segments[1..-1])]
end

#hashObject



166
167
168
# File 'lib/openehr/path.rb', line 166

def hash
  segments.hash
end

#parentObject



170
171
172
173
174
# File 'lib/openehr/path.rb', line 170

def parent
  return self if root?

  self.class.new(segments[0..-2])
end

#root?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/openehr/path.rb', line 151

def root?
  @segments.empty?
end

#to_sObject



155
156
157
158
159
# File 'lib/openehr/path.rb', line 155

def to_s
  return '/' if root?

  '/' + @segments.map(&:to_s).join('/')
end