Class: EDI::Diagram::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/edi4r/diagrams.rb

Overview

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”).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, sg_name, root) ⇒ Branch

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.



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/edi4r/diagrams.rb', line 263

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

Instance Attribute Details

#descObject

Returns the value of attribute desc.



256
257
258
# File 'lib/edi4r/diagrams.rb', line 256

def desc
  @desc
end

#sg_nameObject (readonly)

Returns the value of attribute sg_name.



257
258
259
# File 'lib/edi4r/diagrams.rb', line 257

def sg_name
  @sg_name
end

Instance Method Details

#[](index) ⇒ Object

Access node list by index, cf. Array



339
340
341
# File 'lib/edi4r/diagrams.rb', line 339

def [](index)
  @nodelist[index]
end

#eachObject

Iterate through each node of the node list



322
323
324
325
326
327
328
329
# File 'lib/edi4r/diagrams.rb', line 322

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

Returns TRUE if branch is empty. Example:

The tail of a segment group that consists of just the trigger segment

Returns:

  • (Boolean)


352
353
354
# File 'lib/edi4r/diagrams.rb', line 352

def empty?
  @nodelist.size==0
end

#expandObject

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



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/edi4r/diagrams.rb', line 290

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

#shiftObject

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



310
311
312
# File 'lib/edi4r/diagrams.rb', line 310

def shift
  @nodelist.shift
end

#sizeObject

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



345
346
347
# File 'lib/edi4r/diagrams.rb', line 345

def size
  @nodelist.size
end

#unshift(node) ⇒ Object

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



316
317
318
# File 'lib/edi4r/diagrams.rb', line 316

def unshift(node)
  @nodelist.unshift(node)
end