public
Rubygem
Description: include Enumerable
Homepage: http://ambition.rubyforge.org
Clone URL: git://github.com/defunkt/ambition.git
Search Repo:
license and readme stuff
defunkt (author)
Wed Nov 28 19:01:44 -0800 2007
commit  42b276419222e6fa372da1f6fc50ec9a6f348f01
tree    fc0d8a69cde541a1620f8ef965a9c1e13034305c
parent  189244070fd4dca0fa36a0e6c8745814ccd603e8
...
23
24
25
 
26
27
28
...
23
24
25
26
27
28
29
0
@@ -23,6 +23,7 @@ lib/ambition/processors/select.rb
0
 lib/ambition/processors/slice.rb
0
 lib/ambition/processors/sort.rb
0
 lib/ambition.rb
0
+LICENSE
0
 Manifest
0
 README
0
 test/adapters/exemplar/association_test.rb
...
42
43
44
45
 
46
47
48
...
42
43
44
 
45
46
47
48
0
@@ -42,7 +42,7 @@ class AmbitionAdapterGenerator < RubiGen::Base
0
 
0
       ##
0
       # Normal files
0
- files = %w( README Rakefile )
0
+ files = %w( LICENSE README Rakefile )
0
       files.each do |file|
0
         m.template file, file
0
       end
...
35
36
37
38
 
39
40
41
...
35
36
37
 
38
39
40
41
0
@@ -35,7 +35,7 @@ task :build do
0
     live_file = "live/#{file_name}"
0
 
0
     unless uptodate?(live_file, file) && uptodate?(live_file, "src/layout.textile")
0
- File.new(live_file, 'w') { |f| f.puts(compile(file)); f.flush }
0
+ File.open(live_file, 'w') { |f| f.puts(compile(file)); f.flush }
0
       puts "created #{live_file}"
0
       cp file, 'live/textile/' + file.split('/').last.sub('.textile', '.txt')
0
     end
