nested_string_parser 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nested_string_parser.rb +30 -4
- data/lib/nested_string_parser/version.rb +1 -1
- data/spec/from_rows_spec.rb +62 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bc979b99001df7bd41db9bcd1755e93d4a520b3
|
4
|
+
data.tar.gz: 921e53f32bdcaa6d781eed16804246442263ad55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cecb511ad6f63c406884220096aa74e568c78b9d47caaee94666215a7fa60392c7f249039a88da9fdacb3aaf69b73408fae50792aa3949d7c96e5a1b11f9eea0
|
7
|
+
data.tar.gz: 3f37047b78246cc086e71080c8cc5d72ed327d6833e577a24473bd3667113000ee7fb6f201146c44d73f02a618c41f1990a3680a95de0006f3dd62f7ff0768da
|
data/lib/nested_string_parser.rb
CHANGED
@@ -7,14 +7,40 @@ module NestedStringParser
|
|
7
7
|
def initialize s=nil ; @nesting, @value, @children = (s ? s.gsub(/^( *)[^ ].*/, '\1').length : -1), (s && s.strip), [] ; end
|
8
8
|
def add_child? child ; (child.nesting > self.nesting) ? add_child(child) : parent.add_child?(child) ; end
|
9
9
|
def add_child child ; @children << child ; child.parent = self ; child ; end
|
10
|
+
def nest_child child ; add_child(child).tap { |c| c.nesting = nesting + 1 } ; end
|
10
11
|
def nb? str ; str && str.strip != "" ; end
|
11
12
|
def accept str=nil, *ss ; nb?(str) ? add_child?(Node.new(str)).accept(*ss) : (ss.size > 0 ? self.accept(*ss) : self) ; end
|
12
13
|
def root ; parent ? parent.root : self ; end
|
13
|
-
def format indent ;
|
14
|
+
def format indent ; %[#{indent}#{value}\n#{children.map { |c| c.format "#{indent} " }.join}] ; end
|
15
|
+
def find name ; children.detect { |k| k.value == name } ; end
|
14
16
|
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
# rows is an array of arrays
|
19
|
+
# | A | B | C |
|
20
|
+
# | A | B | D |
|
21
|
+
# | A | E | F |
|
22
|
+
# | A | E | G |
|
23
|
+
# | B | H | J |
|
24
|
+
# | B | H | K |
|
25
|
+
# same as
|
26
|
+
# A
|
27
|
+
# B
|
28
|
+
# C
|
29
|
+
# D
|
30
|
+
# E
|
31
|
+
# F
|
32
|
+
# G
|
33
|
+
# B
|
34
|
+
# H
|
35
|
+
# J
|
36
|
+
# K
|
37
|
+
#
|
38
|
+
def self.from_rows rows, root=new_node
|
39
|
+
rows.each { |row| row.inject(root) { |node, name| node.find(name) || node.nest_child(new_node name) } } ; root
|
40
|
+
end
|
18
41
|
|
19
|
-
def self.
|
42
|
+
def self.new_node val=nil ; NestedStringParser::Node.new.tap { |n| n.value = val } ; end
|
43
|
+
def self.parse str ; new_node.accept(*str.split(/\n/)).root ; end
|
44
|
+
module Extension ; def parse_nested_string ; NestedStringParser.parse self.to_s ; end ; end
|
45
|
+
def self.patch klass=::String ; klass.send :include, Extension ; end
|
20
46
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require "nested_string_parser"
|
3
|
+
|
4
|
+
RSpec::describe NestedStringParser do
|
5
|
+
it "parses nothing at all" do
|
6
|
+
node = NestedStringParser.from_rows []
|
7
|
+
|
8
|
+
expect(node.value ).to eq nil
|
9
|
+
expect(node.nesting ).to eq(-1)
|
10
|
+
expect(node.children).to eq []
|
11
|
+
end
|
12
|
+
|
13
|
+
it "parses a single item" do
|
14
|
+
node = NestedStringParser.from_rows [["hello"]]
|
15
|
+
|
16
|
+
expect(node.value ).to eq nil
|
17
|
+
expect(node.nesting ).to eq(-1)
|
18
|
+
expect(node.children.size).to eq 1
|
19
|
+
|
20
|
+
child = node.children.first
|
21
|
+
expect(child.value ).to eq "hello"
|
22
|
+
expect(child.nesting ).to eq 0
|
23
|
+
expect(child.children).to eq []
|
24
|
+
end
|
25
|
+
|
26
|
+
QUARKS_IN_ROWS = [
|
27
|
+
%w{ quarks G1 up },
|
28
|
+
%w{ quarks G1 down },
|
29
|
+
%w{ quarks G2 strange },
|
30
|
+
%w{ quarks G2 charmed },
|
31
|
+
%w{ quarks G3 top },
|
32
|
+
%w{ quarks G3 bottom },
|
33
|
+
%w{ leptons G1 electron },
|
34
|
+
%w{ leptons G1 electron-neutrino },
|
35
|
+
%w{ leptons G2 muon },
|
36
|
+
%w{ leptons G2 muon-neutrino },
|
37
|
+
%w{ leptons G3 tau },
|
38
|
+
%w{ leptons G3 tau-neutrino },
|
39
|
+
]
|
40
|
+
|
41
|
+
it "parses a single item" do
|
42
|
+
node = NestedStringParser.from_rows QUARKS_IN_ROWS
|
43
|
+
|
44
|
+
expect(node.value ).to eq nil
|
45
|
+
expect(node.nesting ).to eq(-1)
|
46
|
+
expect(node.children.map(&:value)).to eq %w{ quarks leptons }
|
47
|
+
|
48
|
+
quarks = node.children[0]
|
49
|
+
expect(quarks.nesting ).to eq 0
|
50
|
+
expect(quarks.children.map(&:value) ).to eq %w{ G1 G2 G3 }
|
51
|
+
expect(quarks.children.map(&:nesting)).to eq [1,1,1]
|
52
|
+
expect(quarks.children.map { |q| q.children.map(&:nesting) }).to eq [[2,2], [2,2], [2,2]]
|
53
|
+
expect(quarks.children.map { |q| q.children.map(&:value) }).to eq [%w{up down}, %w{strange charmed}, %w{top bottom}]
|
54
|
+
|
55
|
+
leptons = node.children[1]
|
56
|
+
expect(leptons.nesting ).to eq 0
|
57
|
+
expect(leptons.children.map(&:value) ).to eq %w{ G1 G2 G3 }
|
58
|
+
expect(leptons.children.map(&:nesting)).to eq [1,1,1]
|
59
|
+
expect(leptons.children.map { |q| q.children.map(&:nesting) }).to eq [[2,2], [2,2], [2,2]]
|
60
|
+
expect(leptons.children.map { |q| q.children.map(&:value) }).to eq [%w{electron electron-neutrino}, %w{muon muon-neutrino}, %w{tau tau-neutrino}]
|
61
|
+
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_string_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Conan Dalton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- lib/nested_string_parser/version.rb
|
70
70
|
- mit-license.txt
|
71
71
|
- nested_string_parser.gemspec
|
72
|
+
- spec/from_rows_spec.rb
|
72
73
|
- spec/parse_spec.rb
|
73
74
|
- spec/spec_helper.rb
|
74
75
|
homepage: http://github.com/conanite/nested_string_parser
|
@@ -97,5 +98,6 @@ specification_version: 4
|
|
97
98
|
summary: Parse multi-line string with indentation, return a structure where nesting
|
98
99
|
is based on line indentation
|
99
100
|
test_files:
|
101
|
+
- spec/from_rows_spec.rb
|
100
102
|
- spec/parse_spec.rb
|
101
103
|
- spec/spec_helper.rb
|