abstract_mapper 0.0.2 → 0.1.0

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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -4
  3. data/CHANGELOG.md +26 -0
  4. data/README.md +5 -5
  5. data/Rakefile +1 -1
  6. data/abstract_mapper.gemspec +1 -0
  7. data/lib/abstract_mapper.rb +4 -15
  8. data/lib/abstract_mapper/ast.rb +16 -0
  9. data/lib/abstract_mapper/ast/branch.rb +121 -0
  10. data/lib/abstract_mapper/ast/node.rb +91 -0
  11. data/lib/abstract_mapper/builder.rb +2 -2
  12. data/lib/abstract_mapper/commands.rb +4 -2
  13. data/lib/abstract_mapper/commands/base.rb +69 -0
  14. data/lib/abstract_mapper/dsl.rb +2 -2
  15. data/lib/abstract_mapper/errors.rb +17 -0
  16. data/lib/abstract_mapper/errors/wrong_node.rb +1 -1
  17. data/lib/abstract_mapper/errors/wrong_rule.rb +1 -1
  18. data/lib/abstract_mapper/optimizer.rb +1 -1
  19. data/lib/abstract_mapper/rules.rb +10 -5
  20. data/lib/abstract_mapper/rules/base.rb +74 -0
  21. data/lib/abstract_mapper/rules/pair.rb +68 -0
  22. data/lib/abstract_mapper/rules/sole.rb +60 -0
  23. data/lib/abstract_mapper/settings.rb +22 -30
  24. data/lib/abstract_mapper/version.rb +1 -1
  25. data/lib/rspec/nodes.rb +2 -2
  26. data/spec/integration/faceter.rb +4 -4
  27. data/spec/integration/rspec_examples_spec.rb +0 -6
  28. data/spec/unit/abstract_mapper/{branch_spec.rb → ast/branch_spec.rb} +28 -61
  29. data/spec/unit/abstract_mapper/{node_spec.rb → ast/node_spec.rb} +16 -53
  30. data/spec/unit/abstract_mapper/builder_spec.rb +9 -24
  31. data/spec/unit/abstract_mapper/{command_spec.rb → commands/base_spec.rb} +10 -25
  32. data/spec/unit/abstract_mapper/commands_spec.rb +9 -14
  33. data/spec/unit/abstract_mapper/dsl_spec.rb +23 -15
  34. data/spec/unit/abstract_mapper/errors/unknown_command_spec.rb +1 -4
  35. data/spec/unit/abstract_mapper/errors/wrong_node_spec.rb +5 -4
  36. data/spec/unit/abstract_mapper/errors/wrong_rule_spec.rb +5 -5
  37. data/spec/unit/abstract_mapper/functions/compact_spec.rb +0 -2
  38. data/spec/unit/abstract_mapper/functions/filter_spec.rb +0 -2
  39. data/spec/unit/abstract_mapper/functions/identity_spec.rb +0 -2
  40. data/spec/unit/abstract_mapper/functions/restrict_spec.rb +0 -3
  41. data/spec/unit/abstract_mapper/functions/subclass_spec.rb +0 -2
  42. data/spec/unit/abstract_mapper/optimizer_spec.rb +13 -17
  43. data/spec/unit/abstract_mapper/{rule_spec.rb → rules/base_spec.rb} +17 -34
  44. data/spec/unit/abstract_mapper/{pair_rule_spec.rb → rules/pair_spec.rb} +8 -8
  45. data/spec/unit/abstract_mapper/{sole_rule_spec.rb → rules/sole_spec.rb} +5 -5
  46. data/spec/unit/abstract_mapper/rules_spec.rb +24 -37
  47. data/spec/unit/abstract_mapper/settings_spec.rb +38 -32
  48. data/spec/unit/abstract_mapper_spec.rb +9 -16
  49. metadata +37 -22
  50. data/lib/abstract_mapper/attributes.rb +0 -65
  51. data/lib/abstract_mapper/branch.rb +0 -120
  52. data/lib/abstract_mapper/command.rb +0 -68
  53. data/lib/abstract_mapper/node.rb +0 -87
  54. data/lib/abstract_mapper/pair_rule.rb +0 -64
  55. data/lib/abstract_mapper/rule.rb +0 -73
  56. data/lib/abstract_mapper/sole_rule.rb +0 -56
