class EDI::Segment

A “Segment” is a special Collection of type “S” (segment-like), similar to “CDE”. Special Segment attributes are:

sg_name

The name of its segment group (optional)

level

The segment’s hierarchy level, an integer

Attributes

level[R]
sg_name[R]

Public Class Methods

parse_xml( p, xseg ) click to toggle source
# File lib/edi4r/rexml.rb, line 221
def Segment.parse_xml( p, xseg )
  tag = xseg.attributes['name']
  seg = p.new_segment(tag)
  xseg.elements.each('CDE') do |xcde|
    cde_name = xcde.attributes['name']
    i = (xcde.attributes['instance'] || 1).to_i - 1
    cde = seg[cde_name][i]
    Segment.parse_xml_de( cde, xcde )
  end
  Segment.parse_xml_de( seg, xseg )
  seg
end

Private Class Methods

parse_xml_de( seg_or_cde, xseg_or_cde ) click to toggle source
# File lib/edi4r/rexml.rb, line 236
def Segment.parse_xml_de( seg_or_cde, xseg_or_cde )
  de_instance_counter = Hash.new(0)
  xseg_or_cde.elements.each('DE') do |xde|
    de_name = xde.attributes['name']
    i = (xde.attributes['instance'] || 1).to_i - 1
    seg_or_cde[de_name][i].parse( xde.text, true )
  end
end

Public Instance Methods

[]( xpath_expr ) click to toggle source

Access by XPath expression (support is very limited currently) or by name of the dependent component. Pass them as strings.

Used internally - try to avoid at user level! Currently supported XPath expressions:

  • descendant::*

  • descendant-or-self::*

  • child::*

  • child-or-self::*

Calls superclass method EDI::Collection#[]
# File lib/edi4r.rb, line 770
def []( xpath_expr )
  return super( xpath_expr ) if xpath_expr.is_a? Integer

  msg_unsupported = "Unsupported XPath expression: #{xpath_expr}" 

  case xpath_expr

  when /\A(descendant|child)(-or-self)?::(.*)/
    return xpath_matches($1, $2, $3, msg_unsupported)

    # Currently no real path, no predicate available supported
  when /\//, /\[/, /\]/
    raise IndexError, msg_unsupported

  when /child::(\w+)/ # ignore & accept default axis "child"
    return super( $1 )

  when /::/ # No other axes supported for now
    raise IndexError, msg_unsupported

  else # assume simple element name
    return super( xpath_expr )
  end
end
children() click to toggle source

Returns all child elements of the current segment.

# File lib/edi4r.rb, line 746
def children
  self['child::*']
end
children_and_self() click to toggle source

Returns the current segment and all of its child elements. Useful e.g. to deal with one instance of a segment group without traversing included segment groups.

# File lib/edi4r.rb, line 754
def children_and_self
  self['child-or-self::*']
end
descendants() click to toggle source

Returns array of all segments that have the current segment as ancestor.

# File lib/edi4r.rb, line 731
def descendants
  self['descendant::*']
end
descendants_and_self() click to toggle source

Returns array of all segments with the current segment as ancestor, including the current segment. For trigger segments, this method returns all segments of one instance of the corresponding segment group.

# File lib/edi4r.rb, line 740
def descendants_and_self
  self['descendant-or-self::*']
end
inspect( indent='', symlist=[] ) click to toggle source
Calls superclass method EDI::Collection_S#inspect
# File lib/edi4r.rb, line 796
def inspect( indent='', symlist=[] )
  symlist += [:sg_name, :level]
  super
end
is_tnode?() click to toggle source

Returns true if segment is a TNode (i.e. a trigger segment). Note that only TNodes may have descendants.

# File lib/edi4r.rb, line 724
def is_tnode?
  @tnode
end

Private Instance Methods

add(obj) click to toggle source
Calls superclass method EDI::Collection#add
# File lib/edi4r.rb, line 813
def add (obj)
  raise "Only DE or CDE allowed here" unless obj.is_a? DE or obj.is_a? CDE
  super( obj )
end
xpath_matches( axis, or_self, element, msg_unsupported ) click to toggle source
# File lib/edi4r.rb, line 818
def xpath_matches( axis, or_self, element, msg_unsupported )
  raise IndexError, msg_unsupported if element != '*'
  results = []
  results << self if or_self
  child_mode = (axis=='child')
  return results unless self.is_tnode?
  
  # Now add all segments in self's "tail"
  msg = parent
  index = msg.index(self)
  raise IndexError, "#{name} not found in own message?!" unless index
  loop do
    index += 1
    seg = msg[index]
    break if seg.nil?
    next  if child_mode and seg.level > level+1 # other descendants
    break if seg.level <= level
    results << seg
  end
  results
end