scope_composer 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/UPGRADE.md +6 -2
- data/lib/scope_composer/scope.rb +7 -3
- data/lib/scope_composer/version.rb +1 -1
- data/spec/scope_composer/model_spec.rb +36 -19
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OTJjNjcxMTFlZGY3OTAwMzE2MjRiMjZlNzcwODA5YmZlODU3MTM0ZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ODhiMTAzMTA3ZTQyOWY4M2U3ZjJlYWRiNzZjYjQwODgyOGM2ZjJlMw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDg5MjQ0OTA5MThkYmNiOTU3NjJkYWMwNjA4ZTcyMDk2NWM3NjcxOTVmZTAy
|
10
|
+
YWIwN2Y0Y2E0Yjg5Y2VlNDE1YWU3MzhjYmNhYWFjZjAyMzkyNjM4NWMyYjA0
|
11
|
+
MmJjMWQ3ODQ0YmVhY2E0NzcxYmY1OTFmOTBiZGFiZWRlNGFmOGI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTk5M2I3MmFjMTFiNDBjNmY0NDY2MTFkMzllN2VjODk5NGRlZGY2NzNiZGI2
|
14
|
+
Yzg3ZDVlYjAwNzY1YzdmMTYxMjg4Mzg5MzkxYTkzN2FmZDMxZWNhNjU5YzMy
|
15
|
+
MGZmZTEwODkxMGViZDYzNmM0ODk5MWRkNWZkMDhhYThhZjVkNTI=
|
data/UPGRADE.md
CHANGED
data/lib/scope_composer/scope.rb
CHANGED
@@ -92,7 +92,7 @@ class Scope
|
|
92
92
|
# define method
|
93
93
|
define_method(name) do |*args|
|
94
94
|
# if no value is given, act as a getter and retrieve the value
|
95
|
-
return
|
95
|
+
return scope_attributes[name] if args.first.nil?
|
96
96
|
# otherwise set the value
|
97
97
|
instance_exec(*args, &proc)
|
98
98
|
# and return self for chaining
|
@@ -118,9 +118,9 @@ class Scope
|
|
118
118
|
# init
|
119
119
|
value = args.first
|
120
120
|
# if no value is given, act as a getter
|
121
|
-
return
|
121
|
+
return scope_attributes[name] if value.nil?
|
122
122
|
# otherwise set the value
|
123
|
-
|
123
|
+
scope_attributes[name] = value
|
124
124
|
# and return self for chaining
|
125
125
|
return self
|
126
126
|
end
|
@@ -161,5 +161,9 @@ class Scope
|
|
161
161
|
@attributes = self.attributes.merge(attrs)
|
162
162
|
end
|
163
163
|
|
164
|
+
def scope_attributes
|
165
|
+
@scope_attributes ||= {}
|
166
|
+
end
|
167
|
+
|
164
168
|
end
|
165
169
|
end
|
@@ -2,24 +2,24 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe ScopeComposer::Model do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
TestClass
|
5
|
+
|
6
|
+
class ScopeComposerModelTest
|
7
|
+
include ScopeComposer::Model
|
8
|
+
|
9
|
+
has_scope_composer
|
10
|
+
scope :say_hi, ->(t){ 'hi' }
|
11
|
+
scope_helper :helper_method, ->(t){ 'hi' }
|
12
|
+
|
13
|
+
scope_composer_for :search
|
14
|
+
search_scope :limit
|
15
|
+
search_scope :offset, prefix: true
|
16
|
+
search_helper :tester, ->(t){ t.to_i }
|
17
|
+
|
21
18
|
end
|
22
19
|
|
20
|
+
let(:scope){ ScopeComposerModelTest }
|
21
|
+
subject{ scope }
|
22
|
+
|
23
23
|
it { should respond_to :scope_composer_for }
|
24
24
|
it { should respond_to :scope_scope }
|
25
25
|
it { should respond_to :scope }
|
@@ -32,12 +32,29 @@ describe ScopeComposer::Model do
|
|
32
32
|
it { should_not respond_to :tester }
|
33
33
|
|
34
34
|
it "should define a scope helper" do
|
35
|
-
|
35
|
+
ScopeComposerModelTest.say_hi('hi').should respond_to :helper_method
|
36
|
+
end
|
37
|
+
|
38
|
+
describe ".search_scope" do
|
39
|
+
let(:scope){ ScopeComposerModelTest.search_scope.new }
|
40
|
+
subject{ scope }
|
41
|
+
before(:each){ scope.limit(10) }
|
42
|
+
|
43
|
+
its(:scope_attributes){ should eq({ limit: 10 }) }
|
44
|
+
its(:attributes){ should eq({}) }
|
45
|
+
|
46
|
+
describe "#where" do
|
47
|
+
before(:each){ scope.where( id: 20 ) }
|
48
|
+
|
49
|
+
its(:scope_attributes){ should eq({ limit: 10 }) }
|
50
|
+
its(:attributes){ should eq({ id: 20 }) }
|
51
|
+
end
|
52
|
+
|
36
53
|
end
|
37
54
|
|
38
|
-
describe "
|
55
|
+
describe ".scope" do
|
39
56
|
it "should return self" do
|
40
|
-
subject.limit(1).where( id: 1 ).class.should eq
|
57
|
+
subject.limit(1).where( id: 1 ).class.should eq ScopeComposerModelTest::SearchScope
|
41
58
|
end
|
42
59
|
end
|
43
60
|
|