@@ -9,7 +9,6 @@ describe "rspec examples" do
9
9
  include_context "Faceter"
10
10
 
11
11
  describe "Faceter::Rename" do
12
-
13
12
  let(:described_class) { Faceter::Rename }
14
13
 
15
14
  it_behaves_like :creating_immutable_node do
@@ -22,19 +21,15 @@ describe "rspec examples" do
22
21
  let(:input) { { foo: :FOO, baz: :BAZ } }
23
22
  let(:output) { { bar: :FOO, baz: :BAZ } }
24
23
  end
25
-
26
24
  end # describe Faceter::Rename
27
25
 
28
26
  describe "Faceter::List" do
29
-
30
27
  let(:described_class) { Faceter::List }
31
28
 
32
29
  it_behaves_like :creating_immutable_branch
33
-
34
30
  end # describe Faceter::List
35
31
 
36
32
  describe "Faceter::CompactLists" do
37
-
38
33
  let(:described_class) { Faceter::CompactLists }
39
34
 
40
35
  it_behaves_like :skipping_nodes do
@@ -45,7 +40,6 @@ describe "rspec examples" do
45
40
  let(:input) { [Faceter::List.new { [1] }, Faceter::List.new { [2] }] }
46
41
  let(:output) { Faceter::List.new { [1, 2] } }
47
42
  end
48
-
49
43
  end # describe Faceter::CompactLists
50
44
 
51
45
  end # describe mapper definition
@@ -1,9 +1,9 @@
1
1
  # encoding: utf-8
2
2
 
3
- describe AbstractMapper::Branch do
3
+ describe AbstractMapper::AST::Branch do
4
4
 
5
5
  let(:foo) do
6
- class Foo < AbstractMapper::Node
6
+ class Foo < AbstractMapper::AST::Node
7
7
  attribute :foo
8
8
 
9
9
  def transproc
@@ -12,7 +12,6 @@ describe AbstractMapper::Branch do
12
12
  end
13
13
  Foo
14
14
  end
15
-
16
15
  let(:node1) { foo.new foo: :FOO }
17
16
  let(:node2) { foo.new foo: :BAR }
18
17
  let(:node3) { foo.new foo: :BAZ }
@@ -24,63 +23,51 @@ describe AbstractMapper::Branch do
24
23
  end
25
24
 
26
25
  describe ".new" do
27
-
28
26
  subject { branch }
29
27
 
30
28
  it { is_expected.to be_kind_of Enumerable }
31
29
  it { is_expected.to be_frozen }
32
30
 
33
31
  context "without a block" do
34
-
35
32
  subject { test.new(foo: :FOO).entries }
36
- it { is_expected.to be_empty }
37
33
 
38
- end # context
34
+ it { is_expected.to be_empty }
35
+ end
39
36
 
40
37
  context "with a block" do
41
-
42
38
  subject { test.new(foo: :FOO) { [node1, node2] }.entries }
43
- it { is_expected.to eql [node1, node2] }
44
-
45
- end # context
46
39
 
40
+ it { is_expected.to eql [node1, node2] }
41
+ end
47
42
  end # describe .new
48
43
 
49
44
  describe "#attributes" do
50
-
51
45
  subject { branch.attributes }
52
46
 
53
47
  context "initialized" do
54
-
55
48
  it { is_expected.to eql(foo: :FOO) }
56
-
57
- end # context
49
+ end
58
50
 
59
51
  context "not initialized" do
60
-
61
52
  let(:branch) { test.new }
62
- it { is_expected.to eql(foo: nil) }
63
53
 
64
- end # context
54
+ it { is_expected.to eql(foo: nil) }
55
+ end
65
56
 
66
57
  context "not defined" do
67
-
68
58
  let(:branch) { described_class.new }
69
- it { is_expected.to eql({}) }
70
-
71
- end # context
72
59
 
60
+ it { is_expected.to eql({}) }
61
+ end
73
62
  end # describe #attributes
74
63
 
75
64
  describe "#block" do
76
-
77
65
  subject { branch.block }
78
- it { is_expected.to be_nil }
79
66
 
67
+ it { is_expected.to be_nil }
80
68
  end # describe #block
81
69
 
82
70
  describe "#update" do
