ambition 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -34,10 +34,13 @@ Basically, you write your SQL in Ruby. No, not in Ruby. As Ruby.
34
34
 
35
35
  And that's it.
36
36
 
37
- The key is the +each+ method. You build up a +Query+ using +select+, +first+,
38
- and +sort_by+, then call +each+ on it. This'll run the query and enumerate
39
- through the results. Really, you can use any Enumerable method: +map+,
40
- +each_with_index+, etc.
37
+ The key is that queries arent actually run until the data they represent is
38
+ requested. Usually this is done with what I call a kicker method. You can call them
39
+ that, too.
40
+
41
+ Kicker methods are guys like +detect+, +each+, +each_with_index+, +map+, +entries+,
42
+ +to_a+, and +first+ (with no argument). Methods like +select+, +sort_by+, and +first+
43
+ (with an argument) are not kicker methods and return a +Query+ object without running any SQL.
41
44
 
42
45
  Our +Query+ object has two useful methods: +to_sql+ and +to_hash+. With these,
43
46
  we can check out what exactly we're building. Not everyone has +to_sql+,
@@ -127,7 +130,7 @@ still query through ActiveRecord just fine.
127
130
  User.detect { |m| m.name == 'chris' }
128
131
  "SELECT * FROM users WHERE users.`name` = 'chris' LIMIT 1"
129
132
 
130
- == LIMITs -- first, first(x), [offset, limit], [range]
133
+ == LIMITs -- first, first(x), [offset, limit], [range], slice
131
134
 
132
135
  User.select { |m| m.name == 'jon' }.first
133
136
  "SELECT * FROM users WHERE users.`name` = 'jon' LIMIT 1"
@@ -173,6 +176,15 @@ still query through ActiveRecord just fine.
173
176
  >> User.select { |m| m.name == 'jon' }.size
174
177
  => 21
175
178
 
179
+ == Other Enumerables
180
+
181
+ These methods perform COUNT() operations rather than loading your array into memory. They're all
182
+ kickers.
183
+
184
+ User.any? { |m| m.name == 'jon' }
185
+ User.all? { |m| m.name == 'jon' }
186
+ User.select { |m| m.name == 'jon' }.empty?
187
+
176
188
  == SELECT * FROM bugs
177
189
 
178
190
  Found a bug? Sweet. Add it at the Lighthouse: http://err.lighthouseapp.com/projects/466-plugins/tickets/new
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ begin
8
8
  ENV['RUBY_FLAGS'] = ""
9
9
  require 'echoe'
10
10
 
11
- Echoe.new('ambition', '0.1.2') do |p|
11
+ Echoe.new('ambition', '0.1.3') do |p|
12
12
  p.rubyforge_name = 'err'
13
13
  p.summary = "Ambition builds SQL from plain jane Ruby."
14
14
  p.description = "Ambition builds SQL from plain jane Ruby."
@@ -5,5 +5,22 @@ module Ambition
5
5
  def each(&block)
6
6
  find(:all, query_context.to_hash).each(&block)
7
7
  end
8
+
9
+ def any?(&block)
10
+ select(&block).size > 0
11
+ end
12
+
13
+ def all?(&block)
14
+ size == select(&block).size
15
+ end
16
+
17
+ def empty?
18
+ size.zero?
19
+ end
20
+
21
+ def entries
22
+ find(:all, query_context.to_hash)
23
+ end
24
+ alias_method :to_a, :entries
8
25
  end
9
26
  end
@@ -16,6 +16,7 @@ module Ambition
16
16
  first(offset, 1)
17
17
  end
18
18
  end
19
+ alias_method :slice, :[]
19
20
  end
20
21
 
