arel_ruby 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in arel_ruby.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Akira Matsuda
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # ArelRuby
2
+
3
+ ARel Ruby visitor
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'arel_ruby'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install arel_ruby
18
+
19
+ ## Usage
20
+
21
+ See examples
22
+
23
+ ## Contributing
24
+
25
+ Fork and PR
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/arel_ruby/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Akira Matsuda"]
6
+ gem.email = ["ronnie@dio.jp"]
7
+ gem.description = 'ARel Ruby visitor'
8
+ gem.summary = 'ARel Ruby visitor'
9
+ gem.homepage = 'https://github.com/amatsuda/arel_ruby'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "arel_ruby"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ArelRuby::VERSION
17
+ end
@@ -0,0 +1,297 @@
1
+ require 'bigdecimal'
2
+ require 'date'
3
+
4
+ module Arel
5
+ module Visitors
6
+ class Ruby < Arel::Visitors::Visitor
7
+ attr_accessor :last_column
8
+
9
+ def initialize
10
+ @connection = Object.new.extend(ActiveRecord::ConnectionAdapters::Quoting)
11
+ @chains = []
12
+ end
13
+
14
+ def accept object
15
+ self.last_column = nil
16
+ super
17
+ end
18
+
19
+ private
20
+ # def visit_Arel_Nodes_DeleteStatement o
21
+ # end
22
+
23
+ # def build_subselect key, o
24
+ # end
25
+
26
+ # def visit_Arel_Nodes_UpdateStatement o
27
+ # end
28
+
29
+ # def visit_Arel_Nodes_InsertStatement o
30
+ # end
31
+
32
+ # def visit_Arel_Nodes_Exists o
33
+ # end
34
+
35
+ # def visit_Arel_Nodes_True o
36
+ # end
37
+
38
+ # def visit_Arel_Nodes_False o
39
+ # end
40
+
41
+ # def visit_Arel_Nodes_Values o
42
+ # end
43
+
44
+ def visit_Arel_Nodes_SelectStatement o
45
+ [
46
+ o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join,
47
+ ("#{o.orders.map { |x| visit_Arel_Nodes_OrderCore x }.join('.')}" unless o.orders.empty?),
48
+ (visit(o.offset) if o.offset),
49
+ (visit(o.limit) if o.limit),
50
+ ].compact.delete_if {|e| e.respond_to?(:empty?) && e.empty? }.join '.'
51
+ end
52
+
53
+ def visit_Arel_Nodes_SelectCore o
54
+ [
55
+ # ("#{o.projections.map { |x| visit x }.join ', '}" unless o.projections.empty?),
56
+ # (visit(o.source) if o.source && !o.source.empty?),
57
+ ("#{o.wheres.map { |x| visit x }.join '.' }" unless o.wheres.empty?),
58
+ ("#{o.groups.map { |x| visit x }.join '.' }" unless o.groups.empty?)
59
+ # (visit(o.having) if o.having),
60
+ ].compact.join '.'
61
+ end
62
+
63
+ def visit_Arel_Nodes_OrderCore order
64
+ order.split(',').map do |o|
65
+ attr, direction = o.split(/\s+/)
66
+ "sort_by(&:#{visit attr})#{'.reverse' if direction == 'desc'}"
67
+ end
68
+ end
69
+
70
+ # def visit_Arel_Nodes_Bin o
71
+ # end
72
+
73
+ # def visit_Arel_Nodes_Distinct o
74
+ # end
75
+
76
+ # def visit_Arel_Nodes_DistinctOn o
77
+ # end
78
+
79
+ # def visit_Arel_Nodes_With o
80
+ # end
81
+
82
+ # def visit_Arel_Nodes_WithRecursive o
83
+ # end
84
+
85
+ # def visit_Arel_Nodes_Union o
86
+ # end
87
+
88
+ # def visit_Arel_Nodes_UnionAll o
89
+ # end
90
+
91
+ # def visit_Arel_Nodes_Intersect o
92
+ # end
93
+
94
+ # def visit_Arel_Nodes_Except o
95
+ # end
96
+
97
+ # def visit_Arel_Nodes_NamedWindow o
98
+ # end
99
+
100
+ # def visit_Arel_Nodes_Window o
101
+ # end
102
+
103
+ # def visit_Arel_Nodes_Rows o
104
+ # end
105
+
106
+ # def visit_Arel_Nodes_Range o
107
+ # end
108
+
109
+ # def visit_Arel_Nodes_Preceding o
110
+ # end
111
+
112
+ # def visit_Arel_Nodes_Following o
113
+ # end
114
+
115
+ # def visit_Arel_Nodes_CurrentRow o
116
+ # end
117
+
118
+ # def visit_Arel_Nodes_Over o
119
+ # end
120
+
121
+ # def visit_Arel_Nodes_Having o
122
+ # end
123
+
124
+ def visit_Arel_Nodes_Offset o
125
+ "from(#{visit o.expr})"
126
+ end
127
+
128
+ def visit_Arel_Nodes_Limit o
129
+ "take(#{visit o.expr})"
130
+ end
131
+
132
+ # def visit_Arel_Nodes_Grouping o
133
+ # end
134
+
135
+ # def visit_Arel_Nodes_Ascending o
136
+ # end
137
+
138
+ # def visit_Arel_Nodes_Descending o
139
+ # end
140
+
141
+ def visit_Arel_Nodes_Group o
142
+ "group_by {|g| g.#{visit o.expr}}"
143
+ end
144
+
145
+ # def visit_Arel_Nodes_NamedFunction o
146
+ # end
147
+
148
+ # def visit_Arel_Nodes_Extract o
149
+ # end
150
+
151
+ # def visit_Arel_Nodes_Count o
152
+ # end
153
+
154
+ # def visit_Arel_Nodes_Sum o
155
+ # end
156
+
157
+ # def visit_Arel_Nodes_Max o
158
+ # end
159
+
160
+ # def visit_Arel_Nodes_Min o
161
+ # end
162
+
163
+ # def visit_Arel_Nodes_Avg o
164
+ # end
165
+
166
+ # def visit_Arel_Nodes_TableAlias o
167
+ # end
168
+
169
+ # def visit_Arel_Nodes_Between o
170
+ # end
171
+
172
+ # def visit_Arel_Nodes_GreaterThanOrEqual o
173
+ # end
174
+
175
+ # def visit_Arel_Nodes_GreaterThan o
176
+ # end
177
+
178
+ # def visit_Arel_Nodes_LessThanOrEqual o
179
+ # end
180
+
181
+ # def visit_Arel_Nodes_LessThan o
182
+ # end
183
+
184
+ # def visit_Arel_Nodes_Matches o
185
+ # end
186
+
187
+ # def visit_Arel_Nodes_DoesNotMatch o
188
+ # end
189
+
190
+ def visit_Arel_Nodes_JoinSource o
191
+ # do nothing
192
+ end
193
+
194
+ # def visit_Arel_Nodes_StringJoin o
195
+ # end
196
+
197
+ # def visit_Arel_Nodes_OuterJoin o
198
+ # end
199
+
200
+ # def visit_Arel_Nodes_InnerJoin o
201
+ # end
202
+
203
+ # def visit_Arel_Nodes_On o
204
+ # end
205
+
206
+ # def visit_Arel_Nodes_Not o
207
+ # end
208
+
209
+ # def visit_Arel_Table o
210
+ # end
211
+
212
+ # def visit_Arel_Nodes_In o
213
+ # end
214
+
215
+ # def visit_Arel_Nodes_NotIn o
216
+ # end
217
+
218
+ def visit_Arel_Nodes_And o
219
+ o.children.map { |x| "select {|o| #{visit x}}"}.join '.'
220
+ end
221
+
222
+ # def visit_Arel_Nodes_Or o
223
+ # end
224
+
225
+ # def visit_Arel_Nodes_Assignment o
226
+ # end
227
+
228
+ def visit_Arel_Nodes_Equality o
229
+ "#{visit o.left} == #{visit o.right}"
230
+ end
231
+
232
+ # def visit_Arel_Nodes_NotEqual o
233
+ # end
234
+
235
+ # def visit_Arel_Nodes_As o
236
+ # end
237
+
238
+ # def visit_Arel_Nodes_UnqualifiedColumn o
239
+ # end
240
+
241
+
242
+ def visit_Arel_Attributes_Attribute o
243
+ self.last_column = o.name
244
+ "o.#{o.name}"
245
+ end
246
+ alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute
247
+ alias :visit_Arel_Attributes_Float :visit_Arel_Attributes_Attribute
248
+ alias :visit_Arel_Attributes_Decimal :visit_Arel_Attributes_Attribute
249
+ alias :visit_Arel_Attributes_String :visit_Arel_Attributes_Attribute
250
+ alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute
251
+ alias :visit_Arel_Attributes_Boolean :visit_Arel_Attributes_Attribute
252
+
253
+ def literal o; o end
254
+
255
+ alias :visit_Arel_Nodes_BindParam :literal
256
+ alias :visit_Arel_Nodes_SqlLiteral :literal
257
+ alias :visit_Bignum :literal
258
+ alias :visit_Fixnum :literal
259
+
260
+ def quoted o
261
+ quote(o, last_column)
262
+ end
263
+
264
+ alias :visit_ActiveSupport_Multibyte_Chars :quoted
265
+ alias :visit_ActiveSupport_StringInquirer :quoted
266
+ alias :visit_BigDecimal :quoted
267
+ alias :visit_Class :quoted
268
+ alias :visit_Date :quoted
269
+ alias :visit_DateTime :quoted
270
+ alias :visit_FalseClass :quoted
271
+ alias :visit_Float :quoted
272
+ alias :visit_Hash :quoted
273
+ alias :visit_NilClass :quoted
274
+ alias :visit_String :quoted
275
+ alias :visit_Symbol :quoted
276
+ alias :visit_Time :quoted
277
+ alias :visit_TrueClass :quoted
278
+
279
+ # def visit_Arel_Nodes_InfixOperation o
280
+ # "#{visit o.left} #{o.operator} #{visit o.right}"
281
+ # end
282
+
283
+ # alias :visit_Arel_Nodes_Addition :visit_Arel_Nodes_InfixOperation
284
+ # alias :visit_Arel_Nodes_Subtraction :visit_Arel_Nodes_InfixOperation
285
+ # alias :visit_Arel_Nodes_Multiplication :visit_Arel_Nodes_InfixOperation
286
+ # alias :visit_Arel_Nodes_Division :visit_Arel_Nodes_InfixOperation
287
+
288
+ # def visit_Array o
289
+ # o.map { |x| visit x }.join(', ')
290
+ # end
291
+
292
+ def quote value, column = nil
293
+ @connection.quote value, column
294
+ end
295
+ end
296
+ end
297
+ end
@@ -0,0 +1,14 @@
1
+ require 'arel_ruby/version'
2
+ require 'arel/visitors/ruby'
3
+
4
+ module Arel
5
+ # for AR <= 3.2.6 compatibility
6
+ module Relation
7
+ end
8
+
9
+ class TreeManager
10
+ def to_ruby
11
+ Visitors::Ruby.new.accept @ast
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module ArelRuby
2
+ VERSION = "0.0.1.pre"
3
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveRecord
2
+ Relation.class_eval do
3
+ def exec_queries_in_ruby
4
+ return @records if loaded?
5
+ ruby = build_arel.to_ruby
6
+ connection.send(:log, ruby, 'RUBY') do
7
+ self.klass.all.instance_eval ruby
8
+ end
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arel_ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 2107825352
5
+ prerelease: 6
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ - pre
11
+ version: 0.0.1.pre
12
+ platform: ruby
13
+ authors:
14
+ - Akira Matsuda
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-07-06 00:00:00 Z
20
+ dependencies: []
21
+
22
+ description: ARel Ruby visitor
23
+ email:
24
+ - ronnie@dio.jp
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - arel_ruby.gemspec
38
+ - lib/arel/visitors/ruby.rb
39
+ - lib/arel_ruby.rb
40
+ - lib/arel_ruby/version.rb
41
+ - lib/examples/active_record.rb
42
+ homepage: https://github.com/amatsuda/arel_ruby
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">"
63
+ - !ruby/object:Gem::Version
64
+ hash: 25
65
+ segments:
66
+ - 1
67
+ - 3
68
+ - 1
69
+ version: 1.3.1
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.10
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: ARel Ruby visitor
77
+ test_files: []
78
+