Class: EDI::Collection_HT

Inherits:
Collection show all
Defined in:
lib/edi4r.rb,
lib/edi4r/rexml.rb,
lib/edi4r/edifact-rexml.rb

Overview

A collection with header and trailer, common to Interchange, MsgGroup, and Message. Typically, header and trailer are Segment instances.

Direct Known Subclasses

Interchange, Message, MsgGroup

Instance Attribute Summary collapse

Attributes inherited from Object

#name, #parent, #root

Instance Method Summary collapse

Methods inherited from Collection

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

Methods inherited from Object

#initialize

Constructor Details

This class inherits a constructor from EDI::Collection

Dynamic Method Handling

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

Instance Attribute Details

#headerObject

make private or protected?


333
334
335
# File 'lib/edi4r.rb', line 333

def header
  @header
end

#trailerObject

make private or protected?


333
334
335
# File 'lib/edi4r.rb', line 333

def trailer
  @trailer
end

Instance Method Details

#empty?Boolean

Experimental: Ignore content of header / trailer, regard object as empty when nothing “add”ed to it.

Returns:

  • (Boolean)

345
346
347
# File 'lib/edi4r.rb', line 345

def empty?
  @a.empty?
end

#root=(rt) ⇒ Object


335
336
337
338
339
# File 'lib/edi4r.rb', line 335

def root= (rt)
  super( rt )
  @header.root = rt if @header
  @trailer.root = rt if @trailer
end

#to_din16557_4(xparent) ⇒ Object

NOTE: Makes sense only in the UN/EDIFACT context, so we list this method here.


215
216
217
218
219
# File 'lib/edi4r/edifact-rexml.rb', line 215

def to_din16557_4( xparent )
  header.to_din16557_4( xparent )
  each {|obj| obj.to_din16557_4( xparent )}
  trailer.to_din16557_4( xparent )
end

#to_s(postfix = '') ⇒ Object


358
359
360
361
362
363
# File 'lib/edi4r.rb', line 358

def to_s( postfix='' )
  s = @header ? @header.to_s+postfix : ''
  each {|obj| s << (obj.is_a?(Segment) ? obj.to_s+postfix : obj.to_s)}
  s << @trailer.to_s+postfix if @trailer
  s
end

#to_xml(xel_parent) ⇒ Object


55
56
57
58
59
60
61
62
63
64
# File 'lib/edi4r/rexml.rb', line 55

def to_xml( xel_parent )
  xel  = REXML::Element.new( normalized_class_name ) 
  xel.attributes["name"]  = @name
  xel_parent.elements << xel

  xhd = to_xml_header( xel )
  each { |obj| obj.to_xml( xel ) }
  xtr = to_xml_trailer( xel )
  [xel, xhd, xtr] # You might want to add something ...
end

#to_xml_header(xparent) ⇒ Object


67
68
69
70
71
72
73
74
# File 'lib/edi4r/rexml.rb', line 67

def to_xml_header( xparent )
  if @header
    xparent << (xel = REXML::Element.new( 'Header' ))
    @header.to_xml( xel )
    return xel
  end
  nil
end

#to_xml_trailer(xparent) ⇒ Object


76
77
78
79
80
81
82
83
# File 'lib/edi4r/rexml.rb', line 76

def to_xml_trailer( xparent )
  if @trailer
    xparent << (xel = REXML::Element.new( 'Trailer' ))
    @trailer.to_xml( xel )
    return xel
  end
  nil
end

#validate(err_count = 0) ⇒ Object


350
351
352
353
354
355
# File 'lib/edi4r.rb', line 350

def validate( err_count=0 )
  err_count += @header.validate if @header
  err_count += @trailer.validate if @trailer
  each {|obj| err_count += obj.validate}
  err_count
end