scopify 0.1.1 → 0.1.2

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.
data/README.md CHANGED
@@ -23,6 +23,25 @@ Create named scopes, with options, lambdas or other scopes
23
23
  MyDBWrapper.good.first
24
24
  MyDBWrapper.good.goodness(3).first
25
25
 
26
+ ### Custom scope helpers
27
+ class MyDBWrapper
28
+ def self.foo(num)
29
+ scoped(:foo => num)
30
+ end
31
+
32
+ def self.bar(num)
33
+ scoped(:bar => num)
34
+ end
35
+
36
+ # tell scopes to pass raw arguments to foo and bar (not merged options hash)
37
+ def self.raw_args_from_scope?(method_name)
38
+ return true if super
39
+ [:foo, :bar].include?(method_name)
40
+ end
41
+ end
42
+
43
+ MyDBWrapper.foo(1).bar(3) == MyDBWrapper.scoped(:foo => 1, :bar => 3)
44
+
26
45
  ### scope_to_hash
27
46
  Roll your own condition composing.
28
47
  # not good ?
@@ -45,22 +64,6 @@ Roll your own condition composing.
45
64
  # better now !
46
65
  MyDBWrapper.scoped(:order => 'a').scoped(:order => 'b).all --> {:order => "a AND b"}
47
66
 
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.
63
-
64
67
  Author
65
68
  ======
66
69
  [Michael Grosser](http://pragmatig.wordpress.com)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/scopify.rb CHANGED
@@ -12,8 +12,8 @@ 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)
15
+ # overwrite tis to build your custom scopes
16
+ def raw_args_from_scope?(method_name)
17
17
  respond_to?("#{method_name}_scope_options")
18
18
  end
19
19
 
@@ -32,7 +32,7 @@ module Scopify
32
32
  elsif options.is_a?(Scope)
33
33
  options
34
34
  else
35
- scoped(options)
35
+ scoped(options)
36
36
  end
37
37
  end
38
38
  CODE
data/lib/scopify/scope.rb CHANGED
@@ -20,9 +20,8 @@ module Scopify
20
20
  if options.is_a?(Scope)
21
21
  # merge in raw options e.g. :limit => [1, 2]
22
22
  options.scope_options.each do |k,v|
23
- old = merged[k] || []
24
- merged[k] = v
25
- old.each{|x| merged[k] << x }
23
+ merged[k] ||= []
24
+ v.each{|x| merged[k] << x }
26
25
  end
27
26
  else
28
27
  # merge in a normal hash e.g. :limit => 1
@@ -59,10 +58,10 @@ module Scopify
59
58
  end
60
59
 
61
60
  def method_missing(method_name, *args, &block)
62
- if @base.respond_to?(:return_scope?) and @base.return_scope?(method_name)
61
+ if @base.respond_to?(:raw_args_from_scope?) and @base.raw_args_from_scope?(method_name)
63
62
  # the method we call is a scope, continue chaining
64
- scope = @base.send(method_name, *args, &block)
65
- scope.scoped(self)
63
+ result = @base.send(method_name, *args, &block)
64
+ result.is_a?(Scope) ? scoped(result) : result
66
65
  else
67
66
  # the method we call is a normal method, flatten everything
68
67
  options = (args.first||{})
data/scopify.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{scopify}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
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"]
data/spec/scopify_spec.rb CHANGED
@@ -32,12 +32,12 @@ class T3
32
32
  options
33
33
  end
34
34
 
35
- def self.return_scope?(name)
35
+ def self.raw_args_from_scope?(name)
36
36
  return true if super
37
- name == :i_return_scope
37
+ name == :i_scope
38
38
  end
39
39
 
40
- def self.i_return_scope(options)
40
+ def self.i_scope(options)
41
41
  scoped(options)
42
42
  end
43
43
  end
@@ -115,8 +115,20 @@ describe Scopify do
115
115
 
116
116
  it "calls method with raw arguments if they return scope" do
117
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
118
+ T3.should_receive(:i_scope).with(:offset => 2).and_return T3.scoped(:offset => 2)
119
+ T3.bbb.i_scope(:offset => 2)
120
+ end
121
+
122
+ it "scopes in corret order when using scope-returning method" do
123
+ T3.scope(:ccc, :limit => 1)
124
+ T3.stub!(:i_scope).and_return T3.scoped(:offset => 2)
125
+ T3.bbb.i_scope(:offset => 2).scope_options.should == T3.bbb.scoped(:offset => 2).scope_options
126
+ end
127
+
128
+ it "does not crash when scope-returning method does not return a scope" do
129
+ T3.scope(:ccc, :limit => 1)
130
+ T3.stub!(:i_scope).and_return []
131
+ T3.ccc.i_scope(:offset => 2).should == []
120
132
  end
121
133
  end
122
134
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Grosser