class YAHL7::V2::Segment

Segments are the first level of data within a message. They define in general terms the type of data enclosed within. Data within a segment is parsed lazily and then cached. This allows us to speed up the parsing of HL7 segments because not all of the data will be necessary.

Subsequent data access will be cached, however, so the performance hit of subsequent calls should be negligable.

Attributes

body[RW]
field_parser[RW]
parse_options[RW]
parts[RW]

Public Class Methods

get_segment_class(code) click to toggle source
# File lib/yahl7/v2/segment.rb, line 58
def self.get_segment_class(code)
  klass = Module.const_get("#{name}::#{code}")
  klass.is_a?(Class) ? klass : self
rescue NameError
  self
end
new(body, parse_options) click to toggle source
# File lib/yahl7/v2/segment.rb, line 21
def initialize(body, parse_options)
  @body = body
  @parts = @body.split(parse_options.repetition_sep)
  @parsed = Array.new(@parts.count)
  @field_parser = FieldParser.new(parse_options)
  @parse_options = parse_options

  return unless defined?(FIELD_MAPPING)
end
parse(body, parse_options) click to toggle source
# File lib/yahl7/v2/segment.rb, line 49
def self.parse(body, parse_options)
  segment_type(body, parse_options).new(body, parse_options)
end
segment_type(body, parse_options) click to toggle source
# File lib/yahl7/v2/segment.rb, line 53
def self.segment_type(body, parse_options)
  code = body.split(parse_options.repetition_sep)[0]
  get_segment_class(code)
end

Public Instance Methods

[](index) click to toggle source

This method allows users to use an index to get a field out of a segment at a given position. This allows data to be fetched from segments like arrays as shown below:

segment[0]
=> 'MSH'
# File lib/yahl7/v2/segment.rb, line 41
def [](index)
  @parsed[index] ||= field_parser.parse(parts[index])
end
to_s() click to toggle source
# File lib/yahl7/v2/segment.rb, line 31
def to_s
  @body
end
type() click to toggle source
# File lib/yahl7/v2/segment.rb, line 45
def type
  parts[0]
end