class EDI::DE

A basic data element. Its content is accessible through methods value and value=. Allowed contents is described by attribute format.

Note that values are usually Strings, or Numerics when the format indicates a numeric value. Other objects are conceivable, as long as they maintain a reasonable to_s for their representation.

The external representation of the (abstract) value may further depend on rules of the unterlying EDI standard. E.g., UN/EDIFACT comes with a set of reserved characters and an escaping mechanism.

Attributes

format[R]
status[R]
value[RW]

Public Class Methods

new( p, name, status, fmt ) click to toggle source
# File lib/edi4r.rb, line 878
def initialize( p, name, status, fmt )
  @parent, @root, @name, @format, @status = p, p.root, name, fmt, status
  if fmt.nil? || status.nil?
    location = "DE #{parent.name}/#{@name}"
    raise "#{location}: 'nil' is not an allowed format." if fmt.nil?
    raise "#{location}: 'nil' is not an allowed status." if status.nil?
  end
  @value = nil
end

Public Instance Methods

empty?() click to toggle source

Returns true if value is not nil. Note that assigning an empty string to a DE makes it “not empty”.

# File lib/edi4r.rb, line 995
def empty?
  @value == nil
end
inspect( indent='' ) click to toggle source
# File lib/edi4r.rb, line 900
def inspect( indent='' )
  indent + self.name+': ' + [:value, :format, :status].map do |sym|
    "#{sym} = #{(s=send(sym)).nil? ? 'nil' : s.to_s}"
  end.join(', ') + "\n"
end
required?() click to toggle source

Returns true if this is a required / mandatory segment.

# File lib/edi4r.rb, line 1001
def required?
  @status == 'M' or @status == 'R'
end
to_s() click to toggle source
# File lib/edi4r.rb, line 889
def to_s
  str = self.value
  return str if str.is_a? String
  str = str.to_s; len = str.length
  return str unless format =~ /n(\d+)/ && len != (fixlen=$1.to_i)
  location = "DE #{parent.name}/#{@name}"
  raise "#{location}: Too long (#{l}) for fmt #{format}" if len > fixlen
  '0' * (fixlen - len) + str
end
to_xml( xel_parent, instance=1 ) click to toggle source
# File lib/edi4r/rexml.rb, line 251
def to_xml( xel_parent, instance=1 )
  xel = REXML::Element.new( 'DE' ) 
  xel.attributes["name"] = @name
  xel.attributes["instance"] = instance.to_s if instance > 1
  xel_parent.elements << xel
  xel.text = self.to_s( true ) # don't escape!
  xel
end
validate( err_count=0, fmt=@format ) click to toggle source

Performs various validation checks and returns the number of issues found (plus the value of err_count):

  • empty while mandatory?

  • character set limitations violated?

  • various format restrictions violated?

# File lib/edi4r.rb, line 914
    def validate( err_count=0, fmt=@format )
      location = "DE #{parent.name}/#{@name}"
      if empty?
        if required?
          EDI::logger.warn "#{location}: Empty though mandatory!"
          err_count += 1
        end
      else
        #
        # Charset check
        #
        if (pos = (value =~ root.illegal_charset_pattern))# != nil
          EDI::logger.warn "#{location}: Illegal character: #{value[pos].chr} (#{value[pos]})"
          err_count += 1
        end
        #
        # Format check, raise error if not consistent!
        #
        if fmt =~ /^(a|n|an)(..)?(\d+)$/
          _a_n_an, _upto, _size = $1, $2, $3
          case _a_n_an

          when 'n'
            strval = value.to_s
            re = Regexp.new('^(-)?(\d+)([.,]\d+)?$')
            md = re.match strval
            if md.nil?
              raise "#{location}: '#{strval}' - not matching format #{fmt}"
              #              warn "#{strval} - not matching format #{fmt}"
#              err_count += 1
            end

            len = strval.length
            # Sign char does not go into length count:
            len -= 1 if md[1]=='-'
            # Decimal char does not go into length count:
            len -= 1 if not md[3].nil?
            # len -= 1 if (md[1]=='-' and md[3]) || (md[1] != '' and not md[3])

            # break if not required? and len == 0
           if required? or len != 0
            if len > _size.to_i
#            if _upto.nil? and len != _size.to_i or len > _size.to_i
              EDI::logger.warn "Context in #{location}: #{_a_n_an}, #{_upto}, #{_size}; #{md[1]}, #{md[2]}, #{md[3]}"
              EDI::logger.warn "Length # mismatch in #{location}: #{len} vs. #{_size}"
              err_count += 1
              #            warn "  (strval was: '#{strval}')"
            end
            if md[1] =~/^0+/
              EDI::logger.warn "#{strval} contains leading zeroes"
              err_count += 1
            end
            if md[3] and md[3]=~ /.0+$/
              EDI::logger.warn "#{strval} contains trailing decimal sign/zeroes"
              err_count += 1
            end
           end

          when 'a', 'an'
#            len = value.is_a?(Numeric) ? value.to_s.length : value.length
            len = value.to_s.length
            if _upto.nil? and len != $3.to_i or len > $3.to_i
              EDI::logger.warn "Length mismatch in #{location}: #{len} vs. #{_size}"
              err_count += 1
            end
          else
            raise "#{location}: Illegal format prefix #{_a_n_an}"
            # err_count += 1
          end

        else
          EDI::logger.warn "#{location}: Illegal format: #{fmt}!"
          err_count += 1
        end
      end
      err_count
    end