squeel 0.7.4 → 0.7.5

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.
data/README.md CHANGED
@@ -46,7 +46,7 @@ to your object's methods and variables:
46
46
 
47
47
  1. Assign the variable locally before the DSL block, and access it as you would
48
48
  normally.
49
- 2. Supply and arity to the DSL block, as in `Person.where{|dsl| dsl.name == @my_name}`
49
+ 2. Supply an arity to the DSL block, as in `Person.where{|dsl| dsl.name == @my_name}`
50
50
  Downside: You'll need to prefix stubs, keypaths, and functions (explained below)
51
51
  with the DSL object.
52
52
  3. Wrap the method or instance variable inside the block with `my{}`.
@@ -1,4 +1,5 @@
1
1
  require 'squeel/predicate_methods'
2
+ require 'squeel/nodes/aliasing'
2
3
 
3
4
  # These extensions to Symbol are loaded optionally, mostly to provide
4
5
  # a small amount of backwards compatibility with MetaWhere.
@@ -9,6 +10,7 @@ require 'squeel/predicate_methods'
9
10
  # end
10
11
  class Symbol
11
12
  include Squeel::PredicateMethods
13
+ include Squeel::Nodes::Aliasing
12
14
 
13
15
  def asc
14
16
  Squeel::Nodes::Order.new self, 1
@@ -0,0 +1,13 @@
1
+ require 'squeel/nodes/as'
2
+
3
+ module Squeel
4
+ module Nodes
5
+ module Aliasing
6
+
7
+ def as(name)
8
+ As.new(self, name.to_s)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'squeel/nodes/binary'
2
+
3
+ module Squeel
4
+ module Nodes
5
+ # A node representing an SQL alias, which will result in same when visited.
6
+ class As < Binary
7
+ # @param left The node to be aliased
8
+ # @param right The alias name
9
+ def initialize(left, right)
10
+ @left, @right = left, right
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,6 @@
1
1
  require 'squeel/predicate_methods'
2
2
  require 'squeel/nodes/operators'
3
+ require 'squeel/nodes/aliasing'
3
4
 
4
5
  module Squeel
5
6
  module Nodes
@@ -7,9 +8,9 @@ module Squeel
7
8
  # methods and operators defined on it since doing so on Symbol will incur the
8
9
  # nerdrage of many.
9
10
  class Stub
10
-
11
11
  include PredicateMethods
12
12
  include Operators
13
+ include Aliasing
13
14
 
14
15
  alias :== :eq
15
16
  alias :'^' :not_eq
data/lib/squeel/nodes.rb CHANGED
@@ -12,5 +12,6 @@ require 'squeel/nodes/operation'
12
12
  require 'squeel/nodes/order'
13
13
  require 'squeel/nodes/and'
14
14
  require 'squeel/nodes/or'
15
+ require 'squeel/nodes/as'
15
16
  require 'squeel/nodes/not'
16
17
  require 'squeel/nodes/join'
@@ -1,3 +1,3 @@
1
1
  module Squeel
2
- VERSION = "0.7.4"
2
+ VERSION = "0.7.5"
3
3
  end
@@ -136,6 +136,15 @@ module Squeel
136
136
  o.alias ? op.as(o.alias) : op
137
137
  end
138
138
 
139
+ # Visit a Squeel As node, resulting in am ARel As node.
140
+ #
141
+ # @param [Nodes::As] The As node to visit
142
+ # @param parent The parent object in the context
143
+ # @return [Arel::Nodes::As] The resulting as node.
144
+ def visit_Squeel_Nodes_As(o, parent)
145
+ accept(o.left, parent).as(o.right)
146
+ end
147
+
139
148
  # @return [Boolean] Whether the given value implies a context change
140
149
  # @param v The value to consider
141
150
  def implies_context_change?(v)
@@ -165,7 +165,7 @@ module Squeel
165
165
  # Visit a Squeel And node, returning an ARel Grouping containing an
