class EDI::Diagram::Branch

A Branch is a sequence of Nodes. It corresponds to a segment group without its included groups (no sub-branches). A Branch has a name (sg_name) and comes with descriptory text (desc).

Note that included TNodes may have side chains/branches (“tails”).

Attributes

desc[RW]
sg_name[R]

Public Class Methods

new(key, sg_name, root) click to toggle source

A new Branch object is uniquely identified by the key argument that selects the directory entry and its sg_name (if not top branch). root is a reference to the Diagram it belongs to.

# File lib/edi4r/diagrams.rb, line 262
    def initialize(key, sg_name, root)
      # warn "Creating branch for key `#{key||''+sg_name||''}'..."
      @key = key
      @sg_name = sg_name
      @root = root

      @nodelist=[]
      b = @root.dir.message( key+sg_name.to_s )
=begin
      # UN/EDIFACT subsets only:
      if b.nil? && key =~ /(\w{6}:\w+:\w+:\w+:)(.+?)(:.*)/
        puts "2: #{key}"
        @key = key = $1+$3 # Discard the subset DE
        puts "3: #{key}"
        EDI::logger.warn "Subset #{$2} data not found - trying standard instead..."
        b = @root.dir.message( key+sg_name.to_s )
      end
=end
      raise EDI::EDILookupError, "Lookup failed for key `#{key+sg_name.to_s}' - known names: #{@root.dir.message_names.join(', ')}" unless b
      @desc = b.desc
      b.each {|obj| @nodelist << Node.create( obj.name, obj.status, obj.maxrep )}
      raise "Empty branch! key, sg = #{key}, #{sg_name}" if @nodelist.empty?
    end

Public Instance Methods

[](index) click to toggle source

Access node list by index, cf. Array

# File lib/edi4r/diagrams.rb, line 338
def [](index)
  @nodelist[index]
end
each() { |node| ... } click to toggle source

Iterate through each node of the node list

# File lib/edi4r/diagrams.rb, line 321
def each
  @nodelist.each {|node|
    yield(node)
    if node.is_a? TNode and node.tail
      node.tail.each {|tn| yield(tn)} # Recursion
    end
  }
end
empty?() click to toggle source

Returns TRUE if branch is empty. Example:

The tail of a segment group that consists of just the trigger segment
# File lib/edi4r/diagrams.rb, line 351
def empty?
  @nodelist.size==0
end
expand() click to toggle source

Recursively add “tails” (branches, segment groups) to TNodes

# File lib/edi4r/diagrams.rb, line 289
def expand
  each do |node|
    if node.is_a? TNode and node.tail == nil
      #        puts "Expanding #{node}"
      tail = Branch.new(@key, node.name, @root)

      # Merge TNode with first tail node (trigger segment)
      trigger_segment = tail.shift
      node.name = trigger_segment.name
      if trigger_segment.status != 'M' or trigger_segment.maxrep != 1
        raise "#{trigger_segment.name}: Not a trigger seg!" 
      end
      node.tail = tail.expand # Recursion!
    end
  end
  self
end
shift() click to toggle source

Removes and returns the first node from the node list, cf. Array#shift

# File lib/edi4r/diagrams.rb, line 309
def shift
  @nodelist.shift
end
size() click to toggle source

Returns size of the node list (number of nodes of this branch)

# File lib/edi4r/diagrams.rb, line 344
def size
  @nodelist.size
end
unshift(node) click to toggle source

Makes node the first node of the node list, cf. Array#unshift

# File lib/edi4r/diagrams.rb, line 315
def unshift(node)
  @nodelist.unshift(node)
end