module NestedStringParser
Constants
- VERSION
Public Class Methods
from_rows(rows, root=new_node)
click to toggle source
rows is an array of arrays | A | B | C | | A | B | D | | A | E | F | | A | E | G | | B | H | J | | B | H | K |
same as
| A | B | C | | | | D | | | E | F | | | | G | | B | H | J | | | | K |
same as
A
B C D E F G
B
H J K
# File lib/nested_string_parser.rb, line 49 def self.from_rows rows, root=new_node previous_row = [] rows.each { |row| # row.inject(root) { |node, name| node.find(name) || (node.nb?(name) && node.nest_child(new_node name)) || node } } node = root row_has_a_name = false row.length.times { |i| cell_has_a_name = root.nb?(row[i]) row_has_a_name ||= cell_has_a_name previous_row[i] = name = cell_has_a_name ? row[i] : (!row_has_a_name && previous_row[i]) node = node.find(name) || (node.nb?(name) && node.nest_child(new_node name)) || node } } root end
new_node(val=nil ;)
click to toggle source
# File lib/nested_string_parser.rb, line 65 def self.new_node val=nil ; NestedStringParser::Node.new.tap { |n| n.value = val } ; end
parse(str ;)
click to toggle source
# File lib/nested_string_parser.rb, line 66 def self.parse str ; new_node.accept(*str.split(/\n/)).root ; end
patch(klass=::String ;)
click to toggle source
# File lib/nested_string_parser.rb, line 68 def self.patch klass=::String ; klass.send :include, Extension ; end