class EDI::A::Segment

Class EDI::A::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/ansi_x12.rb, line 876
def initialize(p, tag)
  super( p, tag )

  each_BCDS('s'+tag) do |entry| # FIXME: Workaround for X12 segment names
    id = entry.name
    status = entry.status

    # FIXME: Code redundancy in type detection - remove later!
    case id
    when /C\d{3}/           # Composite
      add new_CDE(id, status)
    when /I\d{2}|\d{1,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/ansi_x12.rb, line 908
def Segment.parse (p, buf, tag_expected=nil)
  # Buffer contains a single segment
  # obj_list = buf.split( Regexp.new('\\'+p.root.de_sep.chr) ) # FIXME: Pre-calc the regex!
  obj_list = buf.split( p.root.re_de_sep )
  tag = obj_list.shift                # First entry must be the segment tag

  raise "Illegal tag: #{tag}" unless tag =~ /[A-Z][A-Z0-9]{1,2}/
    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

d0002=( value ) click to toggle source

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

# File lib/edi4r/ansi_x12.rb, line 954
def d0002=( value ); fail "ANSI version not modifiable!"; end
d28=( value ) click to toggle source

Setter for DE 28 in GS & GE (group reference)

Calls superclass method
# File lib/edi4r/ansi_x12.rb, line 964
def d28=( value )
  return super unless self.name=~/G[SE]/
  parent.header['28'].first.value = value
  parent.trailer['28'].first.value = value
end
d329=( value ) click to toggle source

Setter for DE 329 in ST & SE (TS control number)

Calls superclass method
# File lib/edi4r/ansi_x12.rb, line 971
def d329=( value )
  return super unless self.name=~/S[TE]/
  parent.header['329'].first.value = value
  parent.trailer['329'].first.value = value
end
dI12=( value ) click to toggle source

Setter for DE I12 in ISA & IEA (interchange control reference)

Calls superclass method
# File lib/edi4r/ansi_x12.rb, line 957
def dI12=( value )
  return super unless self.name=~/I[SE]A/
  parent.header['I12'].first.value = value
  parent.trailer['I12'].first.value = value
end
new_CDE(id, status) click to toggle source
# File lib/edi4r/ansi_x12.rb, line 896
def new_CDE(id, status)
  CDE.new(self, id, status)
end
new_DE(id, status, fmt) click to toggle source
# File lib/edi4r/ansi_x12.rb, line 901
def new_DE(id, status, fmt)
  DE.new(self, id, status, fmt)
end
to_s() click to toggle source
# File lib/edi4r/ansi_x12.rb, line 926
def to_s
  s = ''
  return s if empty?

  rt = self.root

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