zertico 2.0.0.alpha.1 → 2.0.0.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 612a9f684881747d1a85ca57c61e15ee06e2b9ca
4
- data.tar.gz: 09f94e32870ed318a59852e560ede7816026c4ec
3
+ metadata.gz: bf85d369e45deaff1313840e328a09daa12e988b
4
+ data.tar.gz: b9f61dc7c92ce62866353d0ef3ecb9956d224928
5
5
  SHA512:
6
- metadata.gz: 9edf88ad4dbe5c8a8c48ec01e2b02b776c62a35345e60444ea8adba06b7278be112ef2c8b8b629641263ccca771000c61a2d710288c2fe5feb9981aac2117655
7
- data.tar.gz: a07da191a75726a209a6cc15bb0eecb51caa7dca8a16ccff860eeb4805f1f229f6b0681abb4c043a987b10c58b950e21218fb10ab0371fd758596fe0ba953404
6
+ metadata.gz: 41fe10373503f628f382ce7c68c73a41ce98a3860c44942c329b856162b8d1e24c481b251a3904fcf72caf96a55b83ab3df8efbee61724fa67c781ff6acbb8fe
7
+ data.tar.gz: 17e58d0959b8a3191f9e6f4df0d990f0490bc58473314608a2caeee6e75996e80889cfa1e888879503c35ac601f9ea327847a79f9c6f00f59f1b9eff4ca8b660
@@ -5,12 +5,8 @@ module Zertico
5
5
  attr_reader :service
6
6
 
7
7
  def initialize
8
- @service = Zertico::Service.new
9
- custom_service_name = "::#{self.class.name.gsub('Controller', 'Service')}"
10
- @service = custom_service_name.constantize.new if defined?(custom_service_name)
11
- self.class.responder = Zertico::Responder
12
- custom_responder_name = "::#{self.class.name.gsub('Controller', 'Responder')}"
13
- self.class.responder = custom_responder_name.constantize if defined?(custom_responder_name)
8
+ initialize_service
9
+ set_responder
14
10
  super
15
11
  end
16
12
 
@@ -50,5 +46,19 @@ module Zertico
50
46
  instance_variable_set("@#{service.interface_name}", service.destroy(params))
51
47
  respond_with(instance_variable_get("@#{service.interface_name}"), responder.destroy_options(self))
52
48
  end
49
+
50
+ private
51
+
52
+ def initialize_service
53
+ @service = "::#{self.class.name.gsub('Controller', 'Service')}".constantize.new
54
+ rescue NameError
55
+ @service = Zertico::Service.new
56
+ end
57
+
58
+ def set_responder
59
+ self.class.responder = "::#{self.class.name.gsub('Controller', 'Responder')}".constantize
60
+ rescue NameError
61
+ self.class.responder = Zertico::Responder
62
+ end
53
63
  end
54
64
  end
@@ -32,29 +32,6 @@ module Zertico
32
32
  def resource_source
33
33
  self.class.resource_source || interface_class
34
34
  end
35
-
36
- def interface_id
37
- return self.class.interface_id if self.class.interface_id
38
- if self.class.name.chomp('Controller').split('::').size > 1
39
- "#{interface_name.gsub('/', '_')}_id"
40
- else
41
- 'id'
42
- end
43
- rescue NameError
44
- 'id'
45
- end
46
-
47
- def interface_name
48
- return self.class.interface_name if self.class.interface_name
49
- interface_class.name.singularize.underscore
50
- end
51
-
52
- def interface_class
53
- return self.class.interface_class if self.class.interface_class
54
- self.class.name.chomp('Service').singularize.constantize
55
- rescue NameError
56
- self.class.name.chomp('Service').split('::').last.singularize.constantize
57
- end
58
35
  end
59
36
  end
60
37
  end
@@ -1,9 +1,38 @@
1
1
  module Zertico
2
2
  class Service
3
+ attr_reader :interface_class, :interface_name, :interface_id
4
+
3
5
  autoload :ClassMethods, 'zertico/service/class_methods'
4
6
  autoload :InstanceMethods, 'zertico/service/instance_methods'
5
7
 
6
8
  extend ClassMethods
7
9
  include InstanceMethods