83
-
84
71
  subject { branch.update { [node2, node3] } }
85
72
 
86
73
  it { is_expected.to be_kind_of test }
@@ -92,38 +79,30 @@ describe AbstractMapper::Branch do
92
79
  it "assings new subnodes from a block" do
93
80
  expect(subject.entries).to eql [node2, node3]
94
81
  end
95
-
96
82
  end # describe #update
97
83
 
98
84
  describe "#each" do
99
-
100
85
  let(:subnodes) { [node1, node2] }
101
86
 
102
87
  context "with a block" do
103
-
104
88
  subject { branch.each { |item| item } }
105
89
 
106
90
  it "looks over subnodes" do
107
91
  expect(subject).to eql subnodes
108
92
  end
109
-
110
- end # context
93
+ end
111
94
 
112
95
  context "without a block" do
113
-
114
96
  subject { branch.each }
115
97
 
116
98
  it "returns Enumerator for subnodes" do
117
99
  expect(subject).to be_kind_of Enumerator
118
100
  expect(subject.entries).to eql subnodes
119
101
  end
120
-
121
- end # context
122
-
102
+ end
123
103
  end # describe #each
124
104
 
125
105
  describe "#<<" do
126
-
127
106
  subject { branch << node3 }
128
107
 
129
108
  it "returns the branch with updated subnodes and the same attributes" do
@@ -131,73 +110,61 @@ describe AbstractMapper::Branch do
131
110
  expect(subject.attributes).to eql branch.attributes
132
111
  expect(subject.entries).to eql [node1, node2, node3]
133
112
  end
134
-
135
113
  end # describe #<<
136
114
 
137
115
  describe "#transproc" do
116
+ subject { branch.transproc[data] }
138
117
 
139
118
  let(:data) { "baz" }
140
119
 
141
- subject { branch.transproc[data] }
142
120
  it { is_expected.to eql "baz-1-1" }
143
-
144
121
  end # describe #transproc
145
122
 
146
123
  describe "#to_s" do
147
-
148
124
  subject { branch.to_s }
149
125
 
150
126
  context "of the root" do
151
-
152
127
  let(:branch) { described_class.new { [node1, node2] } }
153
- it { is_expected.to eql "Root [#{node1.inspect}, #{node2.inspect}]" }
154
128
 
155
- end # context
129
+ it { is_expected.to eql "Root [#{node1.inspect}, #{node2.inspect}]" }
130
+ end
156
131
 
157
132
  context "of the specific node" do
158
-
159
133
  let(:branch) { test.new(foo: :FOO) { [node1, node2] } }
134
+
160
135
  it do
161
136
  is_expected
162
137
  .to eql "Foo(foo: :FOO) [#{node1.inspect}, #{node2.inspect}]"
163
138
  end
164
-
165
- end # context
166
-
139
+ end
167
140
  end # describe #to_s
168
141
 
169
142
  describe "#eql?" do
170
-
171
143
  subject { branch.eql? other }
172
144
 
173
145
  context "with the same type, attributes and subnodes" do
174
-
175
146
  let(:other) { test.new(foo: :FOO) { [node1, node2] } }
176
- it { is_expected.to eql true }
177
147
 
178
- end # context
148
+ it { is_expected.to eql true }
149
+ end
179
150
 
180
151
  context "with another type" do
181
-
182
152
  let(:other) { Class.new(test).new(foo: :FOO) { [node1, node2] } }
183
- it { is_expected.to eql false }
184
153
 
185
- end # context
154
+ it { is_expected.to eql false }
155
+ end
186
156
 
187
157
  context "with other attributes" do
188
-
189
158
  let(:other) { test.new(foo: :BAR) { [node1, node2] } }
190
- it { is_expected.to eql false }
191
159
 
192
- end # context
160
+ it { is_expected.to eql false }
161
+ end
193
162
 
194
163
  context "with other subnodes" do
195
-
196
164
  let(:other) { test.new(foo: :FOO) { [node1, node3] } }
197
- it { is_expected.to eql false }
198
-
199
- end # context
200
165
 
166
+ it { is_expected.to eql false }
167
+ end
201
168
  end # describe #eql?
202
169
 
203
- end # describe Branch
170
+ end # describe AbstractMapper::AST::Branch
@@ -2,7 +2,7 @@
2
2
 
