Class: EDI::Collection_S

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

Overview

Utility: Separator method for UN/EDIFACT segments/CDEs

Direct Known Subclasses

CDE, Segment

Instance Attribute Summary collapse

Attributes inherited from Object

#name, #parent, #root

Instance Method Summary collapse

Methods inherited from Collection

#==, #[], #add, #each, #find_all, #first, #index, #last, #length, #map, #names, #normalized_class_name, #root=, #size

Constructor Details

#initialize(parent, name, status = nil) ⇒ Collection_S

Returns a new instance of Collection_S



376
377
378
379
# File 'lib/edi4r.rb', line 376

def initialize(parent, name, status=nil)
  @status = status
  super( parent, parent.root, name)
end

Dynamic Method Handling

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

Instance Attribute Details

#maxrepObject

Returns the value of attribute maxrep



373
374
375
# File 'lib/edi4r.rb', line 373

def maxrep
  @maxrep
end

#repObject

Returns the value of attribute rep



373
374
375
# File 'lib/edi4r.rb', line 373

def rep
  @rep
end

#statusObject

Returns the value of attribute status



373
374
375
# File 'lib/edi4r.rb', line 373

def status
  @status
end

Instance Method Details

#each_BCDS(id, &b) ⇒ Object

:nodoc:



405
406
407
# File 'lib/edi4r.rb', line 405

def each_BCDS(id, &b) # :nodoc:
  @parent.each_BCDS(id, &b)
end

#empty?Boolean

Returns true if all contained elements are empty.

Returns:

  • (Boolean)


412
413
414
415
416
# File 'lib/edi4r.rb', line 412

def empty?
  empty = true
  each {|obj| empty &= obj.empty? } # DE or CDE
  empty
end

#fmt_of_DE(id) ⇒ Object

:nodoc:



400
401
402
# File 'lib/edi4r.rb', line 400

def fmt_of_DE(id) # :nodoc:
  @parent.fmt_of_DE(id)
end

#inspect(indent = '', symlist = []) ⇒ Object



427
428
429
430
# File 'lib/edi4r.rb', line 427

def inspect( indent='', symlist=[] )
  symlist += [:status, :rep, :maxrep]
  super
end

#required?Boolean

Returns true if this segment or CDE is mandatory / required according to its defining “Diagram”.

Returns:

  • (Boolean)


422
423
424
# File 'lib/edi4r.rb', line 422

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

#to_xml(xel_parent, instance = 1) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/edi4r/rexml.rb', line 38

def to_xml( xel_parent, instance=1 )
  xel  = REXML::Element.new( normalized_class_name ) 
  xel.attributes["name"]  = @name
  xel.attributes["instance"]  = instance if instance > 1
  xel_parent.elements << xel
  instance_counter = Hash.new(0)
  each do |obj| 
    i = (instance_counter[obj.name] += 1)
    obj.to_xml( xel, i ) unless obj.empty?
  end
  xel
end

#validate(err_count = 0) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/edi4r.rb', line 382

def validate( err_count=0 )
  location = "#{parent.name} - #{@name}"
  if empty?
    if required?
      warn "#{location}: Empty though mandatory!"
      err_count += 1
    end
  else
    if rep && maxrep && rep > maxrep
      warn "#{location}: Too often repeated: #{rep} > #{maxrep}!"
      err_count += 1
    end
    each {|obj| err_count += obj.validate}
  end
  err_count
end