zertico 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -1
- data/lib/zertico/controller.rb +11 -14
- data/lib/zertico/service.rb +17 -21
- data/lib/zertico/version.rb +1 -1
- data/spec/zertico/controller_spec.rb +7 -8
- data/spec/zertico/service_spec.rb +12 -36
- metadata +4 -4
data/README.md
CHANGED
@@ -55,7 +55,8 @@ end
|
|
55
55
|
|
56
56
|
Your service must be a module and include Zertico::Service to grant access to all the methods already defined.
|
57
57
|
Then you will have to redefine all the methods you need. Each action of the controller is mapped to a method.
|
58
|
-
The
|
58
|
+
The return of the method will be passed to respond_with.
|
59
|
+
You can pass extra options to respond_with by defining @options, default to blank hash {}.
|
59
60
|
|
60
61
|
Sometimes, the ActiveRecord models grow to much. It start to handle all kinds of logic. To make things simple,
|
61
62
|
it should only concern about database access. To clean it, use the Zertico::Accessor. It is a wrapper that will
|
data/lib/zertico/controller.rb
CHANGED
@@ -12,38 +12,35 @@ module Zertico
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def index
|
15
|
-
all
|
16
|
-
respond_with(@responder, @options)
|
15
|
+
respond_with(all, options)
|
17
16
|
end
|
18
17
|
|
19
18
|
def new
|
20
|
-
build
|
21
|
-
respond_with(@responder, @options)
|
19
|
+
respond_with(build, options)
|
22
20
|
end
|
23
21
|
|
24
22
|
def show
|
25
|
-
find
|
26
|
-
respond_with(@responder, @options)
|
23
|
+
respond_with(find, options)
|
27
24
|
end
|
28
25
|
|
29
26
|
def edit
|
30
|
-
find
|
31
|
-
respond_with(@responder, @options)
|
27
|
+
respond_with(find, options)
|
32
28
|
end
|
33
29
|
|
34
30
|
def create
|
35
|
-
generate
|
36
|
-
respond_with(@responder, @options)
|
31
|
+
respond_with(generate, options)
|
37
32
|
end
|
38
33
|
|
39
34
|
def update
|
40
|
-
modify
|
41
|
-
respond_with(@responder, @options)
|
35
|
+
respond_with(modify, options)
|
42
36
|
end
|
43
37
|
|
44
38
|
def destroy
|
45
|
-
delete
|
46
|
-
|
39
|
+
respond_with(delete, options)
|
40
|
+
end
|
41
|
+
|
42
|
+
def options
|
43
|
+
@options || {}
|
47
44
|
end
|
48
45
|
end
|
49
46
|
end
|
data/lib/zertico/service.rb
CHANGED
@@ -1,57 +1,53 @@
|
|
1
1
|
module Zertico
|
2
2
|
module Service
|
3
3
|
def all
|
4
|
-
|
5
|
-
instance_variable_set("@#{interface_name.pluralize}", @responder)
|
6
|
-
@options = {}
|
4
|
+
instance_variable_set("@#{interface_name.pluralize}", resource.all)
|
7
5
|
end
|
8
6
|
|
9
7
|
def build
|
10
|
-
|
11
|
-
instance_variable_set("@#{interface_name}", @responder)
|
12
|
-
@options = {}
|
8
|
+
instance_variable_set("@#{interface_name}", resource.new)
|
13
9
|
end
|
14
10
|
|
15
11
|
def find
|
16
|
-
|
17
|
-
instance_variable_set("@#{interface_name}", @responder)
|
18
|
-
@options = {}
|
12
|
+
instance_variable_set("@#{interface_name}", resource.find(params[interface_id.to_sym]))
|
19
13
|
end
|
20
14
|
|
21
15
|
def generate
|
22
|
-
|
23
|
-
instance_variable_set("@#{interface_name}", @responder)
|
24
|
-
@options = {}
|
16
|
+
instance_variable_set("@#{interface_name}", resource.create(params[interface_name.to_sym]))
|
25
17
|
end
|
26
18
|
|
27
19
|
def modify
|
28
20
|
find
|
29
|
-
|
30
|
-
|
21
|
+
instance_variable_get("@#{interface_name}").update_attributes(params[interface_name.to_sym])
|
22
|
+
instance_variable_get("@#{interface_name}")
|
31
23
|
end
|
32
24
|
|
33
25
|
def delete
|
34
26
|
find
|
35
|
-
|
36
|
-
|
27
|
+
instance_variable_get("@#{interface_name}").destroy
|
28
|
+
instance_variable_get("@#{interface_name}")
|
37
29
|
end
|
38
30
|
|
39
31
|
def resource
|
40
|
-
@
|
32
|
+
@resource_object ||= interface_class
|
41
33
|
end
|
42
34
|
|
43
35
|
def resource=(resource_chain = [])
|
44
|
-
@
|
45
|
-
@
|
36
|
+
@resource_object = resource_chain.shift
|
37
|
+
@resource_object = @resource_object.constantize if @resource_object.respond_to?(:constantize)
|
46
38
|
resource_chain.each do |resource|
|
47
|
-
@
|
39
|
+
@resource_object = @resource_object.send(resource)
|
48
40
|
end
|
49
41
|
end
|
50
42
|
|
51
43
|
protected
|
52
44
|
|
53
45
|
def interface_id
|
54
|
-
|
46
|
+
begin
|
47
|
+
return "#{interface_name}_id" if self.class.name.chomp('Controller').split('::').size > 1
|
48
|
+
rescue NameError
|
49
|
+
'id'
|
50
|
+
end
|
55
51
|
'id'
|
56
52
|
end
|
57
53
|
|
data/lib/zertico/version.rb
CHANGED
@@ -35,14 +35,13 @@ describe Zertico::Controller do
|
|
35
35
|
|
36
36
|
context 'actions' do
|
37
37
|
before :each do
|
38
|
-
controller.instance_variable_set('@responder', 'responder')
|
39
38
|
controller.instance_variable_set('@options', 'options')
|
40
39
|
controller.stub(:respond_with).with('responder', 'options')
|
41
40
|
end
|
42
41
|
|
43
42
|
context '#index' do
|
44
43
|
before :each do
|
45
|
-
controller.stub(:all)
|
44
|
+
controller.stub(:all => 'responder')
|
46
45
|
end
|
47
46
|
|
48
47
|
after :each do
|
@@ -60,7 +59,7 @@ describe Zertico::Controller do
|
|
60
59
|
|
61
60
|
context '#new' do
|
62
61
|
before :each do
|
63
|
-
controller.stub(:build)
|
62
|
+
controller.stub(:build => 'responder')
|
64
63
|
end
|
65
64
|
|
66
65
|
after :each do
|
@@ -78,7 +77,7 @@ describe Zertico::Controller do
|
|
78
77
|
|
79
78
|
context '#show' do
|
80
79
|
before :each do
|
81
|
-
controller.stub(:find)
|
80
|
+
controller.stub(:find => 'responder')
|
82
81
|
end
|
83
82
|
|
84
83
|
after :each do
|
@@ -96,7 +95,7 @@ describe Zertico::Controller do
|
|
96
95
|
|
97
96
|
context '#edit' do
|
98
97
|
before :each do
|
99
|
-
controller.stub(:find)
|
98
|
+
controller.stub(:find => 'responder')
|
100
99
|
end
|
101
100
|
|
102
101
|
after :each do
|
@@ -114,7 +113,7 @@ describe Zertico::Controller do
|
|
114
113
|
|
115
114
|
context '#create' do
|
116
115
|
before :each do
|
117
|
-
controller.stub(:generate)
|
116
|
+
controller.stub(:generate => 'responder')
|
118
117
|
end
|
119
118
|
|
120
119
|
after :each do
|
@@ -132,7 +131,7 @@ describe Zertico::Controller do
|
|
132
131
|
|
133
132
|
context '#update' do
|
134
133
|
before :each do
|
135
|
-
controller.stub(:modify)
|
134
|
+
controller.stub(:modify => 'responder')
|
136
135
|
end
|
137
136
|
|
138
137
|
after :each do
|
@@ -150,7 +149,7 @@ describe Zertico::Controller do
|
|
150
149
|
|
151
150
|
context '#destroy' do
|
152
151
|
before :each do
|
153
|
-
controller.stub(:delete)
|
152
|
+
controller.stub(:delete => 'responder')
|
154
153
|
end
|
155
154
|
|
156
155
|
after :each do
|
@@ -41,12 +41,8 @@ describe Zertico::Service do
|
|
41
41
|
controller.instance_variable_get('@users').should == []
|
42
42
|
end
|
43
43
|
|
44
|
-
it 'should
|
45
|
-
controller.
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'should save a hash with options on an instance variable' do
|
49
|
-
controller.instance_variable_get('@options').should == {}
|
44
|
+
it 'should return a collection' do
|
45
|
+
controller.all.should == []
|
50
46
|
end
|
51
47
|
end
|
52
48
|
|
@@ -59,13 +55,9 @@ describe Zertico::Service do
|
|
59
55
|
it 'should save an object on an instance variable' do
|
60
56
|
controller.instance_variable_get('@user').should == object
|
61
57
|
end
|
62
|
-
|
63
|
-
it 'should save the object on an instance variable to respond' do
|
64
|
-
controller.instance_variable_get('@responder').should == object
|
65
|
-
end
|
66
58
|
|
67
|
-
it 'should
|
68
|
-
controller.
|
59
|
+
it 'should return an object' do
|
60
|
+
controller.build.should == object
|
69
61
|
end
|
70
62
|
end
|
71
63
|
|
@@ -80,12 +72,8 @@ describe Zertico::Service do
|
|
80
72
|
controller.instance_variable_get('@user').should == object
|
81
73
|
end
|
82
74
|
|
83
|
-
it 'should
|
84
|
-
controller.
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'should save a hash with options on an instance variable' do
|
88
|
-
controller.instance_variable_get('@options').should == {}
|
75
|
+
it 'should return an object' do
|
76
|
+
controller.find.should == object
|
89
77
|
end
|
90
78
|
end
|
91
79
|
|
@@ -100,12 +88,8 @@ describe Zertico::Service do
|
|
100
88
|
controller.instance_variable_get('@user').should == object
|
101
89
|
end
|
102
90
|
|
103
|
-
it 'should
|
104
|
-
controller.
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'should save a hash with options on an instance variable' do
|
108
|
-
controller.instance_variable_get('@options').should == {}
|
91
|
+
it 'should return an object' do
|
92
|
+
controller.generate.should == object
|
109
93
|
end
|
110
94
|
end
|
111
95
|
|
@@ -121,12 +105,8 @@ describe Zertico::Service do
|
|
121
105
|
controller.instance_variable_get('@user').should == object
|
122
106
|
end
|
123
107
|
|
124
|
-
it 'should
|
125
|
-
controller.
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'should save a hash with options on an instance variable' do
|
129
|
-
controller.instance_variable_get('@options').should == {}
|
108
|
+
it 'should return an object' do
|
109
|
+
controller.modify.should == object
|
130
110
|
end
|
131
111
|
end
|
132
112
|
|
@@ -142,12 +122,8 @@ describe Zertico::Service do
|
|
142
122
|
controller.instance_variable_get('@user').should == object
|
143
123
|
end
|
144
124
|
|
145
|
-
it 'should
|
146
|
-
controller.
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'should save a hash with options on an instance variable' do
|
150
|
-
controller.instance_variable_get('@options').should == {}
|
125
|
+
it 'should return an object' do
|
126
|
+
controller.delete.should == object
|
151
127
|
end
|
152
128
|
end
|
153
129
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zertico
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -115,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
115
|
version: '0'
|
116
116
|
segments:
|
117
117
|
- 0
|
118
|
-
hash:
|
118
|
+
hash: -898163586098864558
|
119
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
120
|
none: false
|
121
121
|
requirements:
|
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
segments:
|
126
126
|
- 0
|
127
|
-
hash:
|
127
|
+
hash: -898163586098864558
|
128
128
|
requirements: []
|
129
129
|
rubyforge_project:
|
130
130
|
rubygems_version: 1.8.25
|