21
22
  class LimitProcessor
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+
4
+ context "Constructive" do
5
+ xspecify "concat" do
6
+ end
7
+
8
+ xspecify "<<" do
9
+ end
10
+
11
+ xspecify "+=" do
12
+ end
13
+
14
+ xspecify "push" do
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "Destructive" do
4
+ xspecify "clear" do
5
+ end
6
+
7
+ xspecify "delete" do
8
+ end
9
+
10
+ xspecify "delete_if" do
11
+ end
12
+
13
+ xspecify "<<" do
14
+ end
15
+
16
+ xspecify "concat" do
17
+ end
18
+
19
+ xspecify "delete_at" do
20
+ end
21
+ end
@@ -48,4 +48,48 @@ context "Enumerable Methods" do
48
48
  puts "#{i}: #{user.name}"
49
49
  end
50
50
  end
51
+
52
+ specify "any?" do
53
+ User.expects(:count).with(:conditions => "users.`age` > 21").returns(1)
54
+ User.any? { |u| u.age > 21 }.should == true
55
+ end
56
+
57
+ specify "all?" do
58
+ User.expects(:count).at_least_once.returns(10, 20)
59
+ User.all? { |u| u.age > 21 }.should == false
60
+
61
+ User.expects(:count).at_least_once.returns(10, 10)
62
+ User.all? { |u| u.age > 21 }.should == true
63
+ end
64
+
65
+ specify "empty?" do
66
+ User.expects(:count).with(:conditions => "users.`age` > 21").returns(1)
67
+ User.select { |u| u.age > 21 }.empty?.should.equal false
68
+
69
+ User.expects(:count).with(:conditions => "users.`age` > 21").returns(0)
70
+ User.select { |u| u.age > 21 }.empty?.should.equal true
71
+ end
72
+
73
+ specify "entries" do
74
+ User.expects(:find).with(:all, {})
75
+ User.entries
76
+
77
+ hash = { :conditions => "users.`age` = 21" }
78
+ User.expects(:find).with(:all, hash).returns([])
79
+ User.select { |m| m.age == 21 }.entries
80
+ end
81
+
82
+ specify "to_a" do
83
+ User.expects(:find).with(:all, {})
84
+ User.to_a
85
+ end
86
+
87
+ xspecify "each_slice" do
88
+ end
89
+
90
+ xspecify "max" do
91
+ end
92
+
93
+ xspecify "min" do
94
+ end
51
95
  end
data/test/limit_test.rb CHANGED
@@ -29,6 +29,12 @@ context "Limit" do
29
29
  @sql[10, 20]
30
30
  end
31
31
 
32
+ specify "slice is an alias of []" do
33
+ conditions = { :conditions => "users.`name` = 'jon'", :limit => '10, 20' }
34
+ User.expects(:find).with(:all, conditions)
35
+ @sql.slice(10, 20)
36
+ end
37
+
32
38
  specify "[] with range" do
33
39
  conditions = { :conditions => "users.`name` = 'jon'", :limit => '10, 10' }
34
40
  User.expects(:find).with(:all, conditions)
data/test/where_test.rb CHANGED
@@ -122,6 +122,10 @@ context "Where (using select)" do
122
122
  sql = User.select { |m| m.created_at == 2.days.ago.to_s(:db) }.to_sql
123
123
  sql.should == "SELECT * FROM users WHERE users.`created_at` = #{2.days.ago.to_s(:db)}"
124
124
  end
125
+
126
+ specify "inspect" do
127
+ User.select { |u| u.name }.inspect.should.match %r(call #to_sql or #to_hash)
128
+ end
125
129
  end
126
130
 
127
131
  context "Where (using detect)" do
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: ambition
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
6
+ version: 0.1.3
7
7
  date: 2007-08-30 00:00:00 -07:00
8
8
  summary: Ambition builds SQL from plain jane Ruby.
9
9
  require_paths:
@@ -54,7 +54,9 @@ files:
54
54
  - ./Manifest
55
55
  test_files:
56
56
  - test/chaining_test.rb
57
+ - test/constructive_test.rb
57
58
  - test/count_test.rb
59
+ - test/destructive_test.rb
58
60
  - test/enumerable_test.rb
59
61
  - test/join_test.rb
60
62
  - test/limit_test.rb