Class: EDI::Message

Inherits:
Collection_HT show all
Defined in:
lib/edi4r.rb,
lib/edi4r/rexml.rb

Overview

A “Message” is a special “Collection with header and trailer” It collects “Segment” objects.

Direct Known Subclasses

E::Message

Instance Attribute Summary

Attributes inherited from Collection_HT

#header, #trailer

Attributes inherited from Object

#name, #parent, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Collection_HT

#empty?, #root=, #to_din16557_4, #to_s, #to_xml_header, #to_xml_trailer, #validate

Methods inherited from Collection

#==, #[], #each, #find_all, #first, #index, #inspect, #last, #length, #map, #names, #normalized_class_name, #root=, #size

Constructor Details

#initialize(p, user_par = nil) ⇒ Message

@@msgCounter = 1


607
608
609
# File 'lib/edi4r.rb', line 607

def initialize( p, user_par=nil )
  super(p, p.root, 'Message')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class EDI::Collection

Class Method Details

.parse_xml(p, xmsg) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/edi4r/rexml.rb', line 156

def Message.parse_xml(p, xmsg)
  _header    = xmsg.elements["Header/Segment"]
  _trailer   = xmsg.elements["Trailer/Segment"]

  msg = p.new_message( Segment.parse_xml( p, _header ) )
  msg.header  = Segment.parse_xml( msg, _header  ) if _header

  xmsg.elements.each('descendant::Segment') do |xel|
    next if xel.parent.name =~ /Header|Trailer/
    msg.add Segment.parse_xml(msg, xel)
  end
  msg.trailer = Segment.parse_xml( msg, _trailer ) if _trailer

  msg
end

Instance Method Details

#add(seg) ⇒ Object

Add only Segment objects to a message!


630
631
632
633
# File 'lib/edi4r.rb', line 630

def add (seg)
  raise "Only Segments allowed here" unless seg.is_a? Segment
  super
end

#each_BCDS(id, &b) ⇒ Object

:nodoc:


619
620
621
622
623
624
625
# File 'lib/edi4r.rb', line 619

def each_BCDS(id, &b) # :nodoc:
  begin
    @maindata.each_BCDS(id, &b )
  rescue EDILookupError
    @parent.each_BCDS(id, &b)
  end
end

#fmt_of_DE(id) ⇒ Object

:nodoc:


612
613
614
615
616
# File 'lib/edi4r.rb', line 612

def fmt_of_DE(id) # :nodoc:
  de = @maindata.de(id)
  return @parent.fmt_of_DE(id) if de.nil?
  de.format
end

#to_xml(xel_parent) ⇒ Object

Build an XML document tree from

a) the linear sequence of segments
b) metadata from the standards DB (attached to each segment)

Track xml parent element for segments by level

Add 'header' and 'trailer' wrapper element around

header and trailer, if any

Trigger segments and their depending segments get wrapped in a 'SegmentGroup' element that bears the group name as its name.


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/edi4r/rexml.rb', line 185

def to_xml( xel_parent )
  xel_msg = REXML::Element.new( 'Message' ) 
  xel_parent.elements << xel_msg

  # Default parent is XML message element itself
  #
  xel_parent_stack = Hash.new(xel_msg)

  xhd = to_xml_header( xel_msg )

  each do |seg|
    next if seg.empty?
    if seg.is_tnode?
      xgrp = REXML::Element.new( 'SegmentGroup' )
      xgrp.attributes["name"] = seg.sg_name
      xel_parent_stack[seg.level - 1] << xgrp
      seg.to_xml( xgrp )
      xel_parent_stack[seg.level] = xgrp
    else
      seg.to_xml( xel_parent_stack[seg.level - 1] )
    end
  end

  xtr = to_xml_trailer( xel_msg )
  [xel_msg, xhd, xtr]
end