chaingang 0.0.3 → 0.1.0

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.
@@ -7,8 +7,16 @@ module ChainGang
7
7
  end
8
8
 
9
9
  module ClassMethods
10
- def find_with_chaingang(id=nil)
11
- Proxy.new(self).tap {|p| p.single id if id}
10
+ # the #find method is aliased to this.
11
+ # If you call #find with no arguments, it's assumed
12
+ # you're doing a find(:all).
13
+ # Article.find # defaults to :all
14
+ # Article.find(:first)
15
+ # Article.find(:last)
16
+ # Article.find(:one)
17
+ # Article.find('some-id')
18
+ def find_with_chaingang(find_scope = :all)
19
+ Proxy.new(self, find_scope)
12
20
  end
13
21
  end
14
22
  end
@@ -1,41 +1,48 @@
1
1
  module ChainGang
2
2
  class Proxy
3
- def initialize(client)
3
+ attr_accessor :find_scope
4
+
5
+ def initialize(client, id=nil)
4
6
  @client = client
5
7
  @params = {}
6
- @find_scope = :all
8
+ @find_scope = id || :all
7
9
  end
8
10
 
11
+ # Specify a custom url path to retrieve send
12
+ # the service call to.
13
+ # Article.find(:all).from :published
9
14
  def from(f); @from = f; self; end
10
15
 
11
- def one; @find_scope = :one; self; end
12
- def all; @find_scope = :all; self; end
13
- def first; @find_scope = :first; self; end
14
- def last; @find_scope = :last; self; end
15
-
16
- def single(id)
17
- @find_scope = id
18
- self
19
- end
20
-
16
+ # Simply improves readability. Does nothing but return self.
17
+ # Article.find.where.author!("moonmaster9000").and.published!(true)
21
18
  def and; self; end
19
+
20
+ # Simply improves readability. Does nothing but return self.
21
+ # Article.find.where.author!("moonmaster9000").and.published!(true)
22
22
  def where; self; end
23
23
 
24
- def method_missing(method_name, *args, &block)
25
- if args.length == 1
26
- @params[method_name] = args.first
27
- else
28
- super method_name, *args, &block
29
- end
24
+ # Suppose you have a parameter name that collides with an existing method.
25
+ # Simply set it with this param! method:
26
+ # Article.find(:all).param!(:from, "New York Times")
27
+ def param!(name, value)
28
+ @params[name] = value
30
29
  self
31
30
  end
32
31
 
33
- def each
34
- self.execute.each do |result|
35
- yield result
32
+ # Set the query string parameters on your request by calling them as methods
33
+ # with an exclamation point at the end.
34
+ # Article.find(:all).where.author!("moonmaster9000") #/articles.xml?author=moonmaster9000
35
+ def method_missing(method_name, *args, &block)
36
+ if args.length == 1 && exclamatory?(method_name)
37
+ @params[unexclaim method_name] = args.first
38
+ self
39
+ else
40
+ self.execute.send method_name, *args, &block
36
41
  end
37
42
  end
38
43
 
44
+ # Make the service call immediately.
45
+ # Article.find(:all).execute
39
46
  def execute
40
47
  options = {}
41
48
  options[:from] = @from if @from
@@ -44,8 +51,19 @@ module ChainGang
44
51
  end
45
52
 
46
53
  private
47
- def value(attr)
54
+ # @private
55
+ def value(attr) #:nodoc:
48
56
  eval "@#{attr}"
49
57
  end
58
+
59
+ # @private
60
+ def exclamatory?(string) #:nodoc:
61
+ string.to_s[-1..-1] == "!"
62
+ end
63
+
64
+ # @private
65
+ def unexclaim(string) #:nodoc:
66
+ string.to_s[0..-2].to_sym
67
+ end
50
68
  end
51
69
  end
@@ -18,7 +18,7 @@ describe ChainGang do
18
18
 
19
19
  context "when called with an argument" do
20
20
  it "should return a ChainGang::Proxy" do