10
+
11
+ def initialize(options = {})
12
+ @interface_class = options.fetch(:interface_class, self.class.interface_class)
13
+ @interface_class ||= custom_interface_class
14
+ @interface_name = options.fetch(:interface_name, self.class.interface_name)
15
+ @interface_name ||= interface_class.name.singularize.underscore
16
+ @interface_id = options.fetch(:interface_id, self.class.interface_id)
17
+ @interface_id ||= custom_interface_id
18
+ end
19
+
20
+ private
21
+
22
+ def custom_interface_class
23
+ self.class.name.chomp('Service').singularize.constantize
24
+ rescue NameError
25
+ self.class.name.chomp('Service').split('::').last.singularize.constantize
26
+ end
27
+
28
+ def custom_interface_id
29
+ if self.class.name.chomp('Controller').split('::').size > 1
30
+ "#{interface_name.gsub('/', '_')}_id"
31
+ else
32
+ 'id'
33
+ end
34
+ rescue NameError
35
+ 'id'
36
+ end
8
37
  end
9
38
  end
@@ -1,3 +1,3 @@
1
1
  module Zertico
2
- VERSION = '2.0.0.alpha.1'
2
+ VERSION = '2.0.0.alpha.2'
3
3
  end
@@ -0,0 +1,2 @@
1
+ class CarsController < ApplicationController
2
+ end
@@ -0,0 +1,2 @@
1
+ class Gift
2
+ end
@@ -1,19 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Zertico::Controller do
4
- let(:controller) { UsersController.new }
4
+ let(:users_controller) { UsersController.new }
5
+ let(:cars_controller) { CarsController.new }
5
6
  let(:zertico_controller) { Zertico::Controller.new }
6
7
  let(:service) { Zertico::Service.new }
7
8
 
8
9
  context 'without a custom service' do
9
- it 'should initialize Zertico::Service' do
10
- expect(zertico_controller.service).to be_an_instance_of(Zertico::Service)
10
+ context 'on Zertico::Controller' do
11
+ it 'should initialize Zertico::Service' do
12
+ expect(zertico_controller.service).to be_an_instance_of(Zertico::Service)
13
+ end
14
+ end
15
+
16
+ context 'on a custom controller' do
17
+ it 'should initialize Zertico::Service' do
18
+ expect(cars_controller.service).to be_an_instance_of(Zertico::Service)
19
+ end
11
20
  end
12
21
  end
13
22
 
14
23
  context 'with a custom service' do
15
24
  it 'should initialize it!' do
16
- expect(controller.service).to be_an_instance_of(UsersService)
25
+ expect(users_controller.service).to be_an_instance_of(UsersService)
17
26
  end
18
27
  end
19
28
 
@@ -25,7 +34,7 @@ describe Zertico::Controller do
25
34
 
26
35
  context 'with a custom responder' do
27
36
  it 'should initialize it!' do
28
- expect(controller.responder).to be == UsersResponder
37
+ expect(users_controller.responder).to be == UsersResponder
29
38
  end
30
39
  end
31
40
  end
@@ -121,98 +121,4 @@ describe Zertico::Service do
121
121
  end
122
122
  end
123
123
  end
