module HL7::Message::SegmentListStorage

This module includes methods for storing segments inside segments. has_children(child_types) defines three methods dynamically.

Attributes

child_types[R]

Public Instance Methods

add_child_type(child_type) click to toggle source
# File lib/segment_list_storage.rb, line 7
def add_child_type(child_type)
  if defined?(@child_types)
    @child_types << child_type.to_sym
  else
    has_children [ child_type.to_sym ]
  end
end

Private Instance Methods

<<( value ) click to toggle source
# File lib/segment_list_storage.rb, line 51
def <<( value )
  # do nothing if value is nil
  return unless value

  # make sure it's an array
  value = [value].flatten
  value.map{|item| append(item)}
end
append(value) click to toggle source
# File lib/segment_list_storage.rb, line 60
def append(value)
  unless (value && value.kind_of?(HL7::Message::Segment))
    raise HL7::Exception.new( "attempting to append non-segment to a segment list" )
  end

  value.segment_parent = @parental
  k = @parental
  while (k && k.segment_parent && !k.segment_parent.kind_of?(HL7::Message))
    k = k.segment_parent
  end
  k.segment_parent << value if k && k.segment_parent
  old_append( value )
end
define_method_accepts() click to toggle source
# File lib/segment_list_storage.rb, line 32
def define_method_accepts
  self.class_eval do
    define_method('accepts?') do |t|
      t = t.to_sym if t.respond_to?(:to_sym)
      !!child_types.index(t)
    end
  end
end
define_method_child_types() click to toggle source
# File lib/segment_list_storage.rb, line 26
def define_method_child_types
  define_method(:child_types) do
    self.class.child_types
  end
end
define_method_children() click to toggle source
# File lib/segment_list_storage.rb, line 41
def define_method_children
  self.class_eval do
    define_method(:children) do
      unless defined?(@my_children)
        p = self
        @my_children ||= []
        @my_children.instance_eval do
          @parental = p
          alias :old_append :<<

          def <<( value )
            # do nothing if value is nil
            return unless value

            # make sure it's an array
            value = [value].flatten
            value.map{|item| append(item)}
          end

          def append(value)
            unless (value && value.kind_of?(HL7::Message::Segment))
              raise HL7::Exception.new( "attempting to append non-segment to a segment list" )
            end

            value.segment_parent = @parental
            k = @parental
            while (k && k.segment_parent && !k.segment_parent.kind_of?(HL7::Message))
              k = k.segment_parent
            end
            k.segment_parent << value if k && k.segment_parent
            old_append( value )
          end
        end
      end
      @my_children
    end
  end
end
has_children(child_types) click to toggle source

allows a segment to store other segment objects used to handle associated lists like one OBR to many OBX segments

# File lib/segment_list_storage.rb, line 18
def has_children(child_types)
  @child_types = child_types

  define_method_child_types
  define_method_children
  define_method_accepts
end