class EDI::E::DE

Class EDI::E::DE

This class implements UN/EDIFACT data elements 1004, 2005 etc., including the service DEs 0001, 0004, …

For internal use only.

Public Class Methods

new( p, name, status, fmt ) click to toggle source
Calls superclass method EDI::DE::new
# File lib/edi4r/edifact.rb, line 1408
def initialize( p, name, status, fmt )
  super( p, name, status, fmt )
  raise "Illegal DE name: #{name}" unless name =~ /\d{4}/
    # check if supported format syntax
    # check if supported status value
end

Public Instance Methods

parse( buf, already_escaped=false ) click to toggle source

Generate the DE content from the given string representation. buf contains a single DE string, possibly escaped

# File lib/edi4r/edifact.rb, line 1419
def parse( buf, already_escaped=false )
  return nil unless buf
  return @value = nil if buf.empty?
  @value = already_escaped ? buf : unescape(buf)
  if format[0] == ?n
    # Normalize decimal sign
    @value.sub!(/,/, '.')
    # Select appropriate Numeric, FIXME: Also match exponents!
    self.value = @value=~/\d+\.\d+/ ? @value.to_f : @value.to_i
  end
  @value
end
to_din16557_4( xel, rep=nil, prefix='' ) click to toggle source
# File lib/edi4r/edifact-rexml.rb, line 190
def to_din16557_4( xel, rep=nil, prefix='' )
  nm = prefix + 'D' + name
  nm += rep.to_s if rep
  xel.attributes[nm] = to_s( true )
end
to_s( no_escape=false ) click to toggle source
Calls superclass method EDI::DE#to_s
# File lib/edi4r/edifact.rb, line 1433
def to_s( no_escape=false )
  return '' if empty?
  s = if @value.is_a? Numeric
        # Adjust decimal sign
        super().sub(/[.,]/, root.una.decimal_sign.chr)
      else
        super().to_s
      end
  no_escape ? s : escape(s)
end
value=( val ) click to toggle source

The proper method to assign values to a DE. The passed value must respond to to_i .

Calls superclass method
# File lib/edi4r/edifact.rb, line 1448
def value=( val )
  # Suppress trailing decimal part if Integer value
  ival = val.to_i
  val = ival if val.is_a? Float and val == ival
  super
end

Private Instance Methods

escape(str) click to toggle source
# File lib/edi4r/edifact.rb, line 1458
def escape (str) 
  rt = self.root
  raise "Must have a root to do this" if rt == nil

  esc = rt.una.esc_char.chr
  esc << ?\\ if esc == '\\' # Special case if backslash!
               
  if rt.charset == 'UNOA'
    # Implicit conversion to uppercase - convenient,
    # but could be argued against!
    str.upcase.gsub(rt.una.pattern_esc, esc+'\1')
  else
    str.gsub(rt.una.pattern_esc, esc+'\1')
  end
end
unescape(str) click to toggle source
# File lib/edi4r/edifact.rb, line 1474
def unescape (str)
  rt = self.root
  raise "Must have a root to do this" if rt == nil
  str.gsub(rt.una.pattern_unesc, '\1\2')
end