166
166
  # ARel And node.
167
167
  #
168
- # @param [Nodes::And] The And node to visit
168
+ # @param [Nodes::And] o The And node to visit
169
169
  # @param parent The parent object in the context
170
170
  # @return [Arel::Nodes::Grouping] A grouping node, containnig an ARel
171
171
  # And node as its expression. All children will be visited before
@@ -176,9 +176,9 @@ module Squeel
176
176
 
177
177
  # Visit a Squeel Or node, returning an ARel Or node.
178
178
  #
179
- # @param [Nodes::Or] The Or node to visit
179
+ # @param [Nodes::Or] o The Or node to visit
180
180
  # @param parent The parent object in the context
181
- # @return [Arel::Nodes::Or] An ARel Or node, with left and ride sides visited
181
+ # @return [Arel::Nodes::Or] An ARel Or node, with left and right sides visited
182
182
  def visit_Squeel_Nodes_Or(o, parent)
183
183
  accept(o.left, parent).or(accept(o.right, parent))
184
184
  end
@@ -65,4 +65,11 @@ describe Symbol do
65
65
  join._type.should eq Arel::OuterJoin
66
66
  end
67
67
 
68
+ it 'creates as nodes' do
69
+ as = :column.as('other_name')
70
+ as.should be_a Squeel::Nodes::As
71
+ as.left.should eq :column
72
+ as.right.should eq 'other_name'
73
+ end
74
+
68
75
  end
@@ -45,6 +45,14 @@ module Squeel
45
45
  @k.endpoint.args.should eq [1,2,3]
46
46
  end
47
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
+
48
56
  it 'creates AND nodes with & if the endpoint responds to &' do
49
57
  node = @k.third.fourth.eq('Bob') & Stub.new(:attr).eq('Joe')
50
58
  node.should be_a And
@@ -186,6 +186,13 @@ module Squeel
186
186
  function.should be_a Function
187
187
  end
188
188
 
189
+ it 'creates as nodes with #as' do
190
+ as = @s.as('other_name')
191
+ as.should be_a Squeel::Nodes::As
192
+ as.left.should eq @s
193
+ as.right.should eq 'other_name'
194
+ end
195
+
189
196
  end
190
197
  end
191
198
  end
@@ -92,6 +92,21 @@ module Squeel
92
92
  function.to_sql.should match /newname/
93
93
  end
94
94
 
95
+ it 'accepts As nodes containing symbols' do
96
+ as = @v.accept(:name.as('other_name'))
97
+ as.to_sql.should match /"people"."name" AS other_name/
98
+ end
99
+
100
+ it 'accepts As nodes containing stubs' do
101
+ as = @v.accept(dsl{name.as(other_name)})
102
+ as.to_sql.should match /"people"."name" AS other_name/
103
+ end
104
+
105
+ it 'accepts As nodes containing keypaths' do
106
+ as = @v.accept(dsl{children.name.as(other_name)})
107
+ as.to_sql.should match /"children_people"."name" AS other_name/
108
+ end
109
+
95
110
  it 'creates an ARel Addition node for an Operation node with + as operator' do
96
111
  operation = @v.accept(dsl{id + 1})
97
112
  operation.should be_a Arel::Nodes::Addition
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: squeel
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.7.4
5
+ version: 0.7.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ernie Miller
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-24 00:00:00 Z
13
+ date: 2011-06-07 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -114,7 +114,9 @@ files:
114
114
  - lib/squeel/context.rb
115
115
  - lib/squeel/dsl.rb
116
116
  - lib/squeel/nodes.rb
117
+ - lib/squeel/nodes/aliasing.rb
117
118
  - lib/squeel/nodes/and.rb
119
+ - lib/squeel/nodes/as.rb
118
120
  - lib/squeel/nodes/binary.rb
119
121
  - lib/squeel/nodes/function.rb
120
122
  - lib/squeel/nodes/join.rb