scopify 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -3
- data/VERSION +1 -1
- data/lib/scopify.rb +5 -0
- data/lib/scopify/scope.rb +3 -3
- data/scopify.gemspec +2 -2
- data/spec/scopify_spec.rb +27 -4
- metadata +3 -3
data/README.md
CHANGED
@@ -45,9 +45,21 @@ Roll your own condition composing.
|
|
45
45
|
# better now !
|
46
46
|
MyDBWrapper.scoped(:order => 'a').scoped(:order => 'b).all --> {:order => "a AND b"}
|
47
47
|
|
48
|
-
###
|
49
|
-
|
50
|
-
|
48
|
+
### Advanced: return_scope?(method_name)
|
49
|
+
Get raw arguments instead of a merged options hash.
|
50
|
+
class MyDBWrapper
|
51
|
+
def self.return_scope?(method_name)
|
52
|
+
return true if super
|
53
|
+
method_name == :i_return_scope
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.i_return_scope(pure_args)
|
57
|
+
scoped(pure_args.merge(:offset => 1))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
MyDBWrapper.scoped(:limit=>1).i_return_scope(:order => 'x')
|
62
|
+
--> receives {:order => 'x'}, not a merged options hash.
|
51
63
|
|
52
64
|
Author
|
53
65
|
======
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/scopify.rb
CHANGED
@@ -12,6 +12,11 @@ module Scopify
|
|
12
12
|
Scope.build(self, options)
|
13
13
|
end
|
14
14
|
|
15
|
+
# overwrite tis to enable your custom scopes
|
16
|
+
def return_scope?(method_name)
|
17
|
+
respond_to?("#{method_name}_scope_options")
|
18
|
+
end
|
19
|
+
|
15
20
|
def scope(name, options)
|
16
21
|
# give access to current options inside of evaled method
|
17
22
|
meta_class = (class << self; self; end)
|
data/lib/scopify/scope.rb
CHANGED
@@ -42,6 +42,7 @@ module Scopify
|
|
42
42
|
@options.map do |k,v|
|
43
43
|
result = case k
|
44
44
|
when :limit, :offset then v.min
|
45
|
+
when :conditions then "(#{v * ") AND ("})"
|
45
46
|
when :order then v * ', '
|
46
47
|
else v
|
47
48
|
end
|
@@ -58,14 +59,13 @@ module Scopify
|
|
58
59
|
end
|
59
60
|
|
60
61
|
def method_missing(method_name, *args, &block)
|
61
|
-
if @base.respond_to?(
|
62
|
+
if @base.respond_to?(:return_scope?) and @base.return_scope?(method_name)
|
62
63
|
# the method we call is a scope, continue chaining
|
63
|
-
scope = @base.send(method_name, *args)
|
64
|
+
scope = @base.send(method_name, *args, &block)
|
64
65
|
scope.scoped(self)
|
65
66
|
else
|
66
67
|
# the method we call is a normal method, flatten everything
|
67
68
|
options = (args.first||{})
|
68
|
-
options = options.merge(:limit => 1) if method_name.to_sym == :first
|
69
69
|
options = scoped(options).to_hash
|
70
70
|
@base.send(method_name, options, &block)
|
71
71
|
end
|
data/scopify.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{scopify}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
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-
|
12
|
+
s.date = %q{2010-05-14}
|
13
13
|
s.email = %q{grosser.michael@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.md"
|
data/spec/scopify_spec.rb
CHANGED
@@ -26,6 +26,22 @@ class T2
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
class T3
|
30
|
+
include Scopify
|
31
|
+
def self.foo(options)
|
32
|
+
options
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.return_scope?(name)
|
36
|
+
return true if super
|
37
|
+
name == :i_return_scope
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.i_return_scope(options)
|
41
|
+
scoped(options)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
29
45
|
describe Scopify do
|
30
46
|
describe :scoped do
|
31
47
|
it "returns a new scope" do
|
@@ -40,10 +56,6 @@ describe Scopify do
|
|
40
56
|
T1.scoped({:limit => 1}).foo(:offset => 1).should == {:limit => 1, :offset => 1}
|
41
57
|
end
|
42
58
|
|
43
|
-
it "adds limit => 1 to first queries" do
|
44
|
-
T1.scoped({:order => 'FOO'}).first.should == {:limit => 1, :order => 'FOO'}
|
45
|
-
end
|
46
|
-
|
47
59
|
it "can stack" do
|
48
60
|
T1.scoped(:limit => 1).scoped(:order => 'X').foo.should == {:limit => 1, :order => 'X'}
|
49
61
|
end
|
@@ -95,6 +107,17 @@ describe Scopify do
|
|
95
107
|
T1.scope(:ccc2, :order => 'b')
|
96
108
|
T1.ccc2.ccc.foo.should == {:order => 'b, a'}
|
97
109
|
end
|
110
|
+
|
111
|
+
it "calls methods with a options if they dont accept scopes" do
|
112
|
+
T3.scope(:aaa, :limit => 1)
|
113
|
+
T3.aaa.foo(:offset => 1).should == {:offset => 1, :limit => 1}
|
114
|
+
end
|
115
|
+
|
116
|
+
it "calls method with raw arguments if they return scope" do
|
117
|
+
T3.scope(:bbb, :limit => 1)
|
118
|
+
T3.should_receive(:i_return_scope).with(:offset => 1).and_return T3.bbb
|
119
|
+
T3.bbb.i_return_scope(:offset => 1).scope_options.should == T3.bbb.bbb.scope_options
|
120
|
+
end
|
98
121
|
end
|
99
122
|
|
100
123
|
it "has a VERSION" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
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-
|
17
|
+
date: 2010-05-14 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|