chaingang 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1 @@
1
+ PUBLIC DOMAIN
data/lib/chaingang.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'active_resource'
2
+ require 'chaingang/proxy'
3
+ require 'chaingang/chaingang'
@@ -0,0 +1,18 @@
1
+ module ChainGang
2
+ def self.included(base)
3
+ base.extend ChainGang::ClassMethods
4
+ class << base
5
+ alias_method_chain :find, :chaingang
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ def find_with_chaingang(id=nil)
11
+ if id
12
+ find_without_chaingang id
13
+ else
14
+ Proxy.new self
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ module ChainGang
2
+ class Proxy
3
+ def initialize(client)
4
+ @client = client
5
+ @params = {}
6
+ @find_scope = :all
7
+ end
8
+
9
+ def from(f); @from = f; self; end
10
+
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
+
21
+ def and; self; end
22
+ def where; self; end
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
30
+ self
31
+ end
32
+
33
+ def each
34
+ self.execute.each do |result|
35
+ yield result
36
+ end
37
+ end
38
+
39
+ def execute
40
+ options = {}
41
+ options[:from] = @from if @from
42
+ options[:params] = @params
43
+ @client.find_without_chaingang(@find_scope, options)
44
+ end
45
+
46
+ private
47
+ def value(attr)
48
+ eval "@#{attr}"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChainGang do
4
+ describe "##find" do
5
+ context "when called with no arguments" do
6
+ it "should return a ChainGang::Proxy" do
7
+ Client.find.class.should == ChainGang::Proxy
8
+ end
9
+
10
+ it "should set the @client to the calling class" do
11
+ Client.find.send(:value, :client).should == Client
12
+ end
13
+
14
+ it "should default @find_scope to :all" do
15
+ Client.find.send(:value, :find_scope).should == :all
16
+ end
17
+ end
18
+
19
+ context "when called with an argument" do
20
+ it "should return the result" do
21
+ Dupe.create :client, :name => 'test 1'
22
+ @result = Client.find 1
23
+ @result.name.should == 'test 1'
24
+ end
25
+ end
26
+ end
27
+
28
+ describe ChainGang::Proxy do
29
+ before do
30
+ @proxy = Client.find
31
+ end
32
+
33
+ describe "#all" do
34
+ it "should set @find_scope to :all" do
35
+ @proxy.all.send(:value, :find_scope).should == :all
36
+ end
37
+ end
38
+
39
+ describe "#first" do
40
+ it "should set @find_scope to :first" do
41
+ @proxy.first.send(:value, :find_scope).should == :first
42
+ end
43
+ end
44
+
45
+ describe "#last" do
46
+ it "should set @find_scope to :last" do
47
+ @proxy.last.send(:value, :find_scope).should == :last
48
+ end
49
+ end
50
+
51
+ describe "#one" do
52
+ it "should set @find_scope to :one" do
53
+ @proxy.one.send(:value, :find_scope).should == :one
54
+ end
55
+ end
56
+
57
+ describe "#from" do
58
+ it "should require an argument" do
59
+ proc { @proxy.from }.should raise_exception
60
+ end
61
+
62
+ it "should set @from to the argument" do
63
+ @proxy.from("/testing").send(:value, :from).should == "/testing"
64
+ end
65
+ end
66
+
67
+ describe "#where" do
68
+ it "should do nothing and return the proxy" do
69
+ @proxy.where.should == @proxy
70
+ end
71
+ end
72
+
73
+ describe "#and" do
74
+ it "should do nothing and return the proxy" do
75
+ @proxy.and.should == @proxy
76
+ end
77
+ end
78
+
79
+ describe "#method_missing" do
80
+ before do
81
+ @proxy = Client.find
82
+ end
83
+
84
+ it "should allow you to set any parameter" do
85
+ @proxy.food("taco").send(:value, :params)[:food].should == "taco"
86
+ end
87
+ end
88
+
89
+ describe "#execute" do
90
+ it "should call the #find_without_chaingang method on the original client class" do
91
+ Client.should_receive(:find_without_chaingang).with(:all, :from => "/poo", :params => { :hi => 'hi', :yes => 'no' }).and_return([])
92
+ Client.find.from("/poo").where.hi("hi").and.yes("no").execute
93
+ end
94
+ end
95
+
96
+ describe "#each" do
97
+ before do
98
+ Dupe.stub 10, :clients
99
+ @proxy = Client.find.all
100
+ end
101
+
102
+ it "should call #execute" do
103
+ @proxy.should_receive(:execute).and_return([])
104
+ @proxy.each {}
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift './lib'
2
+ require 'chaingang'
3
+ require 'dupe'
4
+
5
+ class Client < ActiveResource::Base
6
+ self.site = 'http://localhost'
7
+ include ChainGang
8
+ end
9
+
10
+ Dupe.define :client do |attrs|
11
+ attrs.name 'test'
12
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chaingang
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matt Parker
14
+ - Gary Cheong
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-01-08 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: activeresource
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 2
33
+ - 2
34
+ version: "2.2"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: AREL has proven that chainable APIs rock. Let's give ActiveResource the same love.
38
+ email: moonmaster9000@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ files:
46
+ - lib/chaingang.rb
47
+ - lib/chaingang/chaingang.rb
48
+ - lib/chaingang/proxy.rb
49
+ - LICENSE
50
+ - spec/chaingang/proxy_spec.rb
51
+ - spec/spec_helper.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/moonmaster9000/chaingang
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A chainable API for ActiveResource.
86
+ test_files:
87
+ - spec/chaingang/proxy_spec.rb
88
+ - spec/spec_helper.rb