ambition 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -7,6 +7,7 @@
7
7
  ./lib/ambition/select_processor.rb
8
8
  ./lib/ambition/simple_processor.rb
9
9
  ./lib/ambition/sort_processor.rb
10
+ ./lib/ambition/source.rb
10
11
  ./lib/ambition.rb
11
12
  ./LICENSE
12
13
  ./Manifest
@@ -38,8 +39,8 @@
38
39
  ./test/helper.rb
39
40
  ./test/join_test.rb
40
41
  ./test/limit_test.rb
41
- ./test/mocks_test.rb
42
42
  ./test/order_test.rb
43
43
  ./test/profiler.rb
44
+ ./test/source_test.rb
44
45
  ./test/types_test.rb
45
46
  ./test/where_test.rb
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rake'
2
2
  require 'rake/testtask'
3
3
  require 'rake/rdoctask'
4
4
 
5
- Version = '0.2.1'
5
+ Version = '0.2.2'
6
6
 
7
7
  module Rake::TaskManager
8
8
  def redefine_task(task_class, args, &block)
data/lib/ambition.rb CHANGED
@@ -3,6 +3,7 @@ unless defined? ActiveRecord
3
3
  require 'active_record'
4
4
  end
5
5
  require 'ambition/api'
6
+ require 'ambition/source'
6
7
  require 'ambition/processor'
7
8
  require 'ambition/select_processor'
8
9
  require 'ambition/sort_processor'
@@ -0,0 +1,53 @@
1
+ module Ambition
2
+ module Source
3
+ def self.api_methods
4
+ Ambition::API.instance_methods(false).reject do |method|
5
+ method =~ /ambition|context/
6
+ end
7
+ end
8
+
9
+ def self.included(base)
10
+ singleton = (class << base; self end)
11
+ api_methods.each do |method|
12
+ ##
13
+ # This is less than cool, but we do it to get what we want.
14
+ # When define_method can create a method which is block-aware,
15
+ # then we can move to pure Ruby.
16
+ singleton.class_eval <<-end_eval
17
+ def #{method}(*args, &block)
18
+ return super unless @ambition_source
19
+ @ambition_source.#{method}(*args, &block)
20
+ end
21
+ end_eval
22
+ end
23
+ end
24
+ end
25
+
26
+ module API
27
+ attr_reader :ambition_source
28
+
29
+ ##
30
+ # This is a destructive method. When given an array,
31
+ # all Enumerable operations will be performed on the array
32
+ # rather than on the database.
33
+ #
34
+ # User.ambition_source = users(:chris, :pj)
35
+ # User.select { |u| u.age > 30 }
36
+ #
37
+ # You don't have to use fixtures, of course. Make stuff up on the fly.
38
+ #
39
+ # users = [ User.new(:name => 'chris', :age => 11), User.new(:name => 'pj', :age => 100) ]
40
+ # User.ambition_source = users
41
+ # User.select { |u| u.age > 30 }
42
+ #
43
+ # Revert back to normal by setting ambition_source to nil.
44
+ #
45
+ # User.ambition_source = nil
46
+ # User.select { |u| u.age > 30 }
47
+ #
48
+ def ambition_source=(records)
49
+ @ambition_source = records
50
+ include Source unless ancestors.include? Source
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'ostruct'
3
+
4
+ context "Setting the ambition_source" do
5
+ setup do
6
+ @users = [
7
+ OpenStruct.new(:name => 'Chris', :age => 22),
8
+ OpenStruct.new(:name => 'PJ', :age => 24),
9
+ OpenStruct.new(:name => 'Kevin', :age => 23),
10
+ OpenStruct.new(:name => '_why', :age => 65)
11
+ ]
12
+ User.ambition_source = @users
13
+ end
14
+
15
+ teardown do
16
+ User.ambition_source = nil
17
+ end
18
+
19
+ specify "should run all selects / detects against that collection" do
20
+ User.detect { |u| u.name == 'Chris' }.should == @users.first
21
+ end
22
+
23
+ specify "should run all sorts against that collection" do
24
+ User.sort_by { |u| -u.age }.entries.should == @users.sort_by { |u| -u.age }
25
+ end
26
+
27
+ specify "should chain successfully" do
28
+ User.select { |u| u.age > 22 }.sort_by { |u| -u.age }.entries.should == [ @users[3], @users[1], @users[2] ]
29
+ end
30
+
31
+ specify "should be able to revert to normal" do
32
+ block = proc { User.select { |m| m.name == 'PJ' }.first }
33
+
34
+ User.expects(:find).never
35
+ block.call.should == @users[1]
36
+
37
+ conditions = { :conditions => "users.name = 'PJ'", :limit => 1 }
38
+ User.expects(:find).with(:first, conditions)
39
+
40
+ User.ambition_source = nil
41
+ block.call
42
+ end
43
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: ambition
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2007-09-10 00:00:00 -07:00
6
+ version: 0.2.2
7
+ date: 2007-09-11 00:00:00 -07:00
8
8
  summary: Ambition builds SQL from plain jane Ruby.
9
9
  require_paths:
10
10
  - lib
@@ -38,6 +38,7 @@ files:
38
38
  - ./lib/ambition/select_processor.rb
39
39
  - ./lib/ambition/simple_processor.rb
40
40
  - ./lib/ambition/sort_processor.rb
41
+ - ./lib/ambition/source.rb
41
42
  - ./lib/ambition.rb
42
43
  - ./LICENSE
43
44
  - ./Manifest
@@ -69,9 +70,9 @@ files:
69
70
  - ./test/helper.rb
70
71
  - ./test/join_test.rb
71
72
  - ./test/limit_test.rb
72
- - ./test/mocks_test.rb
73
73
  - ./test/order_test.rb
74
74
  - ./test/profiler.rb
75
+ - ./test/source_test.rb
75
76
  - ./test/types_test.rb
76
77
  - ./test/where_test.rb
77
78
  test_files:
@@ -80,8 +81,8 @@ test_files:
80
81
  - test/enumerable_test.rb
81
82
  - test/join_test.rb
82
83
  - test/limit_test.rb
83
- - test/mocks_test.rb
84
84
  - test/order_test.rb
85
+ - test/source_test.rb
85
86
  - test/types_test.rb
86
87
  - test/where_test.rb
87
88
  rdoc_options: []
data/test/mocks_test.rb DELETED
@@ -1,29 +0,0 @@
1
- #require File.dirname(__FILE__) + '/helper'
2
- #
3
- #context "Setting the ambition_source" do
4
- # setup do
5
- # @users = [
6
- # OpenStruct.new(:name => 'Chris', :age => 22),
7
- # OpenStruct.new(:name => 'PJ', :age => 24),
8
- # OpenStruct.new(:name => 'Kevin', :age => 23),
9
- # OpenStruct.new(:name => '_why', :age => 65)
10
- # ]
11
- # User.ambition_source = @users
12
- # end
13
- #
14
- # teardown do
15
- # User.ambition_source = nil
16
- # end
17
- #
18
- # xspecify "should run all selects / detects against that collection" do
19
- # User.detect { |u| u.name == 'Chris' }.should == @users.first
20
- # end
21
- #
22
- # specify "should run all sorts against that collection" do
23
- # User.sort_by { |u| -u.age }.entries.should == @users.sort_by { |u| -u.age }
24
- # end
25
- #
26
- # xspecify "should chain successfully" do
27
- # User.select { |u| u.age > 22 }.sort_by { |u| -u.age }.entries.should == [ @users[3], @users[1], @users[2] ]
28
- # end
29
- #end