Class: EDI::DE

Inherits:
Object show all
Defined in:
lib/edi4r.rb,
lib/edi4r/rexml.rb

Overview

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.

Direct Known Subclasses

E::DE

Instance Attribute Summary collapse

Attributes inherited from Object

#name, #parent, #root

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DE


806
807
808
809
810
811
# File 'lib/edi4r.rb', line 806

def initialize( p, name, status, fmt )
  @parent, @root, @name, @format, @status = p, p.root, name, fmt, status
  raise "#{location}: 'nil' is not an allowed format." if fmt.nil?
  raise "#{location}: 'nil' is not an allowed status." if status.nil?
  @value = nil
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format


804
805
806
# File 'lib/edi4r.rb', line 804

def format
  @format
end

#statusObject (readonly)

Returns the value of attribute status


804
805
806
# File 'lib/edi4r.rb', line 804

def status
  @status
end

#valueObject

Returns the value of attribute value


803
804
805
# File 'lib/edi4r.rb', line 803

def value
  @value
end

Instance Method Details

#empty?Boolean

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

Returns:

  • (Boolean)

917
918
919
# File 'lib/edi4r.rb', line 917

def empty?
  @value == nil
end

#inspect(indent = '') ⇒ Object


824
825
826
827
828
# File 'lib/edi4r.rb', line 824

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?Boolean

Returns true if this is a required / mandatory segment.

Returns:

  • (Boolean)

923
924
925
# File 'lib/edi4r.rb', line 923

def required?
  @status == 'M' or @status == 'R'
end

#to_sObject


814
815
816
817
818
819
820
821
# File 'lib/edi4r.rb', line 814

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)
  raise "#{location}: Too long (#{l}) for fmt #{format}" if len > fixlen
  '0' * (fixlen - len) + str
end

#to_xml(xel_parent, instance = nil) ⇒ Object


247
248
249
250
251
252
253
# File 'lib/edi4r/rexml.rb', line 247

def to_xml( xel_parent, instance=nil )
  xel = REXML::Element.new( 'DE' ) 
  xel.attributes["name"] = @name
  xel_parent.elements << xel
  xel.text = self.to_s( true ) # don't escape!
  xel
end

#validate(err_count = 0) ⇒ Object

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?


838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/edi4r.rb', line 838

def validate( err_count=0 )
  location = "DE #{parent.name}/#{@name}"
  if empty?
    if required?
      warn "#{location}: Empty though mandatory!"
      err_count += 1
    end
  else
    #
    # Charset check
    #
    if (pos = (value =~ root.illegal_charset_pattern))# != nil
      warn "#{location}: Illegal character: #{value[pos].chr}"
      err_count += 1
    end
    #
    # Format check, raise error if not consistent!
    #
    if @format =~ /^(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 #@format"
#              warn "#{strval} - not matching format #@format"
#              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])
        if required? and len != 0
          if len > _size.to_i
  #            if _upto.nil? and len != _size.to_i or len > _size.to_i
            warn "Context in #{location}: #{_a_n_an}, #{_upto}, #{_size}; #{md[1]}, #{md[2]}, #{md[3]}"
            warn "Length # mismatch in #{location}: #{len} vs. #{_size}"
            err_count += 1
            #            warn "  (strval was: '#{strval}')"
          end
          if md[1] =~/^0+/
            warn "#{strval} contains leading zeroes"
            err_count += 1
          end
          if md[3] and md[3]=~ /.0+$/
            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
          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
      warn "#{location}: Illegal format: #{@format}!"
      err_count += 1
    end
  end
  err_count
end