3
3
  class AbstractMapper # namespace
4
4
 
5
- describe AbstractMapper::Node do
5
+ describe AbstractMapper::AST::Node do
6
6
 
7
7
  let(:test) do
8
8
  Test::Node = Class.new(described_class) do
@@ -16,7 +16,6 @@ class AbstractMapper # namespace
16
16
  let(:block) { nil }
17
17
 
18
18
  describe ".new" do
19
-
20
19
  subject { node }
21
20
 
22
21
  it "initializes attributes" do
@@ -29,127 +28,91 @@ class AbstractMapper # namespace
29
28
  it "doesn't freeze the source" do
30
29
  expect { subject }.not_to change { attributes.frozen? }
31
30
  end
32
-
33
31
  end # describe .new
34
32
 
35
- describe ".attributes" do
36
-
37
- it "is declared via DSL" do
38
- expect(test.attributes).to eql(foo: nil, bar: :BAR)
39
- end
40
-
41
- it "is inheritable" do
42
- subklass = Class.new(test)
43
- expect(subklass.attributes).to eql(test.attributes)
44
- end
45
-
46
- end # describe .attributes
47
-
48
33
  describe "#attributes" do
49
-
50
34
  subject { node.attributes }
51
35
 
52
36
  it { is_expected.to eql({ foo: nil, bar: :BAR }.merge(attributes)) }
53
37
 
54
38
  context "by default" do
55
-
56
39
  let(:node) { test.new }
57
- it { is_expected.to eql(foo: nil, bar: :BAR) }
58
-
59
- end # context
60
40
 
41
+ it { is_expected.to eql(foo: nil, bar: :BAR) }
42
+ end
61
43
  end # describe #attributes
62
44
 
63
45
  describe "#block" do
64
-
65
46
  subject { node.block }
66
47
 
67
48
  context "when block is absent" do
68
-
69
49
  it { is_expected.to eql nil }
70
-
71
50
  end # context
72
51
 
73
52
  context "when block is present" do
74
-
75
- let(:block) { proc { :foo } }
53
+ let(:block) { proc { :foo } }
76
54
  it { is_expected.to eql block }
77
-
78
- end # context
79
-
55
+ end
80
56
  end # describe #block
81
57
 
82
58
  describe "#to_s" do
83
-
84
59
  subject { node.to_s }
85
60
 
86
61
  context "with uninitialized attributes" do
87
-
88
62
  let(:node) { test.new }
89
- it { is_expected.to eql "Node(foo: nil, bar: :BAR)" }
90
63
 
91
- end # context
64
+ it { is_expected.to eql "Node(foo: nil, bar: :BAR)" }
65
+ end
92
66
 
93
67
  context "with initialized attributes" do
94
-
95
68
  let(:node) { test.new(attributes) }
96
- it { is_expected.to eql "Node(foo: :FOO, bar: :BAR)" }
97
69
 
98
- end # context
70
+ it { is_expected.to eql "Node(foo: :FOO, bar: :BAR)" }
71
+ end
99
72
 
100
73
  context "without attributes" do
101
-
102
74
  let(:node) { described_class.new }
103
- it { is_expected.to eql "Node" }
104
-
105
- end # context
106
75
 
76
+ it { is_expected.to eql "Node" }
77
+ end
107
78
  end # describe #to_s
108
79
 
109
80
  describe "#==" do
110
-
111
81
  subject { node == other }
112
82
 
113
83
  context "node with the same type and attributes" do
114
-
115
84
  let(:other) { test.new(attributes) }
116
- it { is_expected.to eql true }
117
85
 
86
+ it { is_expected.to eql true }
118
87
  end # context
119
88
 
120
89
  context "node with other type" do
121
-
122
90
  let(:other) { Class.new(test).new(attributes) }
123
- it { is_expected.to eql false }
124
91
 
92
+ it { is_expected.to eql false }
125
93
  end # context
126
94
 
127
95
  context "node with other attributes" do
128
-
129
96
  let(:other) { test.new }
130
- it { is_expected.to eql false }
131
97
 
98
+ it { is_expected.to eql false }
132
99
  end # context
133
-
134
100
  end # describe #==
135
101
 
136
102
  describe "#inspect" do
