Class: EDI::E::CDE

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

Overview

Class EDI::E::CDE

This class implements UN/EDIFACT composite data elements C507 etc., including the service CDEs S001, S009 …

For internal use only.

Instance Attribute Summary

Attributes inherited from Collection_S

#maxrep, #rep, #status

Attributes inherited from Object

#name, #parent, #root

Instance Method Summary collapse

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, name, status) ⇒ CDE

Returns a new instance of CDE



1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
# File 'lib/edi4r/edifact.rb', line 1494

def initialize(p, name, status)
  super(p, name, status)

  each_BCDS(name) do |entry|
    id = entry.name
    status = entry.status
    # FIXME: Code redundancy in type detection - remove later!
    if id =~ /\d{4}/
      add new_DE(id, status, fmt_of_DE(id))
    else				# Should never occur
      raise "Not a legal DE: #{id}"
    end
  end
end

Dynamic Method Handling

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

Instance Method Details

#new_DE(id, status, fmt) ⇒ Object



1509
1510
1511
# File 'lib/edi4r/edifact.rb', line 1509

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

#parse(buf) ⇒ Object

Buffer contains content of a single CDE



1514
1515
1516
1517
1518
1519
# File 'lib/edi4r/edifact.rb', line 1514

def parse (buf)	# Buffer contains content of a single CDE
  return nil unless buf
  obj_list = EDI::E::edi_split( buf, root.una.ce_sep, root.una.esc_char )
  each {|obj| obj.parse( obj_list.shift ) }
  # FIXME: Error handling needed here if obj_list is not exhausted now!
end

#to_din16557_4(xel, rep = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/edi4r/edifact-rexml.rb', line 168

def to_din16557_4( xel, rep=nil )
  prefix = name
  prefix += rep.to_s if rep
  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, nil, prefix ) unless obj.empty?
    else
      a.each_with_index do |obj, i| 
        obj.to_din16557_4( xel, i+1, prefix ) unless obj.empty?
      end
    end
  end
end

#to_sObject



1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
# File 'lib/edi4r/edifact.rb', line 1522

def to_s
  rt = self.root
  s = ''; skip_count = 0
  ce_sep = rt.una.ce_sep.chr
  each {|de|
    if de.empty?
      skip_count += 1
    else
      if skip_count > 0
        s << ce_sep * skip_count
        skip_count = 0
      end
      s << de.to_s
      skip_count += 1
    end
  }
  s
end