class EDI::E::Segment

Class EDI::E::Segment

This class implements UN/EDIFACT segments like BGM, NAD etc., including the service segments UNB, UNH …

Public Class Methods

new(p, tag) click to toggle source

A new segment must have a parent (usually, a message). This is the first parameter. The second is a string with the desired segment tag.

Don’t create segments without their context - use Message#new_segment() instead.

Calls superclass method EDI::Collection_S::new
# File lib/edi4r/edifact.rb, line 1222
def initialize(p, tag)
  super( p, tag )

  each_BCDS(tag) do |entry|
    id = entry.name
    status = entry.status

    # FIXME: Code redundancy in type detection - remove later!
    case id
    when /[CES]\d{3}/       # Composite
      add new_CDE(id, status)
    when /\d{4}/            # Simple DE
      add new_DE(id, status, fmt_of_DE(id))
    else                    # Should never occur
      raise "Not a legal DE or CDE id: #{id}"
    end
  end
end
parse(p, buf, tag_expected=nil) click to toggle source

Reserved for internal use

# File lib/edi4r/edifact.rb, line 1254
def Segment.parse (p, buf, tag_expected=nil)
  # Buffer contains a single segment
  
  obj_list = EDI::E::edi_split( buf, p.root.una.de_sep, p.root.una.esc_char )
  tag = obj_list.shift                # First entry must be the segment tag

  raise "Illegal tag: #{tag}" unless tag =~ /[A-Z]{3}/
    if tag_expected and tag_expected != tag
      raise "Wrong segment name! Expected: #{tag_expected}, found: #{tag}"
    end

  seg = p.new_segment(tag)
  seg.each {|obj| obj.parse( obj_list.shift ) }
  seg
  # Error handling needed here if obj_list is not exhausted now!
end

Public Instance Methods

d0001=( value ) click to toggle source

Don’t change DE 0001! d0001=() raises an exception when called.

# File lib/edi4r/edifact.rb, line 1300
def d0001=( value ); fail "Charset not modifiable!"; end
d0002=( value ) click to toggle source

Don’t change DE 0002! d0002=() raises an exception when called.

# File lib/edi4r/edifact.rb, line 1303
def d0002=( value ); fail "EDIFACT Syntax version not modifiable!"; end
d0020=( value ) click to toggle source

Setter for DE 0020 in UNB & UNZ (interchange control reference)

Calls superclass method
# File lib/edi4r/edifact.rb, line 1306
def d0020=( value )
  return super unless self.name=~/UN[BZ]/
  parent.header['0020'].first.value = value
  parent.trailer['0020'].first.value = value
end
d0048=( value ) click to toggle source

Setter for DE 0048 in UNE & UNG (group reference)

Calls superclass method
# File lib/edi4r/edifact.rb, line 1313
def d0048=( value )
  return super unless self.name=~/UN[GE]/
  parent.header['0048'].first.value = value
  parent.trailer['0048'].first.value = value
end
d0062=( value ) click to toggle source

Setter for DE 0062 in UNH & UNT (message reference number)

Calls superclass method
# File lib/edi4r/edifact.rb, line 1320
def d0062=( value ) # UNH
  return super unless self.name=~/UN[HT]/
  parent.header['0062'].first.value = value
  parent.trailer['0062'].first.value = value
end
d0340=( value ) click to toggle source

Setter for DE 0340 in UIH & UIT (message reference number)

Calls superclass method
# File lib/edi4r/edifact.rb, line 1327
def d0340=( value ) # UIH
  return super unless self.name=~/UI[HT]/
  parent.header['0340'].first.value = value
  parent.trailer['0340'].first.value = value
end
new_CDE(id, status) click to toggle source
# File lib/edi4r/edifact.rb, line 1242
def new_CDE(id, status)
  CDE.new(self, id, status)
end
new_DE(id, status, fmt) click to toggle source
# File lib/edi4r/edifact.rb, line 1247
def new_DE(id, status, fmt)
  DE.new(self, id, status, fmt)
end
to_din16557_4( xdoc ) click to toggle source
# File lib/edi4r/edifact-rexml.rb, line 146
def to_din16557_4( xdoc )
  xel  = REXML::Element.new( self.name )
  names.uniq.each do |nm|
    # Array of all items with this name
    a = self[nm]; max = a.size
    raise "DIN16557-4 does not support more than 9 repetitions" if max > 9
    raise "Lookup error (should never occur)" if max == 0
    if max == 1
      obj = a.first
      obj.to_din16557_4( xel ) unless obj.empty?
    else
      a.each_with_index do |obj, i| 
        obj.to_din16557_4( xel, i+1 ) unless obj.empty?
      end
    end
  end
  xdoc.elements << xel
end
to_s() click to toggle source
# File lib/edi4r/edifact.rb, line 1272
def to_s
  s = ''
  return s if empty?

  rt = self.root

  indent = rt.e_indent * (self.level || 0)
  s << indent << name << rt.una.de_sep
  skip_count = 0
  each {|obj| 
    if obj.empty?
      skip_count += 1
    else
      if skip_count > 0
        s << rt.una.de_sep.chr * skip_count
        skip_count = 0
      end
      s << obj.to_s
      skip_count += 1
    end
  }
  s
end