class YAHL7::V2::Message

An HL7 message is a hierarchical data structure. They are generally associated with a triggering event of some sort.

Attributes

parse_options[RW]
segments[RW]

Public Class Methods

get_message_class(code) click to toggle source

This method is used to determine which class should be used to represent the given HL7 message type code (i.e., ADT, ORU, etc.). If a subclass is defined for it, we return that class in order to allow for some type- specific behavior. Otherwise, we use this class.

# File lib/yahl7/v2/message.rb, line 65
def self.get_message_class(code)
  klass = Module.const_get("#{name}::#{code}")
  klass.is_a?(Class) ? klass : self
rescue NameError
  self
end
message_type(body, parse_options = nil) click to toggle source

This method is used to determine which class should be used to represent the given HL7 message string. See `get_message_class` for more information, as this method simply finds the type code and then hands it off to `get_message_class` to find the class to use.

# File lib/yahl7/v2/message.rb, line 47
def self.message_type(body, parse_options = nil)
  return self if body.length < 8

  parse_options ||= ParseOptions.from_body(body)
  head = body.split(parse_options.segment_sep)[0]
  return self if head.nil?

  type = head.split(parse_options.repetition_sep)[8]
  return self if type.nil?

  code = type.split(parse_options.component_sep)[0]
  get_message_class(code)
end
new(body, parse_options = nil) click to toggle source
# File lib/yahl7/v2/message.rb, line 12
def initialize(body, parse_options = nil)
  @parse_options = parse_options || ParseOptions.from_body(body)

  @segments = body
              .split(parse_options.segment_sep)
              .map { |s| Segment.parse(s, parse_options) }
end
parse(body, parse_options = nil) click to toggle source

This method should be used rather than the initializer where possible, because it will defer to a sub-class where possible. This results in the ability to have message type-specific behavior, where possible.

# File lib/yahl7/v2/message.rb, line 38
def self.parse(body, parse_options = nil)
  parse_options ||= ParseOptions.from_body(body)
  message_type(body, parse_options).new(body, parse_options)
end

Public Instance Methods

[](index) click to toggle source
# File lib/yahl7/v2/message.rb, line 24
def [](index)
  case index
  when Integer then segments[index]
  when String then segments_by_type(index)
  end
end
each(&block) click to toggle source
# File lib/yahl7/v2/message.rb, line 31
def each(&block)
  segments.each(&block)
end
to_s() click to toggle source
# File lib/yahl7/v2/message.rb, line 20
def to_s
  @to_s ||= segments.map(&:to_s).join(parse_options.segment_sep)
end

Private Instance Methods

segments_by_type(type) click to toggle source
# File lib/yahl7/v2/message.rb, line 74
def segments_by_type(type)
  segments.filter { |s| s.type == type }
end