137
-
138
103
  subject { node.inspect }
139
- it { is_expected.to eql "<Node(foo: :FOO, bar: :BAR)>" }
140
104
 
105
+ it { is_expected.to eql "<Node(foo: :FOO, bar: :BAR)>" }
141
106
  end # describe #inspect
142
107
 
143
108
  describe "#transproc" do
144
-
145
109
  subject { node.transproc }
146
110
 
147
111
  it "returns the identity function" do
148
112
  expect(subject[:foo]).to eql :foo
149
113
  end
150
-
151
114
  end # describe #transproc
152
115
 
153
- end # describe AbstractMapper::Node
116
+ end # describe AbstractMapper::AST::Node
154
117
 
155
118
  end # class AbstractMapper
@@ -4,64 +4,53 @@ class AbstractMapper
4
4
 
5
5
  describe AbstractMapper::Builder do
6
6
 
7
- let!(:test) { Class.new(described_class) }
8
- let!(:foo) { Test::Foo = Class.new(Node) { attribute :foo } }
9
- let!(:bar) { Test::Bar = Class.new(Branch) }
7
+ let!(:test) { Class.new(described_class) }
8
+ let!(:foo) { Test::Foo = Class.new(AST::Node) { attribute :foo } }
9
+ let!(:bar) { Test::Bar = Class.new(AST::Branch) }
10
10
 
11
11
  let(:builder) { test.new tree }
12
12
  let(:tree) { Test::Foo.new }
13
13
  let(:commands) { Commands.new << [:foo, Test::Foo] << [:bar, Test::Bar] }
14
14
 
15
15
  describe ".commands=" do
16
-
17
16
  subject { test.commands = commands }
18
17
 
19
18
  it "sets the commands" do
20
19
  expect { subject }.to change { test.commands }.to commands
21
20
  end
22
-
23
21
  end # describe .commands=
24
22
 
25
23
  describe ".update" do
26
-
27
24
  before { test.commands = commands }
28
25
 
29
26
  context "by default" do
30
-
31
27
  subject { test.update }
32
28
 
33
29
  it "is an empty branch" do
34
- expect(subject).to be_instance_of AbstractMapper::Branch
30
+ expect(subject).to be_instance_of AST::Branch
35
31
  expect(subject.entries).to be_empty
36
32
  end
37
-
38
- end # context
33
+ end
39
34
 
40
35
  context "initialized" do
41
-
42
36
  subject { test.update(tree) }
43
37
 
44
38
  it "returns exactly the same tree" do
45
39
  expect(subject.inspect).to eql(tree.inspect)
46
40
  end
47
-
48
- end # context
41
+ end
49
42
 
50
43
  context "with a block" do
51
-
52
44
  subject { test.update { bar { foo(foo: :FOO) { fail } } } }
53
45
 
54
46
  it "is built" do
55
47
  expect(subject.inspect).to eql "<Root [<Bar [<Foo(foo: :FOO)>]>]>"
56
48
  expect(subject.first.first.block).not_to be_nil
57
49
  end
58
-
59
- end # context
60
-
50
+ end
61
51
  end # describe .update
62
52
 
63
53
  describe ".new" do
64
-
65
54
  subject { builder }
66
55
 
67
56
  it { is_expected.to be_frozen }
@@ -69,11 +58,9 @@ class AbstractMapper
69
58
  it "doesn't freezes the tree" do
70
59
  expect { subject }.not_to change { tree.frozen? }
71
60
  end
72
-
73
61
  end # describe .new
74
62
 
75
63
  describe "#tree" do
76
-
77
64
  subject { builder.tree }
78
65
 
79
66
  it "returns exactly the same tree as initialized" do
@@ -81,16 +68,14 @@ class AbstractMapper
81
68
  end
82
69
 
83
70
  it { is_expected.to be_frozen }
84
-
85
71
  end # describe #tree
86
72
 
87
73
  describe "#respond_to?" do
88
-
89
74
  subject { builder.respond_to? :arbitrary_method }
90
- it { is_expected.to eql true }
91
75
 
76
+ it { is_expected.to eql true }
92
77
  end # describe #respond_to?
93
78
 
94
79
  end # describe AbstractMapper::Builder
95
80
 
96
- end
81
+ end # class AbstractMapper