124
-
125
- describe '#interface_id' do
126
- context 'on a pluralized service' do
127
- it 'should return id' do
128
- expect(users_service.send(:interface_id)).to eq('id')
129
- end
130
- end
131
-
132
- context 'on a namespaced service and interface model' do
133
- it 'should return id with the model name' do
134
- expect(profile_service.send(:interface_id)).to eq('person_profile_id')
135
- end
136
- end
137
-
138
- context 'on a namespaced service and non namespaced interface model' do
139
- it 'should return id with the model name' do
140
- expect(admin_service.send(:interface_id)).to eq('user_id')
141
- end
142
- end
143
-
144
- context 'on a non namespaced service and non namespaced interface model' do
145
- it 'should return id' do
146
- expect(users_service.send(:interface_id)).to eq('id')
147
- end
148
- end
149
-
150
- context 'on a namespaced service and an undefined interface model' do
151
- it 'should return id' do
152
- expect(gifts_service.send(:interface_id)).to eq('id')
153
- end
154
- end
155
-
156
- context 'when defined on class' do
157
- before :each do
158
- gifts_service.class.instance_variable_set('@interface_id', 'abc')
159
- end
160
-
161
- it 'should return the defined value' do
162
- expect(gifts_service.send(:interface_id)).to eq('abc')
163
- end
164
- end
165
- end
166
-
167
- describe '#interface_name' do
168
- it 'should return the interface name' do
169
- expect(users_service.send(:interface_name)).to eq('user')
170
- end
171
-
172
- context 'when defined on class' do
173
- before :each do
174
- gifts_service.class.instance_variable_set('@interface_name', 'abc')
175
- end
176
-
177
- it 'should return the defined value' do
178
- expect(gifts_service.send(:interface_name)).to eq('abc')
179
- end
180
- end
181
- end
182
-
183
- describe '#interface_class' do
184
- context 'on a pluralized service' do
185
- it 'should find the interface model' do
186
- expect(users_service.send(:interface_class)).to eq(User)
187
- end
188
- end
189
-
190
- context 'on a namespaced service and interface model' do
191
- it 'should find the interface model' do
192
- expect(profile_service.send(:interface_class)).to eq(Person::Profile)
193
- end
194
- end
195
-
196
- context 'on a namespaced service and non namespaced interface model' do
197
- it 'should find the interface model' do
198
- expect(admin_service.send(:interface_class)).to eq(User)
199
- end
200
- end
201
-
202
- context 'on a non namespaced service and non namespaced interface model' do
203
- it 'should find the interface model' do
204
- expect(users_service.send(:interface_class)).to eq(User)
205
- end
206
- end
207
-
208
- context 'when defined on class' do
209
- before :each do
210
- gifts_service.class.instance_variable_set('@interface_class', User)
211
- end
212
-
213
- it 'should return the defined value' do
214
- expect(gifts_service.send(:interface_class)).to eq(User)
215
- end
216
- end
217
- end
218
124
  end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zertico::Service do
4
+ let(:admin_service) { Admin::UserService.new }
5
+ let(:gifts_service) { Person::GiftsService.new }
6
+ let(:profile_service) { Person::ProfileService.new }
7
+ let(:object) { Object.new }
8
+ let(:users_service) { UsersService.new }
9
+ let(:user) { User.new }
10
+
11
+ describe '#interface_id' do
12
+ context 'when passed to initialize' do
13
+ before :each do
14
+ @gifts_service = Person::GiftsService.new(:interface_id => 'teste')
15
+ end
16
+
17
+ it 'should return it' do
18
+ expect(@gifts_service.send(:interface_id)).to eq('teste')
19
+ end
20
+ end
21
+
22
+ context 'on a pluralized service' do
23
+ it 'should return id' do
24
+ expect(users_service.send(:interface_id)).to eq('id')
25
+ end
26
+ end
27
+
28
+ context 'on a namespaced service and interface model' do
29
+ it 'should return id with the model name' do
30
+ expect(profile_service.send(:interface_id)).to eq('person_profile_id')
31
+ end
32
+ end
33
+
34
+ context 'on a namespaced service and non namespaced interface model' do
35
+ it 'should return id with the model name' do
36
+ expect(admin_service.send(:interface_id)).to eq('user_id')
37
+ end
38
+ end
39
+
40
+ context 'on a non namespaced service and non namespaced interface model' do
41
+ it 'should return id' do
42
+ expect(users_service.send(:interface_id)).to eq('id')
43
+ end
44
+ end
45
+
46
+ context 'when defined on class' do
47
+ before :each do
48
+ Person::GiftsService.instance_variable_set('@interface_id', 'abc')
49
+ @gifts_service = Person::GiftsService.new
50
+ end
51
+
52
+ it 'should return the defined value' do
53
+ expect(@gifts_service.send(:interface_id)).to eq('abc')
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#interface_name' do
59
+ context 'when passed to initialize' do
60
+ before :each do
61
+ @gifts_service = Person::GiftsService.new(:interface_name => 'teste')
62
+ end
63
+
64
+ it 'should return it' do
65
+ expect(@gifts_service.send(:interface_name)).to eq('teste')
66
+ end
67
+ end
68
+
69
+ it 'should return the interface name' do
70
+ expect(users_service.send(:interface_name)).to eq('user')
71
+ end
72
+
73
+ context 'when defined on class' do
74
+ before :each do
75
+ Person::GiftsService.instance_variable_set('@interface_name', 'abc')
76
+ @gifts_service = Person::GiftsService.new
77
+ end
78
+
79
+ it 'should return the defined value' do
80
+ expect(@gifts_service.send(:interface_name)).to eq('abc')
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '#interface_class' do
86
+ context 'when passed to initialize' do
87
+ before :each do
88
+ @gifts_service = Person::GiftsService.new(:interface_class => User)
89
+ end
90
+
91
+ it 'should return it' do
92
+ expect(@gifts_service.send(:interface_class)).to eq(User)
93
+ end
94
+ end
95
+
96
+ context 'on a pluralized service' do
97
+ it 'should find the interface model' do
98
+ expect(users_service.send(:interface_class)).to eq(User)
99
+ end
100
+ end
101
+
102
+ context 'on a namespaced service and interface model' do
103
+ it 'should find the interface model' do
104
+ expect(profile_service.send(:interface_class)).to eq(Person::Profile)
105
+ end
106
+ end
107
+
108
+ context 'on a namespaced service and non namespaced interface model' do
109
+ it 'should find the interface model' do
110
+ expect(admin_service.send(:interface_class)).to eq(User)
111
+ end
112
+ end
113
+
114
+ context 'on a non namespaced service and non namespaced interface model' do
115
+ it 'should find the interface model' do
116
+ expect(users_service.send(:interface_class)).to eq(User)
117
+ end
118
+ end
119
+
120
+ context 'when defined on class' do
121
+ before :each do
122
+ Person::GiftsService.instance_variable_set('@interface_class', User)
123
+ @gifts_service = Person::GiftsService.new
124
+ end
125
+
126
+ it 'should return the defined value' do
127
+ expect(@gifts_service.send(:interface_class)).to eq(User)
128
+ end
129
+ end
130
+ end
131
+ end
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: 2.0.0.alpha.1
4
+ version: 2.0.0.alpha.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Henrique Lopes Ribeiro
@@ -134,6 +134,7 @@ files:
134
134
  - lib/zertico/version.rb