21
- Client.find(1).class.should == ChainGang::Proxy
21
+ Client.find(:first).class.should == ChainGang::Proxy
22
22
  end
23
23
 
24
24
  it "should set the @client to the calling class" do
@@ -36,30 +36,6 @@ describe ChainGang do
36
36
  @proxy = Client.find
37
37
  end
38
38
 
39
- describe "#all" do
40
- it "should set @find_scope to :all" do
41
- @proxy.all.send(:value, :find_scope).should == :all
42
- end
43
- end
44
-
45
- describe "#first" do
46
- it "should set @find_scope to :first" do
47
- @proxy.first.send(:value, :find_scope).should == :first
48
- end
49
- end
50
-
51
- describe "#last" do
52
- it "should set @find_scope to :last" do
53
- @proxy.last.send(:value, :find_scope).should == :last
54
- end
55
- end
56
-
57
- describe "#one" do
58
- it "should set @find_scope to :one" do
59
- @proxy.one.send(:value, :find_scope).should == :one
60
- end
61
- end
62
-
63
39
  describe "#from" do
64
40
  it "should require an argument" do
65
41
  proc { @proxy.from }.should raise_exception
@@ -87,26 +63,71 @@ describe ChainGang do
87
63
  @proxy = Client.find
88
64
  end
89
65
 
90
- it "should allow you to set any parameter" do
91
- @proxy.food("taco").send(:value, :params)[:food].should == "taco"
66
+ it "should set a parameter when you call a method that ends with an exclamation and has exactly one argument" do
67
+ @proxy.food!("taco").send(:value, :params)[:food].should == "taco"
68
+ end
69
+
70
+ it "should proxy the method to the executed result otherwise" do
71
+ Dupe.stub 5, :clients
72
+ @proxy.count.should == 5
92
73
  end
93
74
  end
94
75
 
95
76
  describe "#execute" do
96
77
  it "should call the #find_without_chaingang method on the original client class" do
97
78
  Client.should_receive(:find_without_chaingang).with(:all, :from => "/poo", :params => { :hi => 'hi', :yes => 'no' }).and_return([])
98
- Client.find.from("/poo").where.hi("hi").and.yes("no").execute
79
+ Client.find.from("/poo").where.hi!("hi").and.yes!("no").execute
99
80
  end
100
81
  end
82
+
83
+ describe "#param!" do
84
+ before do
85
+ @proxy = Client.find
86
+ end
87
+
88
+ it "should require two arguments: param name and param value" do
89
+ proc { @proxy.param! }.should raise_exception
90
+ proc { @proxy.param! :param_name }.should raise_exception
91
+ proc { @proxy.param! :param_name, "param value" }.should_not raise_exception
92
+ end
93
+
94
+ it "should set the parameter" do
95
+ @proxy.param! :param_name, "param value"
96
+ @proxy.send(:value, :params)[:param_name].should == "param value"
97
+ end
98
+ end
99
+
100
+ describe "#first" do
101
+ before do
102
+ @proxy = Client.find
103
+ end
104
+
105
+ it "should call the execute method, then call #first on the result" do
106
+ @proxy.should_receive(:execute).and_return ['hi', 'there']
107
+ @proxy.first.should == 'hi'
108
+ end
109
+ end
110
+
111
+ describe "#last" do
112
+ before do
113
+ @proxy = Client.find
114
+ end
115
+
116
+ it "should call the execute method, then call #last on the result" do
117
+ @proxy.should_receive(:execute).and_return ['hi', 'there']
118
+ @proxy.last.should == 'there'
119
+ end
120
+ end
121
+
101
122
 
102
123
  describe "#each" do
103
124
  before do
104
125
  Dupe.stub 10, :clients
105
- @proxy = Client.find.all
126
+ @proxy = Client.find
106
127
  end
107
128
 
108
129
  it "should call #execute" do
109
- @proxy.should_receive(:execute).and_return([])
130
+ @proxy.should_receive(:execute).and_return []
110
131
  @proxy.each {}
111
132
  end
112
133
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chaingang
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Parker