hexx 6.0.3 → 7.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/README.rdoc +63 -48
- data/lib/hexx.rb +1 -0
- data/lib/hexx/coercible.rb +2 -2
- data/lib/hexx/configurable.rb +1 -1
- data/lib/hexx/{helpers → creators}/base.rb +11 -15
- data/lib/hexx/{helpers → creators}/coercion.rb +8 -8
- data/lib/hexx/{helpers → creators}/dependency.rb +8 -8
- data/lib/hexx/{helpers → creators}/module_dependency.rb +2 -2
- data/lib/hexx/{helpers → creators}/parameter.rb +1 -1
- data/lib/hexx/dependable.rb +1 -1
- data/lib/hexx/helpers/exceptions.rb +53 -0
- data/lib/hexx/helpers/messages.rb +28 -0
- data/lib/hexx/helpers/parameters.rb +47 -0
- data/lib/hexx/helpers/validations.rb +21 -0
- data/lib/hexx/message.rb +79 -0
- data/lib/hexx/null.rb +0 -4
- data/lib/hexx/service.rb +259 -208
- data/lib/hexx/service_invalid.rb +73 -0
- data/lib/hexx/version.rb +1 -1
- data/spec/hexx/helpers/exceptions_spec.rb +96 -0
- data/spec/hexx/helpers/messages_spec.rb +56 -0
- data/spec/hexx/helpers/parameters_spec.rb +96 -0
- data/spec/hexx/helpers/validations_spec.rb +32 -0
- data/spec/hexx/message_spec.rb +83 -0
- data/spec/hexx/service_invalid_spec.rb +46 -0
- data/spec/hexx/service_spec.rb +41 -242
- data/spec/spec_helper.rb +1 -1
- data/spec/{initializers → support/initializers}/focus.rb +0 -0
- data/spec/{initializers → support/initializers}/garbage_collection.rb +0 -0
- data/spec/{initializers → support/initializers}/i18n.rb +0 -0
- data/spec/{initializers → support/initializers}/random_order.rb +0 -0
- data/spec/{initializers → support/initializers}/rspec.rb +0 -0
- data/spec/support/matchers/methods.rb +11 -0
- metadata +37 -23
- data/lib/hexx/service/invalid.rb +0 -76
- data/lib/hexx/service/message.rb +0 -81
- data/spec/hexx/service/invalid_spec.rb +0 -52
- data/spec/hexx/service/message_spec.rb +0 -85
@@ -0,0 +1,46 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Hexx
|
4
|
+
describe ServiceInvalid do
|
5
|
+
|
6
|
+
let(:service) { Service.new }
|
7
|
+
let(:error) { Hexx::Message.new(type: "error", text: "some error") }
|
8
|
+
|
9
|
+
describe ".new" do
|
10
|
+
|
11
|
+
subject { Invalid }
|
12
|
+
|
13
|
+
it "fails if no arguments given" do
|
14
|
+
expect { described_class.new }.to raise_error ArgumentError
|
15
|
+
end
|
16
|
+
|
17
|
+
it "initializes the #service" do
|
18
|
+
expect { described_class.new Service.new }.not_to raise_error
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#messages" do
|
23
|
+
|
24
|
+
subject { described_class.new(service).messages }
|
25
|
+
let(:message) { subject.first }
|
26
|
+
|
27
|
+
it "returns an array" do
|
28
|
+
expect(subject).to be_kind_of Array
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns error messages" do
|
32
|
+
service.errors.send :add, :base, error.text
|
33
|
+
expect(message).to eq error
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#message" do
|
38
|
+
|
39
|
+
subject { described_class.new(service).message }
|
40
|
+
|
41
|
+
it "returns a proper message" do
|
42
|
+
expect(subject).to eq "Service invalid: #{ service.inspect }"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/hexx/service_spec.rb
CHANGED
@@ -14,120 +14,47 @@ module Hexx
|
|
14
14
|
|
15
15
|
let!(:described_class) { Test }
|
16
16
|
|
17
|
-
it "extends Hexx::Dependable" do
|
18
|
-
expect(described_class).to be_kind_of Hexx::Dependable
|
19
|
-
end
|
20
|
-
|
21
|
-
it "includes ActiveModel::Validations" do
|
22
|
-
expect(subject).to be_kind_of ActiveModel::Validations
|
23
|
-
end
|
24
|
-
|
25
17
|
it "includes Whisper::Publisher" do
|
26
18
|
expect(subject).to be_kind_of Wisper::Publisher
|
27
19
|
end
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
it "accepts one argument" do
|
34
|
-
expect { described_class.send :allow_params, :name }
|
35
|
-
.not_to raise_error
|
36
|
-
end
|
37
|
-
|
38
|
-
it "accepts a list of arguments" do
|
39
|
-
expect { described_class.send :allow_params, :name, :type }
|
40
|
-
.not_to raise_error
|
41
|
-
end
|
42
|
-
|
43
|
-
it "accepts an array of arguments" do
|
44
|
-
expect { described_class.send :allow_params, %w(name type) }
|
45
|
-
.not_to raise_error
|
46
|
-
end
|
47
|
-
|
48
|
-
it "requires strings or symbols" do
|
49
|
-
expect { described_class.send :allow_params, 1 }
|
50
|
-
.to raise_error SyntaxError
|
51
|
-
end
|
52
|
-
|
53
|
-
it "requires non-empty values" do
|
54
|
-
expect { described_class.send :allow_params, "" }
|
55
|
-
.to raise_error ArgumentError
|
56
|
-
end
|
57
|
-
|
58
|
-
it "requires values" do
|
59
|
-
expect { described_class.send :allow_params }
|
60
|
-
.to raise_error ArgumentError
|
61
|
-
end
|
62
|
-
|
63
|
-
context "with valid argument" do
|
64
|
-
|
65
|
-
before { described_class.send :allow_params, :name }
|
66
|
-
subject do
|
67
|
-
described_class.new(name: "name", wrong: "wrong").with_callbacks
|
68
|
-
end
|
69
|
-
|
70
|
-
it "allows initialization of the parameter" do
|
71
|
-
expect(subject.params).to eq("name" => "name")
|
72
|
-
end
|
73
|
-
|
74
|
-
it "defines private getter for the parameter" do
|
75
|
-
expect(described_class.private_instance_methods)
|
76
|
-
.to be_include :name
|
77
|
-
expect { subject.params["name"] = "new" }
|
78
|
-
.to change { subject.name }.to "new"
|
79
|
-
end
|
80
|
-
|
81
|
-
it "defines private setter for the parameter" do
|
82
|
-
expect(described_class.private_instance_methods)
|
83
|
-
.to be_include :name=
|
84
|
-
expect { subject.name = "new" }
|
85
|
-
.to change { subject.params["name"] }.to "new"
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
21
|
+
it "includes helpers from Parameters" do
|
22
|
+
expect(described_class).to include Hexx::Helpers::Parameters
|
23
|
+
expect(described_class).to have_private_method :allow_params
|
24
|
+
expect(subject).to have_private_method :params
|
89
25
|
end
|
90
26
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
subject
|
96
|
-
|
97
|
-
it "stringifies params keys" do
|
98
|
-
expect(subject.params["name"]).to eq value
|
99
|
-
expect(subject.params[:name]).to be_nil
|
100
|
-
end
|
27
|
+
it "includes helpers from Messages" do
|
28
|
+
expect(described_class).to include Hexx::Helpers::Messages
|
29
|
+
expect(subject).to have_public_method :messages
|
30
|
+
expect(subject).to have_private_method :messages=
|
31
|
+
expect(subject).to have_private_method :add_message
|
32
|
+
expect(subject).to have_private_method :t
|
101
33
|
end
|
102
34
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
subject { described_class.new.with_callbacks }
|
35
|
+
it "includes helpers from Validations" do
|
36
|
+
expect(described_class).to include Hexx::Helpers::Validations
|
37
|
+
expect(subject).to have_private_method :validate!
|
38
|
+
end
|
109
39
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
end
|
40
|
+
it "includes helpers from Exceptions" do
|
41
|
+
expect(described_class).to include Hexx::Helpers::Exceptions
|
42
|
+
expect(described_class).to have_private_method :raises
|
43
|
+
expect(subject).to have_private_method :on_error
|
44
|
+
expect(subject).to have_private_method :escape
|
45
|
+
end
|
117
46
|
|
118
|
-
|
47
|
+
it "doesn't extend Hexx::Dependable by default" do
|
48
|
+
expect(described_class).not_to be_kind_of Hexx::Dependable
|
49
|
+
end
|
119
50
|
|
120
|
-
|
51
|
+
describe ".new" do
|
121
52
|
|
122
|
-
|
123
|
-
|
124
|
-
expect { subject.on_something }.not_to raise_error
|
125
|
-
end
|
53
|
+
before { described_class.send :allow_params, :name }
|
54
|
+
subject { described_class.new(name: "name", code: "code") }
|
126
55
|
|
127
|
-
|
128
|
-
|
129
|
-
expect { subject.something }.to raise_error
|
130
|
-
end
|
56
|
+
it "assigns params properly" do
|
57
|
+
expect(subject.send :params).to eq("name" => "name")
|
131
58
|
end
|
132
59
|
end
|
133
60
|
|
@@ -138,153 +65,25 @@ module Hexx
|
|
138
65
|
end
|
139
66
|
end
|
140
67
|
|
141
|
-
describe "
|
142
|
-
|
143
|
-
it "returns an array" do
|
144
|
-
expect(subject.messages).to be_kind_of Array
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
describe "instance helpers" do
|
149
|
-
|
150
|
-
let(:service) { Service.new name: "name" }
|
151
|
-
subject { service.with_callbacks }
|
68
|
+
describe "##run_service" do
|
152
69
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
it "fails when service is invalid" do
|
160
|
-
allow(service).to receive(:valid?).and_return false
|
161
|
-
expect { subject.validate! }
|
162
|
-
.to raise_error { Service::Invalid.new(service) }
|
163
|
-
end
|
70
|
+
def service_spy(result)
|
71
|
+
object = described_class.new
|
72
|
+
allow(object).to receive(:run) { object.send :publish, result }
|
73
|
+
class_spy described_class.name, new: object
|
164
74
|
end
|
165
75
|
|
166
|
-
|
167
|
-
|
168
|
-
let(:scope) { %w(activemodel messages models hexx/service) }
|
169
|
-
let(:traslation) { I18n.t(:text, scope: scope, name: "name") }
|
170
|
-
|
171
|
-
it "translates symbols in the service scope" do
|
172
|
-
expect(subject.t(:text, name: "name")).to eq traslation
|
173
|
-
end
|
174
|
-
|
175
|
-
it "doesn't translate the string" do
|
176
|
-
expect(subject.t("text")).to eq "text"
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
describe "#messages=" do
|
181
|
-
|
182
|
-
it "sets #messages" do
|
183
|
-
expect { subject.messages = ["text"] }
|
184
|
-
.to change { subject.messages }.to ["text"]
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
describe "#add_message" do
|
189
|
-
|
190
|
-
before { subject.add_message :info, :text }
|
191
|
-
let(:message) { subject.messages.first }
|
192
|
-
|
193
|
-
it "adds a new message" do
|
194
|
-
expect(message).to be_kind_of Service::Message
|
195
|
-
expect(message.type).to eq "info"
|
196
|
-
end
|
197
|
-
|
198
|
-
it "translates a symbol" do
|
199
|
-
translation = subject.t :text
|
200
|
-
expect(message.text).to eq translation
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
describe "#on_error" do
|
205
|
-
|
206
|
-
let!(:message) { Service::Message.new(type: "error", text: "error") }
|
207
|
-
|
208
|
-
it "fails with Service::Invalid" do
|
209
|
-
expect { subject.on_error [message] }
|
210
|
-
.to raise_error { Service::Invalid }
|
211
|
-
end
|
76
|
+
let(:other_service_class) { service_spy :result }
|
77
|
+
before { allow(subject).to receive(:on_service_result) }
|
212
78
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
end
|
79
|
+
it "if wrong class given it fails with TypeError" do
|
80
|
+
expect { subject.send :run_service, String, :on_string }
|
81
|
+
.to raise_error { TypeError }
|
217
82
|
end
|
218
83
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
before { subject.subscribe listener }
|
223
|
-
|
224
|
-
it "yields a block" do
|
225
|
-
value = "Hello!"
|
226
|
-
result = subject.escape { value }
|
227
|
-
expect(result).to eq value
|
228
|
-
end
|
229
|
-
|
230
|
-
context "when a block raises Service::Invalid error" do
|
231
|
-
|
232
|
-
let!(:exception) { Service::Invalid.new(Service.new) }
|
233
|
-
|
234
|
-
it "re-raises the exception" do
|
235
|
-
expect { subject.escape { fail exception } }
|
236
|
-
.to raise_error { exception }
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
context "when a block raises StandardError" do
|
241
|
-
|
242
|
-
let(:run) { subject.escape { fail "text" } }
|
243
|
-
let(:message) { Service::Message.new type: "error", text: "text" }
|
244
|
-
|
245
|
-
it "re-raises the Service::Invalid" do
|
246
|
-
expect { run }.to raise_error { Service::Invalid }
|
247
|
-
end
|
248
|
-
|
249
|
-
it "adds error to the messages" do
|
250
|
-
begin
|
251
|
-
run
|
252
|
-
rescue => err
|
253
|
-
expect(err.messages).to eq [message]
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
describe "#run_service" do
|
260
|
-
|
261
|
-
let!(:other) { double "other", subscribe: nil, run: nil }
|
262
|
-
before { allow(described_class).to receive(:new).and_return other }
|
263
|
-
|
264
|
-
it "if wrong class given it fails with TypeError" do
|
265
|
-
expect { subject.run_service String, :on_string }
|
266
|
-
.to raise_error { TypeError }
|
267
|
-
end
|
268
|
-
|
269
|
-
it "creates a service object" do
|
270
|
-
options = { "name" => "some name" }
|
271
|
-
expect(described_class).to receive(:new).with options
|
272
|
-
subject.run_service described_class, :on_service, options
|
273
|
-
end
|
274
|
-
|
275
|
-
it "subscribes self for the service notifications" do
|
276
|
-
expect(other).to receive(:subscribe) do |listener, params|
|
277
|
-
expect(listener).to eq subject.with_callbacks
|
278
|
-
expect(params).to eq(prefix: :on_service)
|
279
|
-
end
|
280
|
-
subject.run_service described_class, :on_service
|
281
|
-
end
|
282
|
-
|
283
|
-
it "runs the service object after subscriptions" do
|
284
|
-
expect(other).to receive(:subscribe).ordered
|
285
|
-
expect(other).to receive(:run).ordered
|
286
|
-
subject.run_service described_class, :on_service
|
287
|
-
end
|
84
|
+
it "receives notifications from a service object" do
|
85
|
+
subject.send :run_service, other_service_class, :on_service
|
86
|
+
expect(subject).to have_received(:on_service_result)
|
288
87
|
end
|
289
88
|
end
|
290
89
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,7 +4,7 @@ require "coveralls"
|
|
4
4
|
root = File.expand_path "../..", __FILE__
|
5
5
|
|
6
6
|
# Support files and Rspec settings
|
7
|
-
Dir[File.join(root, "spec/
|
7
|
+
Dir[File.join(root, "spec/support/**/*.rb")].each { |file| require file }
|
8
8
|
|
9
9
|
# Application files
|
10
10
|
Coveralls.wear!
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,11 @@
|
|
1
|
+
RSpec::Matchers.define :have_private_method do |name|
|
2
|
+
match do |object|
|
3
|
+
expect(object.private_methods).to include(name)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec::Matchers.define :have_public_method do |name|
|
8
|
+
match do |object|
|
9
|
+
expect(object.public_methods).to include(name)
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hexx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 7.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -151,31 +151,40 @@ files:
|
|
151
151
|
- lib/hexx.rb
|
152
152
|
- lib/hexx/coercible.rb
|
153
153
|
- lib/hexx/configurable.rb
|
154
|
+
- lib/hexx/creators/base.rb
|
155
|
+
- lib/hexx/creators/coercion.rb
|
156
|
+
- lib/hexx/creators/dependency.rb
|
157
|
+
- lib/hexx/creators/module_dependency.rb
|
158
|
+
- lib/hexx/creators/parameter.rb
|
154
159
|
- lib/hexx/dependable.rb
|
155
|
-
- lib/hexx/helpers/
|
156
|
-
- lib/hexx/helpers/
|
157
|
-
- lib/hexx/helpers/
|
158
|
-
- lib/hexx/helpers/
|
159
|
-
- lib/hexx/
|
160
|
+
- lib/hexx/helpers/exceptions.rb
|
161
|
+
- lib/hexx/helpers/messages.rb
|
162
|
+
- lib/hexx/helpers/parameters.rb
|
163
|
+
- lib/hexx/helpers/validations.rb
|
164
|
+
- lib/hexx/message.rb
|
160
165
|
- lib/hexx/null.rb
|
161
166
|
- lib/hexx/service.rb
|
162
|
-
- lib/hexx/service/invalid.rb
|
163
|
-
- lib/hexx/service/message.rb
|
164
167
|
- lib/hexx/service/with_callbacks.rb
|
168
|
+
- lib/hexx/service_invalid.rb
|
165
169
|
- lib/hexx/version.rb
|
166
170
|
- spec/hexx/coercible_spec.rb
|
167
171
|
- spec/hexx/configurable_spec.rb
|
168
172
|
- spec/hexx/dependable_spec.rb
|
173
|
+
- spec/hexx/helpers/exceptions_spec.rb
|
174
|
+
- spec/hexx/helpers/messages_spec.rb
|
175
|
+
- spec/hexx/helpers/parameters_spec.rb
|
176
|
+
- spec/hexx/helpers/validations_spec.rb
|
177
|
+
- spec/hexx/message_spec.rb
|
169
178
|
- spec/hexx/null_spec.rb
|
170
|
-
- spec/hexx/
|
171
|
-
- spec/hexx/service/message_spec.rb
|
179
|
+
- spec/hexx/service_invalid_spec.rb
|
172
180
|
- spec/hexx/service_spec.rb
|
173
|
-
- spec/initializers/focus.rb
|
174
|
-
- spec/initializers/garbage_collection.rb
|
175
|
-
- spec/initializers/i18n.rb
|
176
|
-
- spec/initializers/random_order.rb
|
177
|
-
- spec/initializers/rspec.rb
|
178
181
|
- spec/spec_helper.rb
|
182
|
+
- spec/support/initializers/focus.rb
|
183
|
+
- spec/support/initializers/garbage_collection.rb
|
184
|
+
- spec/support/initializers/i18n.rb
|
185
|
+
- spec/support/initializers/random_order.rb
|
186
|
+
- spec/support/initializers/rspec.rb
|
187
|
+
- spec/support/matchers/methods.rb
|
179
188
|
homepage: https://github.com/nepalez/hexx
|
180
189
|
licenses:
|
181
190
|
- MIT
|
@@ -203,17 +212,22 @@ summary: Service objects for Rails.
|
|
203
212
|
test_files:
|
204
213
|
- spec/spec_helper.rb
|
205
214
|
- spec/hexx/service_spec.rb
|
215
|
+
- spec/hexx/message_spec.rb
|
206
216
|
- spec/hexx/null_spec.rb
|
207
|
-
- spec/hexx/
|
208
|
-
- spec/hexx/
|
217
|
+
- spec/hexx/helpers/parameters_spec.rb
|
218
|
+
- spec/hexx/helpers/messages_spec.rb
|
219
|
+
- spec/hexx/helpers/validations_spec.rb
|
220
|
+
- spec/hexx/helpers/exceptions_spec.rb
|
221
|
+
- spec/hexx/service_invalid_spec.rb
|
209
222
|
- spec/hexx/coercible_spec.rb
|
210
223
|
- spec/hexx/dependable_spec.rb
|
211
224
|
- spec/hexx/configurable_spec.rb
|
212
|
-
- spec/
|
213
|
-
- spec/initializers/
|
214
|
-
- spec/initializers/
|
215
|
-
- spec/initializers/
|
216
|
-
- spec/initializers/
|
225
|
+
- spec/support/matchers/methods.rb
|
226
|
+
- spec/support/initializers/garbage_collection.rb
|
227
|
+
- spec/support/initializers/i18n.rb
|
228
|
+
- spec/support/initializers/focus.rb
|
229
|
+
- spec/support/initializers/random_order.rb
|
230
|
+
- spec/support/initializers/rspec.rb
|
217
231
|
- Rakefile
|
218
232
|
- ".rubocop.yml"
|
219
233
|
has_rdoc:
|