class X12::Loop

$Id: Loop.rb 59 2009-03-19 22:32:13Z ikk $

Implements nested loops of segments

Public Instance Methods

each() { |res| ... } click to toggle source
# File lib/x12/loop.rb, line 78
def each
  res = self.to_a
  0.upto(res.length - 1) do |x|
    yield res[x]
  end
end
parse(str) click to toggle source

Parse a string and fill out internal structures with the pieces of it. Returns an unparsed portion of the string or the original string if nothing was parsed out.

# File lib/x12/loop.rb, line 47
def parse(str)
  #puts "Parsing loop #{name}: "+str
  s = str
  nodes.each{|i|
    m = i.parse(s)
    s = m if m
  } 
  if str == s
    return nil
  else
    self.parsed_str = str[0..-s.length-1]
    s = do_repeats(s)
  end
  #puts 'Parsed loop '+self.inspect
  return s
end
render() click to toggle source

Render all components of this loop as string suitable for EDI

# File lib/x12/loop.rb, line 65
def render
  if self.has_content?
    self.to_a.inject(''){|loop_str, i|
      loop_str += i.nodes.inject(''){|nodes_str, j|
        nodes_str += j.render
      } 
    }
  else
    ''
  end
end