Class: EDI::E::DE

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

Overview

Class EDI::E::DE

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

For internal use only.

Instance Attribute Summary

Attributes inherited from DE

#format, #status, #value

Attributes inherited from Object

#name, #parent, #root

Instance Method Summary collapse

Methods inherited from DE

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

Constructor Details

#initialize(p, name, status, fmt) ⇒ DE

Returns a new instance of DE



1555
1556
1557
1558
1559
1560
# File 'lib/edi4r/edifact.rb', line 1555

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

Instance Method Details

#parse(buf, already_escaped = false) ⇒ Object

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



1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
# File 'lib/edi4r/edifact.rb', line 1566

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



190
191
192
193
194
# 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) ⇒ Object



1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
# File 'lib/edi4r/edifact.rb', line 1580

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

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



1595
1596
1597
1598
1599
1600
# File 'lib/edi4r/edifact.rb', line 1595

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