rip-parser 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da07388d321262375ccaa89af4d7bbc22464a05c
4
- data.tar.gz: 5565c4dbfcd9702157839b6aa0e6ce9cc7eccfee
3
+ metadata.gz: b5b0926d469ebb45500d16f544f14c4e4d52363d
4
+ data.tar.gz: 2b8690bb493360e6fa32fd75f6e6761b12d3a2d3
5
5
  SHA512:
6
- metadata.gz: 7e37d5077ae71f610effe5840e2f10c715d5460d2c523edde202b7b01acc1ab1b54d36f918928e9f505b936c49febcf7177e5413b339391aad8c8a5b76c5c4d1
7
- data.tar.gz: 9446353584fdddf852fb1472e67a2e462510ede2c9645165072a97961de481fe647a62ca7ccffb337a2476d38230f0c51378e129fa6fb4976be6a11cbfc6e078
6
+ metadata.gz: ef663c6e862950bdcf357402ac070fb3c371fba4f6780518040023f91b4850c6e5fd85e67183a22d3779e4511838927f03d930cca8f87a8ec3705bc57343722a
7
+ data.tar.gz: fcfa4fd0a5580c5ddd308a53be565a4650b418fda6490c5b7ae71edcbc28ebdc0066512043fa3abc44673b2b460a4bcb6aeb71dc1df655a63afd8b9286846de7
@@ -2,7 +2,7 @@ module Rip
2
2
  module Parser
3
3
  module About
4
4
  def self.version
5
- '0.1.0'
5
+ '0.1.1'
6
6
  end
7
7
  end
8
8
  end
@@ -46,20 +46,35 @@ module Rip::Parser
46
46
  self.class.new(extra.merge(other.to_h).merge(location: location, type: other[:type] || type))
47
47
  end
48
48
 
49
- def s_expression
49
+ def to_h(include_location: true)
50
50
  _extra = extra.map do |key, value|
51
- [ key, self.class.try_convert_s_expression(value) ]
51
+ [ key, self.class.try_convert_to_h(value, include_location) ]
52
52
  end.to_h
53
53
 
54
- { type: type }.merge(_extra)
54
+ if include_location
55
+ { location: location, type: type }
56
+ else
57
+ { type: type }
58
+ end.merge(_extra)
55
59
  end
56
60
 
57
- def to_h
61
+ def traverse(&callback)
58
62
  _extra = extra.map do |key, value|
59
- [ key, self.class.try_convert_to_h(value) ]
63
+ _value = case value
64
+ when Array
65
+ value.map do |v|
66
+ v.traverse(&callback)
67
+ end
68
+ when self.class
69
+ value.traverse(&callback)
70
+ else
71
+ value
72
+ end
73
+
74
+ [ key, _value ]
60
75
  end.to_h
61
76
 
62
- { location: location, type: type }.merge(_extra)
77
+ callback.call(merge(_extra))
63
78
  end
64
79
 
65
80
  private
@@ -90,23 +105,14 @@ module Rip::Parser
90
105
  end
91
106
  end
92
107
 
93
- def self.try_convert_to_h(value)
94
- case value
95
- when Array
96
- value.map(&method(:try_convert_to_h))
97
- when self
98
- value.to_h
99
- else
100
- value
101
- end
102
- end
103
-
104
- def self.try_convert_s_expression(value)
108
+ def self.try_convert_to_h(value, include_location)
105
109
  case value
106
110
  when Array
107
- value.map(&method(:try_convert_s_expression))
111
+ value.map do |v|
112
+ try_convert_to_h(v, include_location)
113
+ end
108
114
  when self
109
- value.s_expression
115
+ value.to_h(include_location: include_location)
110
116
  else
111
117
  value
112
118
  end
@@ -62,29 +62,6 @@ RSpec.describe Rip::Parser::Node do
62
62
  specify { expect(node.merge(other).to_h).to eq(location: location, type: :other_test, answer: 42, foo: :bar) }
63
63
  end
64
64
 
65
- describe '#s_expression' do
66
- let(:tree) { Rip::Parser::Node.new(location: location, type: :root, children: [ node ]) }
67
-
68
- let(:expected) do
69
- {
70
- type: :root,
71
- children: [
72
- {
73
- type: :test,
74
- answer: 42
75
- }
76
- ]
77
- }
78
- end
79
-
80
- specify { expect(tree.s_expression).to eq(expected) }
81
- specify { expect(tree.s_expression).to be_a(Hash) }
82
-
83
- specify { expect(tree.s_expression.keys).to eq([ :type, :children ]) }
84
-
85
- specify { expect(tree.s_expression[:children].first).to be_a(Hash) }
86
- end
87
-
88
65
  describe '#to_h' do
89
66
  specify { expect(node.to_h.keys).to eq([ :location, :type, :answer ]) }
90
67
 
@@ -106,6 +83,52 @@ RSpec.describe Rip::Parser::Node do
106
83
  specify { expect(tree.to_h).to eq(expected) }
107
84
  specify { expect(tree.to_h[:other]).to be_a(Hash) }
108
85
  end
86
+
87
+ context 'include_location: false' do
88
+ let(:tree) { Rip::Parser::Node.new(location: location, type: :root, children: [ node ]) }
89
+
90
+ let(:expected) do
91
+ {
92
+ type: :root,
93
+ children: [
94
+ {
95
+ type: :test,
96
+ answer: 42
97
+ }
98
+ ]
99
+ }
100
+ end
101
+
102
+ specify { expect(tree.to_h(include_location: false).keys).to eq([ :type, :children ]) }
103
+
104
+ specify { expect(tree.to_h(include_location: false)[:children].first.keys).to eq([ :type, :answer ]) }
105
+ end
106
+ end
107
+
108
+ describe '#traverse' do
109
+ let(:node_counts) { Hash.new { |h, k| h[k] = 0 } }
110
+
111
+ let(:tree) do
112
+ Rip::Parser::Node.new(
113
+ location: location,
114
+ type: :root,
115
+ answer: 42,
116
+ nested: [
117
+ { location: location, type: :leaf, data: :aaa },
118
+ { location: location, type: :leaf, data: :bbb },
119
+ { location: location, type: :leaf, data: :ccc }
120
+ ],
121
+ aux: {
122
+ location: location,
123
+ type: :auxiliary,
124
+ whatever: :anything
125
+ }
126
+ )
127
+ end
128
+
129
+ before { tree.traverse { |node| node_counts[node.type] += 1 } }
130
+
131
+ specify { expect(node_counts).to eq(root: 1, leaf: 3, auxiliary: 1) }
109
132
  end
110
133
 
111
134
  describe '.new' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rip-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Ingram