Class: RowX
- Inherits:
-
Object
- Object
- RowX
- Defined in:
- lib/rowx.rb
Instance Attribute Summary collapse
-
#to_a ⇒ Object
readonly
Returns the value of attribute to_a.
-
#to_lines(delimiter: ' # ') ⇒ Object
readonly
Returns the value of attribute to_lines.
-
#to_xml ⇒ Object
readonly
Returns the value of attribute to_xml.
Instance Method Summary collapse
-
#initialize(txt, level: nil, ignore_blank_lines: false, abort_1_row: false, debug: false, name: 'item', allow_lonely_keyfield: false) ⇒ RowX
constructor
A new instance of RowX.
Constructor Details
#initialize(txt, level: nil, ignore_blank_lines: false, abort_1_row: false, debug: false, name: 'item', allow_lonely_keyfield: false) ⇒ RowX
Returns a new instance of RowX.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rowx.rb', line 38 def initialize(txt, level: nil, ignore_blank_lines: false, abort_1_row: false, debug: false, name: 'item', allow_lonely_keyfield: false) @name, @debug = name, debug # auto indent any multiline values indent = '' lines = txt.gsub(/^-+$/m,'').lines.map do |line| if not line[/^ *\w+:|^ +/] then indent + ' ' + line else indent = line[/^ +/] || '' line end end puts 'lines: ' + lines.inspect if @debug a = LineTree.new(lines.join, level: level, ignore_blank_lines: ignore_blank_lines, debug: debug).to_a puts ('a: ' + a.inspect).debug if @debug keyfield = a[0][0][/\w+:/]; i = 0 puts ('keyfield: ' + keyfield.inspect).debug if @debug if not allow_lonely_keyfield then # find the keyfield. if there's only 1 keyfield in all of the rows it's # not a keyfield. Keep searching until all rows have been searched while a.select {|x| x[0][/^#{keyfield}/]}.length <= 1 and \ i < a.length and a[i+1] i += 1 keyfield = a[i][0][/\w+/] end end keyfield = a[0][0][/\w+/] if i == a.length - 1 if a.flatten(1).grep(/^#{keyfield}/).length == 1 then # only 1 record i = 0 raise RowXException, 'Expected more than 1 row' if abort_1_row end records = a[i..-1].collate { |x| x.first =~ /^#{keyfield }/ } summary = scan_a a.slice!(0,i) summary[0] = 'summary' @rexle_a = scan_records(records, level) @to_a = ['root', {}] + [summary] + @rexle_a @to_xml = Rexle.new(@to_a).xml pretty: true end |
Instance Attribute Details
#to_a ⇒ Object (readonly)
Returns the value of attribute to_a.
36 37 38 |
# File 'lib/rowx.rb', line 36 def to_a @to_a end |
#to_lines(delimiter: ' # ') ⇒ Object (readonly)
Returns the value of attribute to_lines.
36 37 38 |
# File 'lib/rowx.rb', line 36 def to_lines @to_lines end |
#to_xml ⇒ Object (readonly)
Returns the value of attribute to_xml.
36 37 38 |
# File 'lib/rowx.rb', line 36 def to_xml @to_xml end |