...
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
...
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
0
@@ -1 +1,234 @@
0
-Hi.
0
+h2. An Ambitious ActiveRecord Adapter
0
+
0
+I could tell you all about how awesome the internals are, or
0
+how fun it was to write, or how it'll make you rich and famous,
0
+but instead I'm just going to show you some examples.
0
+
0
+h2. Get It
0
+
0
+@$ sudo gem install ambitious-activerecord@
0
+
0
+This will suck in the adapter and its dependencies (ActiveRecord & Ambition).
0
+It's fully usable outside of Rails (I use it in a Camping app or two), as long
0
+as you're riding ActiveRecord.
0
+
0
+To use with Rails, after installing the gem:
0
+
0
+<pre>
0
+$ cd vendor/plugins
0
+$ gem unpack ambitious-activerecord
0
+</pre>
0
+
0
+h2. Examples
0
+
0
+Basically, you write your SQL in Ruby. No, not in Ruby. As Ruby.
0
+
0
+<ruby>
0
+User.select { |u| u.city == 'San Francisco' }.each do |user|
0
+ puts user.name
0
+end
0
+</ruby>
0
+
0
+And that's it.
0
+
0
+The key is that queries aren't actually run until the data they represent is
0
+requested. Usually this is done with what I call a kicker method. You can call them
0
+that, too.
0
+
0
+Kicker methods are guys like @detect@, @each@, @each_with_index@, @map@, @entries@,
0
+@to_a@, and @first@ (with no argument). Methods like @select@, @sort_by@, and @first@
0
+(with an argument) are not kicker methods and return a @Context@ object without running any SQL.
0
+
0
+Our @Context@ object has two useful methods: @to_s@ and @to_hash@. With these,
0
+we can check out what exactly we're building. Not everyone has @to_s@,
0
+though. Mostly ignore these methods and treat everything like you normally
0
+would.
0
+
0
+See, @to_s@:
0
+
0
+<ruby>
0
+>> User.select { |m| m.name == 'jon' }.to_s
0
+=> "SELECT * FROM users WHERE users.name = 'jon'"
0
+</ruby>
0
+
0
+See, @to_hash@:
0
+
0
+<ruby>
0
+>> User.select { |m| m.name == 'jon' }.to_hash
0
+=> { :conditions => "users.name = 'jon'" }
0
+</ruby>
0
+
0
+h2. Equality - select { |u| u.field == 'bob' }
0
+
0
+<ruby>
0
+User.select { |m| m.name == 'jon' }
0
+"SELECT * FROM users WHERE users.name = 'jon'"
0
+
0
+User.select { |m| m.created_at > 2.days.ago }
0
+"SELECT * FROM users WHERE users.created_at > '2007-09-26 20:37:47'"
0
+
0
+User.select { |m| m.name == 'jon' }
0
+"SELECT * FROM users WHERE users.name = 'jon'"
0
+
0
+User.select { |m| m.name != 'jon' }
0
+"SELECT * FROM users WHERE users.name <> 'jon'"
0
+
0
+User.select { |m| m.name == 'jon' && m.age == 21 }
0
+"SELECT * FROM users WHERE (users.name = 'jon' AND users.age = 21)"
0
+
0
+User.select { |m| m.name == 'jon' || m.age == 21 }
0
+"SELECT * FROM users WHERE (users.name = 'jon' OR users.age = 21)"
0
+
0
+User.select { |m| m.name == 'jon' || m.age == 21 && m.password == 'pass' }
0
+"SELECT * FROM users WHERE
0
+ (users.name = 'jon' OR (users.age = 21 AND users.password = 'pass'))"
0
+
0
+User.select { |m| (m.name == 'jon' || m.name == 'rick') && m.age == 21 }
0
+"SELECT * FROM users WHERE
0
+ ((users.name = 'jon' OR users.name = 'rick') AND users.age = 21)"
0
+</ruby>
0
+
0
+h2. Associations - select { |u| u.field == 'bob' && u.association.field == 'bob@bob.com' }
0
+
0
+The @to_s@ method doesn't work on associations yet, but that's okay: they can
0
+still query through ActiveRecord just fine.
0
+
0
+<ruby>
0
+User.select do |u|
0
+ u.email == 'chris@ozmm.org' && u.profile.name == 'chris wanstrath'
0
+end.map(&:title)
0
+
0
+"SELECT users.id AS t0_r0, ... FROM users
0
+ LEFT OUTER JOIN profiles ON profiles.user_id = users.id
0
+ WHERE ((users.email = 'chris@ozmm.org' AND profiles.name = 'chris wanstrath'))"
0
+</ruby>
0
+
0
+h2. Comparisons - select { |u| u.age > 21 }
0
+
0
+<ruby>
0
+User.select { |m| m.age > 21 }
0
+"SELECT * FROM users WHERE users.age > 21"
0
+
0
+User.select { |m| m.age < 21 }.to_s
0
+"SELECT * FROM users WHERE users.age < 21"
0
+
0
+User.select { |m| [1, 2, 3, 4].include? m.id }
0
+"SELECT * FROM users WHERE users.id IN (1, 2, 3, 4)"
0
+</ruby>
0
+
0
+h2. LIKE and REGEXP (RLIKE) - select { |m| m.name =~ 'chris' }
0
+
0
+<ruby>
0
+User.select { |m| m.name =~ 'chris' }
0
+"SELECT * FROM users WHERE users.name LIKE 'chris'"
0
+
0
+User.select { |m| m.name =~ 'chri%' }
0
+"SELECT * FROM users WHERE users.name LIKE 'chri%'"
0
+
0
+User.select { |m| m.name !~ 'chris' }
0
+"SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
0
+
0
+User.select { |m| !(m.name =~ 'chris') }
0
+"SELECT * FROM users WHERE users.name NOT LIKE 'chris'"
0
+
0
+User.select { |m| m.name =~ /chris/ }
0
+"SELECT * FROM users WHERE users.name REGEXP 'chris'"
0
+</ruby>
0
+
0
+h2. #detect
0
+
0
+<ruby>
0
+User.detect { |m| m.name == 'chris' }
0
+"SELECT * FROM users WHERE users.name = 'chris' LIMIT 1"
0
+</ruby>
0
+
0
+h2. LIMITs - first, first(x), [offset, limit], [range], slice
0
+
0
+<ruby>
0
+User.select { |m| m.name == 'jon' }.first
0
+"SELECT * FROM users WHERE users.name = 'jon' LIMIT 1"
0
+
0
+User.select { |m| m.name == 'jon' }.first(5)
0
+"SELECT * FROM users WHERE users.name = 'jon' LIMIT 5"
0
+
0
+User.select { |m| m.name == 'jon' }[10, 20]
0
+"SELECT * FROM users WHERE users.name = 'jon' LIMIT 10, 20"
0
+
0
+User.select { |m| m.name == 'jon' }[10..20]
0
+"SELECT * FROM users WHERE users.name = 'jon' LIMIT 10, 10"
0
+</ruby>
0
+
0
+h2. ORDER - sort_by { |u| u.field }
0
+
0
+<ruby>
0
+User.select { |m| m.name == 'jon' }.sort_by { |m| m.name }
0
+"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name"
0
+
0
+User.select { |m| m.name == 'jon' }.sort_by { |m| [ m.name, m.age ] }
0
+"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
0
+
0
+User.select { |m| m.name == 'jon' }.sort_by { |m| [ m.name, -m.age ] }
0
+"SELECT * FROM users WHERE users.name = 'jon'
0
+ ORDER BY users.name, users.age DESC"
0
+
0
+User.select { |m| m.name == 'jon' }.sort_by { |m| [ -m.name, -m.age ] }
0
+"SELECT * FROM users WHERE users.name = 'jon'
0
+ ORDER BY users.name DESC, users.age DESC"
0
+
0
+User.select { |m| m.name == 'jon' }.sort_by { |m| -m.age }
0
+"SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
0
+
0
+User.select { |m| m.name == 'jon' }.sort_by { |m| -m.profiles.title }
0
+"SELECT users.id AS t0_r0, ... FROM users
0
+ LEFT OUTER JOIN profiles ON profiles.user_id = users.id
0
+ WHERE (users.name = 'jon') ORDER BY profiles.title DESC"
0
+
0
+User.select { |m| m.name == 'jon' }.sort_by { rand }
0
+"SELECT * FROM users WHERE users.name = 'jon' ORDER BY RAND()"
0
+</ruby>
0
+
0
+h2. COUNT - select { |u| u.name == 'jon' }.size
0
+
0
+<ruby>
0
+User.select { |m| m.name == 'jon' }.size
0
+"SELECT count(*) AS count_all FROM users WHERE (users.name = 'jon')"
0
+
0
+>> User.select { |m| m.name == 'jon' }.size
0
+=> 21
0
+</ruby>
0
+
0
+h2. Other Enumerables
0
+
0
+These methods perform COUNT() operations rather than loading your array into memory. They're all
0
+kickers.
0
+
0
+<ruby>
0
+User.any? { |m| m.name == 'jon' }
0
+User.all? { |m| m.name == 'jon' }
0
+User.select { |m| m.name == 'jon' }.empty?
0
+</ruby>
0
+
0
+h2. More Sugar
0
+
0
+The @downcase@ and @upcase@ methods will map to LOWER() and UPPER(), respectively.
0
+
0
+<ruby>
0
+>> User.select { |m| m.name.downcase =~ 'jon%' }.to_s
0
+=> "SELECT * FROM users WHERE LOWER(users.name) LIKE 'jon%'"
0
+</ruby>
0
+
0
+h2. Quoting
0
+
0
+Columns and values will be quoted using ActiveRecord's quote_column_name and quote methods, if
0
+possible.
0
+
0
+h2. SELECT * FROM bugs
0
+
0
+Found a bug? Sweet. Add it at "the Lighthouse":http://err.lighthouseapp.com/projects/466-plugins/tickets/new.
0
+
0
+More information on Ambition:
0
+
0
+ * "http://ambition.rubyforge.org":http://ambition.rubyforge.org
0
+ * "http://groups.google.com/group/ambition-rb/":http://groups.google.com/group/ambition-rb/
0
+
0
+- Chris Wanstrath [ chris@ozmm.org ]
0
\ No newline at end of file
...
113
114
115
116
117
118
 
119
120
121
...
113
114
115
 
 
 
116
117
118
119
0
@@ -113,9 +113,7 @@ pre {
0
 .ruby .brackets {  
0
   color: #777;
0
 }
0
-.ruby .symbol {
0
- color: #00fb79;
0
-}
0
+
0
 .ruby .regex {
0
   color: #37ffff;
0
 }

Comments

    No one has commented yet.