class X12::Loop

Implements nested loops of segments.

Public Instance Methods

each() { |res| ... } click to toggle source

Provides looping through repeats of a message.

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

Formats a printable string containing the loops element's content added to provide compatability with ruby > 2.0.0. @return [String]

# File lib/x12/loop.rb, line 55
def inspect
  "#{self.class.to_s.sub(/^.*::/, '')} (#{name}) #{repeats} =<#{parsed_str}, #{next_repeat.inspect}> ".gsub(/\\*\"/, '"')
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 21
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 39
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