Class: EDI::E::Segment

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

Overview

Class EDI::E::Segment

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

Instance Attribute Summary

Attributes inherited from Segment

#level, #sg_name

Attributes inherited from Collection_S

#maxrep, #rep, #status

Attributes inherited from Object

#name, #parent, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Segment

#[], #children, #children_and_self, #descendants, #descendants_and_self, #inspect, #is_tnode?, parse_xml, #update_with

Methods inherited from Collection_S

#each_BCDS, #empty?, #fmt_of_DE, #inspect, #required?, #to_xml, #validate

Methods inherited from Collection

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

Constructor Details

#initialize(p, tag) ⇒ Segment

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.



1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
# File 'lib/edi4r/edifact.rb', line 1370

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

Dynamic Method Handling

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

Class Method Details

.parse(p, buf, tag_expected = nil) ⇒ Object

Reserved for internal use



1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
# File 'lib/edi4r/edifact.rb', line 1402

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

Instance Method Details

#d0001=(value) ⇒ Object

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



1447
# File 'lib/edi4r/edifact.rb', line 1447

def d0001=( value ); fail "Charset not modifiable!"; end

#d0002=(value) ⇒ Object

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



1450
# File 'lib/edi4r/edifact.rb', line 1450

def d0002=( value ); fail "EDIFACT Syntax version not modifiable!"; end

#d0020=(value) ⇒ Object

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



1453
1454
1455
1456
1457
# File 'lib/edi4r/edifact.rb', line 1453

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) ⇒ Object

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



1460
1461
1462
1463
1464
# File 'lib/edi4r/edifact.rb', line 1460

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) ⇒ Object

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



1467
1468
1469
1470
1471
# File 'lib/edi4r/edifact.rb', line 1467

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) ⇒ Object

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



1474
1475
1476
1477
1478
# File 'lib/edi4r/edifact.rb', line 1474

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) ⇒ Object



1390
1391
1392
# File 'lib/edi4r/edifact.rb', line 1390

def new_CDE(id, status)
  CDE.new(self, id, status)
end

#new_DE(id, status, fmt) ⇒ Object



1395
1396
1397
# File 'lib/edi4r/edifact.rb', line 1395

def new_DE(id, status, fmt)
  DE.new(self, id, status, fmt)
end

#to_din16557_4(xdoc) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 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_sObject



1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/edi4r/edifact.rb', line 1419

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