135
135
  - spec/fake_app/app/controllers/admin/user_controller.rb
136
136
  - spec/fake_app/app/controllers/application_controller.rb
137
+ - spec/fake_app/app/controllers/cars_controller.rb
137
138
  - spec/fake_app/app/controllers/person/gifts_controller.rb
138
139
  - spec/fake_app/app/controllers/person/profile_controller.rb
139
140
  - spec/fake_app/app/controllers/user_controller.rb
@@ -145,6 +146,7 @@ files:
145
146
  - spec/fake_app/app/interactors/create_product_interactor.rb
146
147
  - spec/fake_app/app/interactors/create_user_interactor.rb
147
148
  - spec/fake_app/app/interactors/send_welcome_email_interactor.rb
149
+ - spec/fake_app/app/interfaces/gift.rb
148
150
  - spec/fake_app/app/interfaces/invoice.rb
149
151
  - spec/fake_app/app/interfaces/person/profile.rb
150
152
  - spec/fake_app/app/interfaces/product.rb
@@ -176,6 +178,7 @@ files:
176
178
  - spec/zertico/responder_spec.rb
177
179
  - spec/zertico/service/class_methods_spec.rb
178
180
  - spec/zertico/service/instance_methods_spec.rb
181
+ - spec/zertico/service_spec.rb
179
182
  - zertico.gemspec
180
183
  homepage:
181
184
  licenses:
@@ -207,6 +210,7 @@ test_files:
207
210
  - gemfiles/Gemfile.rails4-ruby1.9.2+
208
211
  - spec/fake_app/app/controllers/admin/user_controller.rb
209
212
  - spec/fake_app/app/controllers/application_controller.rb
213
+ - spec/fake_app/app/controllers/cars_controller.rb
210
214
  - spec/fake_app/app/controllers/person/gifts_controller.rb
211
215
  - spec/fake_app/app/controllers/person/profile_controller.rb
212
216
  - spec/fake_app/app/controllers/user_controller.rb
@@ -218,6 +222,7 @@ test_files:
218
222
  - spec/fake_app/app/interactors/create_product_interactor.rb
219
223
  - spec/fake_app/app/interactors/create_user_interactor.rb
220
224
  - spec/fake_app/app/interactors/send_welcome_email_interactor.rb
225
+ - spec/fake_app/app/interfaces/gift.rb
221
226
  - spec/fake_app/app/interfaces/invoice.rb
222
227
  - spec/fake_app/app/interfaces/person/profile.rb
223
228
  - spec/fake_app/app/interfaces/product.rb
@@ -249,3 +254,4 @@ test_files:
249
254
  - spec/zertico/responder_spec.rb
250
255
  - spec/zertico/service/class_methods_spec.rb
251
256
  - spec/zertico/service/instance_methods_spec.rb
257
+ - spec/zertico/service_spec.rb