scopify 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,6 +23,14 @@ Create named scopes, with options, lambdas or other scopes
23
23
  MyDBWrapper.good.first
24
24
  MyDBWrapper.good.goodness(3).first
25
25
 
26
+ ### simple_scope
27
+ Defines otherwise repetitive named scopes for you.
28
+
29
+ simple_scope :limit, :order, :where => :conditions
30
+ --> MyDBWrapper.limit(3).order('foo').where("1 = 2") ==
31
+ {:limit => 3, :order => 'foo', :conditions => "1 = 2"}
32
+
33
+
26
34
  ### Custom scope helpers
27
35
  class MyDBWrapper
28
36
  def self.foo(num)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.5
@@ -37,5 +37,17 @@ module Scopify
37
37
  end
38
38
  CODE
39
39
  end
40
+
41
+ def simple_scope(*args)
42
+ args.each do |names|
43
+ if names.is_a?(Hash)
44
+ names.each do |name, value|
45
+ scope name, lambda{|x| {value => x}}
46
+ end
47
+ else
48
+ scope names, lambda{|x| {names => x}}
49
+ end
50
+ end
51
+ end
40
52
  end
41
53
  end
@@ -68,10 +68,13 @@ module Scopify
68
68
  result = @base.send(method_name, *args, &block)
69
69
  result.is_a?(Scope) ? scoped(result) : result
70
70
  else
71
- # the method we call is a normal method, flatten everything
72
- options = (args.first||{})
71
+ # the method we call is a normal method
72
+ # - scope by options from last method call
73
+ # - flatten scope to options hash
74
+ options = (args.last.is_a?(Hash) ? args.pop : {})
73
75
  options = scoped(options).to_hash
74
- @base.send(method_name, options, &block)
76
+ args << options
77
+ @base.send(method_name, *args, &block)
75
78
  end
76
79
  end
77
80
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{scopify}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2010-05-14}
12
+ s.date = %q{2010-05-24}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.md"
@@ -42,22 +42,47 @@ class T3
42
42
  end
43
43
  end
44
44
 
45
+ class T4
46
+ include Scopify
47
+ simple_scope :limit, :where => :conditions
48
+ end
49
+
50
+ class T5
51
+ include Scopify
52
+
53
+ def self.foo(*args)
54
+ args
55
+ end
56
+ end
57
+
45
58
  describe Scopify do
46
59
  describe :scoped do
47
60
  it "returns a new scope" do
48
61
  T1.scoped({}).class.should == Scopify::Scope
49
62
  end
50
63
 
64
+ it "can call hash on scope to reach base" do
65
+ T5.scoped({:limit => 1}).foo.should == [{:limit => 1}]
66
+ end
67
+
51
68
  it "can call anything on scope to reach base" do
52
- T1.scoped({:limit => 1}).foo.should == {:limit => 1}
69
+ T5.scoped({:limit => 1}).foo(:xxx).should == [:xxx, {:limit => 1}]
53
70
  end
54
71
 
55
- it "can call anything giving additional options" do
56
- T1.scoped({:limit => 1}).foo(:offset => 1).should == {:limit => 1, :offset => 1}
72
+ it "can call giving additional options" do
73
+ T5.scoped({:limit => 1}).foo(:offset => 1).should == [{:limit => 1, :offset => 1}]
74
+ end
75
+
76
+ it "can call with args and giving additional options" do
77
+ T5.scoped({:limit => 1}).foo(:xxx, :offset => 1).should == [:xxx, {:limit => 1, :offset => 1}]
57
78
  end
58
79
 
59
80
  it "can stack" do
60
- T1.scoped(:limit => 1).scoped(:order => 'X').foo.should == {:limit => 1, :order => 'X'}
81
+ T5.scoped(:limit => 1).scoped(:order => 'X').foo.should == [{:limit => 1, :order => 'X'}]
82
+ end
83
+
84
+ it "can stack with additional args" do
85
+ T5.scoped(:limit => 1).scoped(:order => 'X').foo(:xxx).should == [:xxx, {:limit => 1, :order => 'X'}]
61
86
  end
62
87
 
63
88
  it "overwrites limit with the minimum" do
@@ -119,7 +144,7 @@ describe Scopify do
119
144
  T3.bbb.i_scope(:offset => 2)
120
145
  end
121
146
 
122
- it "scopes in corret order when using scope-returning method" do
147
+ it "scopes in correct order when using scope-returning method" do
123
148
  T3.scope(:ccc, :limit => 1)
124
149
  T3.stub!(:i_scope).and_return T3.scoped(:offset => 2)
125
150
  T3.bbb.i_scope(:offset => 2).scope_options.should == T3.bbb.scoped(:offset => 2).scope_options
@@ -132,6 +157,16 @@ describe Scopify do
132
157
  end
133
158
  end
134
159
 
160
+ describe :simple_scope do
161
+ it "builds simple scopes" do
162
+ T4.limit(1).scope_options.should == {:limit => [1]}
163
+ end
164
+
165
+ it "builds renamed scopes" do
166
+ T4.where('x').scope_options.should == {:conditions => ['x']}
167
+ end
168
+ end
169
+
135
170
  describe "default to_hash" do
136
171
  it "merges conditions that are hashes" do
137
172
  T1.scoped(:conditions => {:x=>true}).scoped(:conditions => {:y => true}).to_hash.should == {:conditions => {:x => true, :y => true}}
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Grosser
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-14 00:00:00 +02:00
17
+ date: 2010-05-24 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20