squeel_rbg 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +4 -0
  2. data/.yardopts +3 -0
  3. data/Gemfile +13 -0
  4. data/LICENSE +20 -0
  5. data/README.md +398 -0
  6. data/Rakefile +19 -0
  7. data/lib/core_ext/hash.rb +13 -0
  8. data/lib/core_ext/symbol.rb +39 -0
  9. data/lib/squeel/adapters/active_record/3.0/association_preload.rb +15 -0
  10. data/lib/squeel/adapters/active_record/3.0/compat.rb +142 -0
  11. data/lib/squeel/adapters/active_record/3.0/context.rb +66 -0
  12. data/lib/squeel/adapters/active_record/3.0/join_association.rb +54 -0
  13. data/lib/squeel/adapters/active_record/3.0/join_dependency.rb +84 -0
  14. data/lib/squeel/adapters/active_record/3.0/relation.rb +327 -0
  15. data/lib/squeel/adapters/active_record/context.rb +66 -0
  16. data/lib/squeel/adapters/active_record/join_association.rb +44 -0
  17. data/lib/squeel/adapters/active_record/join_dependency.rb +83 -0
  18. data/lib/squeel/adapters/active_record/preloader.rb +21 -0
  19. data/lib/squeel/adapters/active_record/relation.rb +351 -0
  20. data/lib/squeel/adapters/active_record.rb +28 -0
  21. data/lib/squeel/configuration.rb +54 -0
  22. data/lib/squeel/constants.rb +24 -0
  23. data/lib/squeel/context.rb +67 -0
  24. data/lib/squeel/dsl.rb +86 -0
  25. data/lib/squeel/nodes/aliasing.rb +13 -0
  26. data/lib/squeel/nodes/and.rb +9 -0
  27. data/lib/squeel/nodes/as.rb +14 -0
  28. data/lib/squeel/nodes/binary.rb +32 -0
  29. data/lib/squeel/nodes/function.rb +66 -0
  30. data/lib/squeel/nodes/join.rb +113 -0
  31. data/lib/squeel/nodes/key_path.rb +192 -0
  32. data/lib/squeel/nodes/nary.rb +45 -0
  33. data/lib/squeel/nodes/not.rb +9 -0
  34. data/lib/squeel/nodes/operation.rb +32 -0
  35. data/lib/squeel/nodes/operators.rb +43 -0
  36. data/lib/squeel/nodes/or.rb +9 -0
  37. data/lib/squeel/nodes/order.rb +53 -0
  38. data/lib/squeel/nodes/predicate.rb +71 -0
  39. data/lib/squeel/nodes/predicate_operators.rb +29 -0
  40. data/lib/squeel/nodes/stub.rb +125 -0
  41. data/lib/squeel/nodes/unary.rb +28 -0
  42. data/lib/squeel/nodes.rb +17 -0
  43. data/lib/squeel/predicate_methods.rb +14 -0
  44. data/lib/squeel/version.rb +3 -0
  45. data/lib/squeel/visitors/attribute_visitor.rb +191 -0
  46. data/lib/squeel/visitors/base.rb +112 -0
  47. data/lib/squeel/visitors/predicate_visitor.rb +319 -0
  48. data/lib/squeel/visitors/symbol_visitor.rb +48 -0
  49. data/lib/squeel/visitors.rb +3 -0
  50. data/lib/squeel.rb +28 -0
  51. data/lib/squeel_rbg.rb +5 -0
  52. data/spec/blueprints/articles.rb +5 -0
  53. data/spec/blueprints/comments.rb +5 -0
  54. data/spec/blueprints/notes.rb +3 -0
  55. data/spec/blueprints/people.rb +4 -0
  56. data/spec/blueprints/tags.rb +3 -0
  57. data/spec/console.rb +22 -0
  58. data/spec/core_ext/symbol_spec.rb +75 -0
  59. data/spec/helpers/squeel_helper.rb +21 -0
  60. data/spec/spec_helper.rb +66 -0
  61. data/spec/squeel/adapters/active_record/context_spec.rb +44 -0
  62. data/spec/squeel/adapters/active_record/join_association_spec.rb +18 -0
  63. data/spec/squeel/adapters/active_record/join_dependency_spec.rb +66 -0
  64. data/spec/squeel/adapters/active_record/relation_spec.rb +627 -0
  65. data/spec/squeel/dsl_spec.rb +92 -0
  66. data/spec/squeel/nodes/function_spec.rb +149 -0
  67. data/spec/squeel/nodes/join_spec.rb +47 -0
  68. data/spec/squeel/nodes/key_path_spec.rb +100 -0
  69. data/spec/squeel/nodes/operation_spec.rb +149 -0
  70. data/spec/squeel/nodes/operators_spec.rb +87 -0
  71. data/spec/squeel/nodes/order_spec.rb +30 -0
  72. data/spec/squeel/nodes/predicate_operators_spec.rb +88 -0
  73. data/spec/squeel/nodes/predicate_spec.rb +50 -0
  74. data/spec/squeel/nodes/stub_spec.rb +198 -0
  75. data/spec/squeel/visitors/attribute_visitor_spec.rb +142 -0
  76. data/spec/squeel/visitors/predicate_visitor_spec.rb +342 -0
  77. data/spec/squeel/visitors/symbol_visitor_spec.rb +42 -0
  78. data/spec/support/schema.rb +104 -0
  79. data/squeel.gemspec +43 -0
  80. metadata +246 -0
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ module Squeel
4
+ describe DSL do
5
+
6
+ it 'evaluates code' do
7
+ result = DSL.eval { {id => 1} }
8
+ result.should be_a Hash
9
+ result.keys.first.should be_a Nodes::Stub
10
+ end
11
+
12
+ it 'creates function nodes when a method has arguments' do
13
+ result = DSL.eval { max(id) }
14
+ result.should be_a Nodes::Function
15
+ result.args.should eq [Nodes::Stub.new(:id)]
16
+ end
17
+
18
+ it 'creates polymorphic join nodes when a method has a single class argument' do
19
+ result = DSL.eval { association(Person) }
20
+ result.should be_a Nodes::Join
21
+ result._klass.should eq Person
22
+ end
23
+
24
+ it 'handles OR between predicates' do
25
+ result = DSL.eval {(name =~ 'Joe%') | (articles.title =~ 'Hello%')}
26
+ result.should be_a Nodes::Or
27
+ result.left.should be_a Nodes::Predicate
28
+ result.right.should be_a Nodes::KeyPath
29
+ result.right.endpoint.should be_a Nodes::Predicate
30
+ end
31
+
32
+ it 'is not a full closure (instance_evals) when the block supplied has no arity' do
33
+ my_class = Class.new do
34
+ def a_method
35
+ 'test'
36
+ end
37
+
38
+ def dsl_test
39
+ DSL.eval {name =~ a_method}
40
+ end
41
+ end
42
+
43
+ obj = my_class.new
44
+ result = obj.dsl_test
45
+ result.should be_a Nodes::Predicate
46
+ result.expr.should eq :name
47
+ result.method_name.should eq :matches
48
+ result.value.should be_a Nodes::Stub
49
+ result.value.symbol.should eq :a_method
50
+ end
51
+
52
+ it 'is a full closure (yields self) when the block supplied has an arity' do
53
+ my_class = Class.new do
54
+ def a_method
55
+ 'test'
56
+ end
57
+
58
+ def dsl_test
59
+ DSL.eval {|q| q.name =~ a_method}
60
+ end
61
+ end
62
+
63
+ obj = my_class.new
64
+ result = obj.dsl_test
65
+ result.should be_a Nodes::Predicate
66
+ result.expr.should eq :name
67
+ result.method_name.should eq :matches
68
+ result.value.should be_a String
69
+ result.value.should eq 'test'
70
+ end
71
+
72
+ describe '#my' do
73
+ it 'allows access to caller instance variables' do
74
+ @test_var = "test"
75
+ result = DSL.eval{my{@test_var}}
76
+ result.should be_a String
77
+ result.should eq @test_var
78
+ end
79
+
80
+ it 'allows access to caller methods' do
81
+ def test_scoped_method
82
+ :name
83
+ end
84
+
85
+ result = DSL.eval{my{test_scoped_method}}
86
+ result.should be_a Symbol
87
+ result.should eq :name
88
+ end
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,149 @@
1
+ module Squeel
2
+ module Nodes
3
+ describe Function do
4
+ before do
5
+ @f = :function.func(1,2,3)
6
+ end
7
+
8
+ Squeel::Constants::PREDICATES.each do |method_name|
9
+ it "creates #{method_name} predicates with no value" do
10
+ predicate = @f.send(method_name)
11
+ predicate.expr.should eq @f
12
+ predicate.method_name.should eq method_name
13
+ predicate.value?.should be_false
14
+ end
15
+
16
+ it "creates #{method_name} predicates with a value" do
17
+ predicate = @f.send(method_name, 'value')
18
+ predicate.expr.should eq @f
19
+ predicate.method_name.should eq method_name
20
+ predicate.value.should eq 'value'
21
+ end
22
+ end
23
+
24
+ Squeel::Constants::PREDICATE_ALIASES.each do |method_name, aliases|
25
+ aliases.each do |aliaz|
26
+ ['', '_any', '_all'].each do |suffix|
27
+ it "creates #{method_name.to_s + suffix} predicates with no value using the alias #{aliaz.to_s + suffix}" do
28
+ predicate = @f.send(aliaz.to_s + suffix)
29
+ predicate.expr.should eq @f
30
+ predicate.method_name.should eq "#{method_name}#{suffix}".to_sym
31
+ predicate.value?.should be_false
32
+ end
33
+
34
+ it "creates #{method_name.to_s + suffix} predicates with a value using the alias #{aliaz.to_s + suffix}" do
35
+ predicate = @f.send((aliaz.to_s + suffix), 'value')
36
+ predicate.expr.should eq @f
37
+ predicate.method_name.should eq "#{method_name}#{suffix}".to_sym
38
+ predicate.value.should eq 'value'
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ it 'creates ascending Order nodes with #asc' do
45
+ order = @f.asc
46
+ order.expr.should eq @f
47
+ order.should be_ascending
48
+ end
49
+
50
+ it 'creates descending Order nodes with #desc' do
51
+ order = @f.desc
52
+ order.expr.should eq @f
53
+ order.should be_descending
54
+ end
55
+
56
+ it 'creates eq predicates with ==' do
57
+ predicate = @f == 1
58
+ predicate.expr.should eq @f
59
+ predicate.method_name.should eq :eq
60
+ predicate.value.should eq 1
61
+ end
62
+
63
+ it 'creates not_eq predicates with ^' do
64
+ predicate = @f ^ 1
65
+ predicate.expr.should eq @f
66
+ predicate.method_name.should eq :not_eq
67
+ predicate.value.should eq 1
68
+ end
69
+
70
+ it 'creates not_eq predicates with !=' do
71
+ predicate = @f != 1
72
+ predicate.expr.should eq @f
73
+ predicate.method_name.should eq :not_eq
74
+ predicate.value.should eq 1
75
+ end if respond_to?('!=')
76
+
77
+ it 'creates in predicates with >>' do
78
+ predicate = @f >> [1,2,3]
79
+ predicate.expr.should eq @f
80
+ predicate.method_name.should eq :in
81
+ predicate.value.should eq [1,2,3]
82
+ end
83
+
84
+ it 'creates not_in predicates with <<' do
85
+ predicate = @f << [1,2,3]
86
+ predicate.expr.should eq @f
87
+ predicate.method_name.should eq :not_in
88
+ predicate.value.should eq [1,2,3]
89
+ end
90
+
91
+ it 'creates matches predicates with =~' do
92
+ predicate = @f =~ '%bob%'
93
+ predicate.expr.should eq @f
94
+ predicate.method_name.should eq :matches
95
+ predicate.value.should eq '%bob%'
96
+ end
97
+
98
+ it 'creates does_not_match predicates with !~' do
99
+ predicate = @f !~ '%bob%'
100
+ predicate.expr.should eq @f
101
+ predicate.method_name.should eq :does_not_match
102
+ predicate.value.should eq '%bob%'
103
+ end if respond_to?('!~')
104
+
105
+ it 'creates gt predicates with >' do
106
+ predicate = @f > 1
107
+ predicate.expr.should eq @f
108
+ predicate.method_name.should eq :gt
109
+ predicate.value.should eq 1
110
+ end
111
+
112
+ it 'creates gteq predicates with >=' do
113
+ predicate = @f >= 1
114
+ predicate.expr.should eq @f
115
+ predicate.method_name.should eq :gteq
116
+ predicate.value.should eq 1
117
+ end
118
+
119
+ it 'creates lt predicates with <' do
120
+ predicate = @f < 1
121
+ predicate.expr.should eq @f
122
+ predicate.method_name.should eq :lt
123
+ predicate.value.should eq 1
124
+ end
125
+
126
+ it 'creates lteq predicates with <=' do
127
+ predicate = @f <= 1
128
+ predicate.expr.should eq @f
129
+ predicate.method_name.should eq :lteq
130
+ predicate.value.should eq 1
131
+ end
132
+
133
+ describe '#as' do
134
+
135
+ it 'aliases the function' do
136
+ @f.as('the_alias')
137
+ @f.alias.should eq 'the_alias'
138
+ end
139
+
140
+ it 'casts the alias to a string' do
141
+ @f.as(:the_alias)
142
+ @f.alias.should eq 'the_alias'
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ module Squeel
4
+ module Nodes
5
+ describe Join do
6
+
7
+ before do
8
+ @j = Join.new :name
9
+ end
10
+
11
+ it 'defaults to Arel::InnerJoin' do
12
+ @j._type.should eq Arel::InnerJoin
13
+ end
14
+
15
+ it 'allows setting join type' do
16
+ @j.outer
17
+ @j._type.should eq Arel::OuterJoin
18
+ end
19
+
20
+ it 'allows setting polymorphic class' do
21
+ @j._klass = Person
22
+ @j.should be_polymorphic
23
+ @j._klass.should eq Person
24
+ end
25
+
26
+ it 'creates a KeyPath when sent an unknown method' do
27
+ keypath = @j.another
28
+ keypath.should be_a KeyPath
29
+ keypath.path_with_endpoint.should eq [@j, Stub.new(:another)]
30
+ end
31
+
32
+ it 'creates a KeyPath with a join endpoint when sent a method with a Class param' do
33
+ keypath = @j.another(Person)
34
+ keypath.should be_a KeyPath
35
+ keypath.path_with_endpoint.should eq [@j, Join.new(:another, Arel::InnerJoin, Person)]
36
+ end
37
+
38
+ it 'creates an absolute keypath with just an endpoint with ~' do
39
+ node = ~@j
40
+ node.should be_a KeyPath
41
+ node.path.should eq []
42
+ node.endpoint.should eq @j
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,100 @@
1
+ module Squeel
2
+ module Nodes
3
+ describe KeyPath do
4
+ before do
5
+ @k = KeyPath.new(:first, :second)
6
+ end
7
+
8
+ it 'appends to its path when endpoint is a Stub' do
9
+ @k.third.fourth.fifth
10
+ @k.path.should eq [:first, :second, :third, :fourth]
11
+ @k.endpoint.should eq Stub.new(:fifth)
12
+ end
13
+
14
+ it 'becomes absolute when prefixed with ~' do
15
+ ~@k.third.fourth.fifth
16
+ @k.path.should eq [:first, :second, :third, :fourth]
17
+ @k.endpoint.should eq Stub.new(:fifth)
18
+ @k.should be_absolute
19
+ end
20
+
21
+ it 'stops appending once its endpoint is not a Stub' do
22
+ @k.third.fourth.fifth == 'cinco'
23
+ @k.endpoint.should eq Predicate.new(Stub.new(:fifth), :eq, 'cinco')
24
+ expect { @k.another }.to raise_error NoMethodError
25
+ end
26
+
27
+ it 'sends missing calls to its endpoint if the endpoint responds to them' do
28
+ @k.third.fourth.fifth.matches('Joe%')
29
+ @k.endpoint.should be_a Predicate
30
+ @k.endpoint.expr.should eq :fifth
31
+ @k.endpoint.method_name.should eq :matches
32
+ @k.endpoint.value.should eq 'Joe%'
33
+ end
34
+
35
+ it 'creates a polymorphic join at its endpoint' do
36
+ @k.third.fourth.fifth(Person)
37
+ @k.endpoint.should be_a Join
38
+ @k.endpoint.should be_polymorphic
39
+ end
40
+
41
+ it 'creates a named function at its endpoint' do
42
+ @k.third.fourth.fifth.max(1,2,3)
43
+ @k.endpoint.should be_a Function
44
+ @k.endpoint.name.should eq :max
45
+ @k.endpoint.args.should eq [1,2,3]
46
+ end
47
+
48
+ it 'creates as nodes with #as' do
49
+ @k.as('other_name')
50
+ as = @k.endpoint
51
+ as.should be_a Squeel::Nodes::As
52
+ as.left.should eq Stub.new(:fourth)
53
+ as.right.should eq 'other_name'
54
+ end
55
+
56
+ it 'creates AND nodes with & if the endpoint responds to &' do
57
+ node = @k.third.fourth.eq('Bob') & Stub.new(:attr).eq('Joe')
58
+ node.should be_a And
59
+ node.children.should eq [@k, Stub.new(:attr).eq('Joe')]
60
+ end
61
+
62
+ it 'raises NoMethodError with & if the endpoint does not respond to &' do
63
+ expect {@k.third.fourth & Stub.new(:attr).eq('Joe')}.to raise_error NoMethodError
64
+ end
65
+
66
+ it 'creates Or nodes with | if the endpoint responds to |' do
67
+ node = @k.third.fourth.eq('Bob') | Stub.new(:attr).eq('Joe')
68
+ node.should be_a Or
69
+ node.left.should eq @k
70
+ node.right.should eq Stub.new(:attr).eq('Joe')
71
+ end
72
+
73
+ it 'raises NoMethodError with | if the endpoint does not respond to |' do
74
+ expect {@k.third.fourth | Stub.new(:attr).eq('Joe')}.to raise_error NoMethodError
75
+ end
76
+
77
+ it 'creates Operation nodes with - if the endpoint responds to -' do
78
+ node = @k.third.fourth - 4
79
+ node.should be_an Operation
80
+ node.left.should eq @k
81
+ node.right.should eq 4
82
+ end
83
+
84
+ it 'raises NoMethodError with - if the endpoint does not respond to -' do
85
+ expect {@k.third.fourth(Person) - Stub.new(:attr).eq('Joe')}.to raise_error NoMethodError
86
+ end
87
+
88
+ it 'creates NOT nodes with -@ if the endpoint responds to -@' do
89
+ node = - @k.third.fourth.eq('Bob')
90
+ node.should be_a Not
91
+ node.expr.should eq @k
92
+ end
93
+
94
+ it 'raises NoMethodError with -@ if the endpoint does not respond to -@' do
95
+ expect {-@k.third.fourth}.to raise_error NoMethodError
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,149 @@
1
+ module Squeel
2
+ module Nodes
3
+ describe Operation do
4
+ before do
5
+ @o = dsl{name + 1}
6
+ end
7
+
8
+ Squeel::Constants::PREDICATES.each do |method_name|
9
+ it "creates #{method_name} predicates with no value" do
10
+ predicate = @o.send(method_name)
11
+ predicate.expr.should eq @o
12
+ predicate.method_name.should eq method_name
13
+ predicate.value?.should be_false
14
+ end
15
+
16
+ it "creates #{method_name} predicates with a value" do
17
+ predicate = @o.send(method_name, 'value')
18
+ predicate.expr.should eq @o
19
+ predicate.method_name.should eq method_name
20
+ predicate.value.should eq 'value'
21
+ end
22
+ end
23
+
24
+ Squeel::Constants::PREDICATE_ALIASES.each do |method_name, aliases|
25
+ aliases.each do |aliaz|
26
+ ['', '_any', '_all'].each do |suffix|
27
+ it "creates #{method_name.to_s + suffix} predicates with no value using the alias #{aliaz.to_s + suffix}" do
28
+ predicate = @o.send(aliaz.to_s + suffix)
29
+ predicate.expr.should eq @o
30
+ predicate.method_name.should eq "#{method_name}#{suffix}".to_sym
31
+ predicate.value?.should be_false
32
+ end
33
+
34
+ it "creates #{method_name.to_s + suffix} predicates with a value using the alias #{aliaz.to_s + suffix}" do
35
+ predicate = @o.send((aliaz.to_s + suffix), 'value')
36
+ predicate.expr.should eq @o
37
+ predicate.method_name.should eq "#{method_name}#{suffix}".to_sym
38
+ predicate.value.should eq 'value'
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ it 'creates ascending Order nodes with #asc' do
45
+ order = @o.asc
46
+ order.expr.should eq @o
47
+ order.should be_ascending
48
+ end
49
+
50
+ it 'creates descending Order nodes with #desc' do
51
+ order = @o.desc
52
+ order.expr.should eq @o
53
+ order.should be_descending
54
+ end
55
+
56
+ it 'creates eq predicates with ==' do
57
+ predicate = @o == 1
58
+ predicate.expr.should eq @o
59
+ predicate.method_name.should eq :eq
60
+ predicate.value.should eq 1
61
+ end
62
+
63
+ it 'creates not_eq predicates with ^' do
64
+ predicate = @o ^ 1
65
+ predicate.expr.should eq @o
66
+ predicate.method_name.should eq :not_eq
67
+ predicate.value.should eq 1
68
+ end
69
+
70
+ it 'creates not_eq predicates with !=' do
71
+ predicate = @o != 1
72
+ predicate.expr.should eq @o
73
+ predicate.method_name.should eq :not_eq
74
+ predicate.value.should eq 1
75
+ end if respond_to?('!=')
76
+
77
+ it 'creates in predicates with >>' do
78
+ predicate = @o >> [1,2,3]
79
+ predicate.expr.should eq @o
80
+ predicate.method_name.should eq :in
81
+ predicate.value.should eq [1,2,3]
82
+ end
83
+
84
+ it 'creates not_in predicates with <<' do
85
+ predicate = @o << [1,2,3]
86
+ predicate.expr.should eq @o
87
+ predicate.method_name.should eq :not_in
88
+ predicate.value.should eq [1,2,3]
89
+ end
90
+
91
+ it 'creates matches predicates with =~' do
92
+ predicate = @o =~ '%bob%'
93
+ predicate.expr.should eq @o
94
+ predicate.method_name.should eq :matches
95
+ predicate.value.should eq '%bob%'
96
+ end
97
+
98
+ it 'creates does_not_match predicates with !~' do
99
+ predicate = @o !~ '%bob%'
100
+ predicate.expr.should eq @o
101
+ predicate.method_name.should eq :does_not_match
102
+ predicate.value.should eq '%bob%'
103
+ end if respond_to?('!~')
104
+
105
+ it 'creates gt predicates with >' do
106
+ predicate = @o > 1
107
+ predicate.expr.should eq @o
108
+ predicate.method_name.should eq :gt
109
+ predicate.value.should eq 1
110
+ end
111
+
112
+ it 'creates gteq predicates with >=' do
113
+ predicate = @o >= 1
114
+ predicate.expr.should eq @o
115
+ predicate.method_name.should eq :gteq
116
+ predicate.value.should eq 1
117
+ end
118
+
119
+ it 'creates lt predicates with <' do
120
+ predicate = @o < 1
121
+ predicate.expr.should eq @o
122
+ predicate.method_name.should eq :lt
123
+ predicate.value.should eq 1
124
+ end
125
+
126
+ it 'creates lteq predicates with <=' do
127
+ predicate = @o <= 1
128
+ predicate.expr.should eq @o
129
+ predicate.method_name.should eq :lteq
130
+ predicate.value.should eq 1
131
+ end
132
+
133
+ describe '#as' do
134
+
135
+ it 'aliases the function' do
136
+ @o.as('the_alias')
137
+ @o.alias.should eq 'the_alias'
138
+ end
139
+
140
+ it 'casts the alias to a string' do
141
+ @o.as(:the_alias)
142
+ @o.alias.should eq 'the_alias'
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ module Squeel
4
+ module Nodes
5
+ describe Operators do
6
+
7
+ [:+, :-, :*, :/].each do |operator|
8
+ describe "#{operator}" do
9
+ it "creates Operations with #{operator} operator from stubs" do
10
+ left = Stub.new(:stubby_mcstubbenstein)
11
+ node = left.send(operator, 1)
12
+ node.should be_an Operation
13
+ node.left.should eq left
14
+ node.operator.should eq operator
15
+ node.right.should eq 1
16
+ end
17
+
18
+ it "creates Operations with #{operator} operator from key paths" do
19
+ left = KeyPath.new(:first, :second)
20
+ node = left.send(operator, 1)
21
+ node.should be_an Operation
22
+ node.left.should eq left
23
+ node.operator.should eq operator
24
+ node.right.should eq 1
25
+ end
26
+
27
+ it "creates Operations with #{operator} operator from functions" do
28
+ left = Function.new(:name, ["arg1", "arg2"])
29
+ node = left.send(operator, 1)
30
+ node.should be_an Operation
31
+ node.left.should eq left
32
+ node.operator.should eq operator
33
+ node.right.should eq 1
34
+ end
35
+
36
+ it "creates Operations with #{operator} operator from operations" do
37
+ left = Stub.new(:stubby_mcstubbenstein) + 1
38
+ node = left.send(operator, 1)
39
+ node.should be_an Operation
40
+ node.left.should eq left
41
+ node.operator.should eq operator
42
+ node.right.should eq 1
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#op' do
48
+ it "creates Operations with custom operator from stubs" do
49
+ left = Stub.new(:stubby_mcstubbenstein)
50
+ node = left.op('||', 1)
51
+ node.should be_an Operation
52
+ node.left.should eq left
53
+ node.operator.should eq '||'
54
+ node.right.should eq 1
55
+ end
56
+
57
+ it "creates Operations with custom operator from key paths" do
58
+ left = KeyPath.new(:first, :second)
59
+ node = left.op('||', 1)
60
+ node.should be_an Operation
61
+ node.left.should eq left
62
+ node.operator.should eq '||'
63
+ node.right.should eq 1
64
+ end
65
+
66
+ it "creates Operations with custom operator from functions" do
67
+ left = Function.new(:name, ["arg1", "arg2"])
68
+ node = left.op('||', 1)
69
+ node.should be_an Operation
70
+ node.left.should eq left
71
+ node.operator.should eq '||'
72
+ node.right.should eq 1
73
+ end
74
+
75
+ it "creates Operations with custom operator from operations" do
76
+ left = Stub.new(:stubby_mcstubbenstein) + 1
77
+ node = left.op('||', 1)
78
+ node.should be_an Operation
79
+ node.left.should eq left
80
+ node.operator.should eq '||'
81
+ node.right.should eq 1
82
+ end
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module Squeel
4
+ module Nodes
5
+ describe Order do
6
+
7
+ it 'requires a direction of -1 or 1' do
8
+ expect { Order.new :attribute, 0 }.to raise_error ArgumentError
9
+ end
10
+
11
+ it 'defaults to ascending order' do
12
+ @o = Order.new :attribute
13
+ @o.should be_ascending
14
+ end
15
+
16
+ it 'allows reversal of order' do
17
+ @o = Order.new :attribute, 1
18
+ @o.reverse!
19
+ @o.should be_descending
20
+ end
21
+
22
+ it 'allows setting order' do
23
+ @o = Order.new :attribute
24
+ @o.desc
25
+ @o.should be_descending
26
+ end
27
+
28
+ end
29
+ end
30
+ end