hexx 0.1.1 → 1.0.0

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: 328ff58d93538e10c8f80618deaf8c4e640f8ecf
4
- data.tar.gz: 6b3957ae87b7f0dd01da5f5213acf9567df9e076
3
+ metadata.gz: 012517b45eeaa322d1d46183685969bb38abb240
4
+ data.tar.gz: 793d572f5092196bb9cc9651a7479fa21237ea4c
5
5
  SHA512:
6
- metadata.gz: 24bedd30a43978ea114d5acff719bdb8416ff1ba0cdacfe75ca722163d67e4560dcfab4841a2965227626f182c15c4d0973a33efb87678bd35eb74e29772c0ba
7
- data.tar.gz: 194270afa308dfdae9648aec04055b8ae99d539fc203df8efe83cd000662cc86a631cf353a73b03c93de9c3fb447c0d3881d0712495e6f50b89bfa289b8951c3
6
+ metadata.gz: 3286ecf42c146eaa615c104ad13caa867cb722de3b06e068e231d7bb131a1494c4daa24a4353c4d410b03221b68f9b1394d539d4d9fadc96de8dd3ef098a3d7b
7
+ data.tar.gz: 9f3e70de2e2ea1df43ca3d8d3e1d65f0c753ea77646bdf4bbc9944a4fd0c9ed0e336d08e307d5beb41a9b5e54900e77eae563f27d3049d7a8b3cc2c2deeebc4d
@@ -9,8 +9,10 @@ module Hexx
9
9
  #
10
10
  class NotFoundError < RuntimeError
11
11
 
12
- def message
13
- "Not found: #{ errors.inspect }"
12
+ private
13
+
14
+ def default
15
+ "Not found"
14
16
  end
15
17
  end
16
18
  end
@@ -7,18 +7,21 @@ module Hexx
7
7
  #
8
8
  class RuntimeError < ::RuntimeError
9
9
 
10
- attr_reader :object
10
+ attr_reader :messages
11
11
 
12
12
  def initialize(object)
13
- @object = object
13
+ fail ArgumentError unless object.is_a? UseCase
14
+ @messages ||= object.errors.messages.values
14
15
  end
15
16
 
16
- def errors
17
- object.errors if object
17
+ def message
18
+ @message ||= "#{ default }: #{ messages.join("; ") }"
18
19
  end
19
20
 
20
- def message
21
- "Runtime error: #{ errors.inspect }"
21
+ private
22
+
23
+ def default
24
+ "Runtime error"
22
25
  end
23
26
  end
24
27
  end
@@ -9,8 +9,10 @@ module Hexx
9
9
  #
10
10
  class UseCaseInvalid < RuntimeError
11
11
 
12
- def message
13
- "Invalid use case: #{ errors.inspect }"
12
+ private
13
+
14
+ def default
15
+ "Use case invalid"
14
16
  end
15
17
  end
16
18
  end
data/lib/hexx/message.rb CHANGED
@@ -6,7 +6,7 @@ module Hexx
6
6
  attr_reader :type, :text
7
7
 
8
8
  def initialize(type:, text:)
9
- @type, @text = type.to_s, text.to_s
9
+ self.type, self.text = type, text
10
10
  end
11
11
 
12
12
  def type=(value)
data/lib/hexx/models.rb CHANGED
@@ -2,10 +2,7 @@ require "active_support"
2
2
 
3
3
  module Hexx
4
4
 
5
- # Module defines:
6
- #
7
- # * <tt>validate!</tt> public instance method
8
- # * +attr_coerced+ public class methods.
5
+ # Contains +attr_coerced+ public class method.
9
6
  #
10
7
  # Include the module into the Rails model:
11
8
  #
@@ -22,13 +19,6 @@ module Hexx
22
19
  module Models
23
20
  extend ActiveSupport::Concern
24
21
 
25
- # Runs validations and raises a RecordInvalid exceptions in case of
26
- # an invalid case.
27
- def validate!
28
- return true if valid?
29
- fail RecordInvalid.new self
30
- end
31
-
32
22
  # Model class helpers for attributes coercion.
33
23
  module ClassMethods
34
24
 
data/lib/hexx/use_case.rb CHANGED
@@ -224,9 +224,9 @@ module Hexx
224
224
  def run
225
225
  run!
226
226
  rescue Hexx::NotFoundError => error
227
- finish_with :not_found, error.errors.values
227
+ finish_with :not_found, error.messages
228
228
  rescue Hexx::RuntimeError => error
229
- finish_with :error, error.errors.values
229
+ finish_with :error, error.messages
230
230
  rescue StandardError => error
231
231
  finish_with :error, [error.message]
232
232
  end
data/lib/hexx/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # Core classes to follow hexagonal domain architecture.
2
2
  module Hexx
3
- VERSION = "0.1.1"
3
+ VERSION = "1.0.0"
4
4
  end
@@ -11,7 +11,11 @@ module Hexx
11
11
  end
12
12
 
13
13
  it "has customized message" do
14
- expect(subject).to have_error_message "Not found"
14
+ use_case = Hexx::UseCase.new
15
+ allow(use_case)
16
+ .to receive_message_chain(:errors, :messages, :values)
17
+ .and_return %w(one two)
18
+ expect(subject.new(use_case).message).to eq "Not found: one; two"
15
19
  end
16
20
  end
17
21
  end
@@ -4,51 +4,39 @@ require "support/exception_matchers"
4
4
  module Hexx
5
5
  describe RuntimeError do
6
6
 
7
- subject { RuntimeError }
8
- let!(:object) { double "object" }
9
- let!(:errors) { double "errors" }
10
- before { allow(object).to receive(:errors) { errors } }
11
-
12
- let!(:error) { subject.new object }
7
+ subject { RuntimeError }
8
+ let(:use_case) { Hexx::UseCase.new }
13
9
 
14
10
  it "inherits core RuntimeError" do
15
11
  expect(subject).to inherit ::RuntimeError
16
12
  end
17
13
 
18
- it "has customized message" do
19
- expect(subject).to have_error_message "Runtime error"
20
- end
21
-
22
14
  describe ".new" do
23
15
 
24
- it "initializes the object attribute" do
25
- expect(subject).to respond_to(:new).with(1).argument
16
+ it "requires Hexx::UseCase as an argument" do
17
+ expect { subject.new use_case }.not_to raise_error
18
+ expect { subject.new "" }.to raise_error
19
+ expect { subject.new }.to raise_error
26
20
  end
27
21
  end
28
22
 
29
- describe "#object" do
30
-
31
- it "is public" do
32
- expect(error).to respond_to :object
33
- end
23
+ describe "#messages" do
34
24
 
35
- it "initializes the #object attribute" do
36
- expect(error.object).to eq object
25
+ it "returns use case's error messages" do
26
+ allow(use_case)
27
+ .to receive_message_chain(:errors, :messages, :values)
28
+ .and_return :messages
29
+ expect(subject.new(use_case).messages).to eq :messages
37
30
  end
38
31
  end
39
32
 
40
- describe "#errors" do
41
-
42
- it "is public" do
43
- expect(error).to respond_to :errors
44
- end
33
+ describe "#message" do
45
34
 
46
- it "delegated to object" do
47
- expect(error.errors).to eq errors
48
- end
35
+ let!(:error) { subject.new use_case }
36
+ before { allow(error).to receive(:messages).and_return %w(one two) }
49
37
 
50
- it "set to nil unless object is set" do
51
- expect(subject.new(nil).errors).to be_nil
38
+ it "returns a proper text" do
39
+ expect(error.message).to eq "Runtime error: one; two"
52
40
  end
53
41
  end
54
42
  end
@@ -11,7 +11,11 @@ module Hexx
11
11
  end
12
12
 
13
13
  it "has customized message" do
14
- expect(subject).to have_error_message "Invalid use case"
14
+ use_case = Hexx::UseCase.new
15
+ allow(use_case)
16
+ .to receive_message_chain(:errors, :messages, :values)
17
+ .and_return %w(one two)
18
+ expect(subject.new(use_case).message).to eq "Use case invalid: one; two"
15
19
  end
16
20
  end
17
21
  end
@@ -29,34 +29,6 @@ module Hexx
29
29
  # Run tests
30
30
  # ==========================================================================
31
31
 
32
- describe "#validate!" do
33
-
34
- it "is public" do
35
- expect(object).to respond_to(:validate!).with(0).arguments
36
- end
37
-
38
- context "with valid object" do
39
-
40
- before { allow(object).to receive(:valid?) { true } }
41
-
42
- it "returns true" do
43
- expect(object.validate!).to be_truthy
44
- end
45
- end
46
-
47
- context "with invalid object" do
48
-
49
- before { allow(object).to receive(:valid?) { false } }
50
-
51
- it "fails with Hexx::RecordInvalid" do
52
- expect { object.validate! }.to raise_error do |error|
53
- expect(error).to be_kind_of Hexx::RecordInvalid
54
- expect(error.object).to eq object
55
- end
56
- end
57
- end
58
- end
59
-
60
32
  describe ".attr_coerced" do
61
33
 
62
34
  before { subject.send :attr_coerced, :name, type: TestAttribute }
@@ -132,11 +132,8 @@ module Hexx
132
132
  describe "#run" do
133
133
 
134
134
  let!(:use_case) { subject.new }
135
- let!(:object) { double "entity" }
136
- let!(:errors) { { base: :invalid } }
137
135
  let!(:listener) { double "listener" }
138
136
 
139
- before { allow(object).to receive(:errors) { errors } }
140
137
  before { use_case.subscribe listener }
141
138
 
142
139
  it "calls the #run!" do
@@ -157,7 +154,7 @@ module Hexx
157
154
  context "if the #run! raises NotFoundError" do
158
155
 
159
156
  before do
160
- allow(use_case).to receive(:run!) { fail NotFoundError.new object }
157
+ allow(use_case).to receive(:run!) { fail NotFoundError.new use_case }
161
158
  end
162
159
 
163
160
  it "doesn't raise" do
@@ -165,10 +162,7 @@ module Hexx
165
162
  end
166
163
 
167
164
  it "publishes the :not_found notification" do
168
- expect(listener).to receive(:not_found) do |messages|
169
- expect(messages.map(&:type)).to eq ["error"]
170
- expect(messages.map(&:text)).to eq ["invalid"]
171
- end
165
+ expect(listener).to receive(:not_found)
172
166
  use_case.run
173
167
  end
174
168
 
@@ -180,7 +174,7 @@ module Hexx
180
174
  context "if the #run! raises another Hexx::Runtime error" do
181
175
 
182
176
  before do
183
- allow(use_case).to receive(:run!) { fail RuntimeError.new object }
177
+ allow(use_case).to receive(:run!) { fail RuntimeError.new use_case }
184
178
  end
185
179
 
186
180
  it "doesn't raise" do
@@ -188,10 +182,7 @@ module Hexx
188
182
  end
189
183
 
190
184
  it "publishes the :error notification" do
191
- expect(listener).to receive(:error) do |messages|
192
- expect(messages.map(&:type)).to eq ["error"]
193
- expect(messages.map(&:text)).to eq ["invalid"]
194
- end
185
+ expect(listener).to receive(:error)
195
186
  use_case.run
196
187
  end
197
188
 
@@ -5,16 +5,3 @@ RSpec::Matchers.define :inherit do |ancestor|
5
5
  expect(subject.ancestors).to be_include ancestor
6
6
  end
7
7
  end
8
-
9
- RSpec::Matchers.define :have_error_message do |header|
10
- match do |subject|
11
- object = double "object"
12
- errors = double "errors"
13
- allow(object).to receive(:errors) { errors }
14
- begin
15
- fail subject.new(object)
16
- rescue => error
17
- expect(error.message).to eq "#{ header }: #{ errors.inspect }"
18
- end
19
- end
20
- 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: 0.1.1
4
+ version: 1.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-10-13 00:00:00.000000000 Z
11
+ date: 2014-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -210,7 +210,6 @@ files:
210
210
  - lib/generators/use_case/use_case.rb
211
211
  - lib/hexx.rb
212
212
  - lib/hexx/exceptions/not_found_error.rb
213
- - lib/hexx/exceptions/record_invalid.rb
214
213
  - lib/hexx/exceptions/runtime_error.rb
215
214
  - lib/hexx/exceptions/use_case_invalid.rb
216
215
  - lib/hexx/message.rb
@@ -219,7 +218,6 @@ files:
219
218
  - lib/hexx/use_case.rb
220
219
  - lib/hexx/version.rb
221
220
  - spec/hexx/exceptions/not_found_error_spec.rb
222
- - spec/hexx/exceptions/record_invalid_spec.rb
223
221
  - spec/hexx/exceptions/runtime_error_spec.rb
224
222
  - spec/hexx/exceptions/use_case_invalid_spec.rb
225
223
  - spec/hexx/message_spec.rb
@@ -262,7 +260,6 @@ test_files:
262
260
  - spec/spec_helper.rb
263
261
  - spec/hexx/message_spec.rb
264
262
  - spec/hexx/settings_spec.rb
265
- - spec/hexx/exceptions/record_invalid_spec.rb
266
263
  - spec/hexx/exceptions/not_found_error_spec.rb
267
264
  - spec/hexx/exceptions/use_case_invalid_spec.rb
268
265
  - spec/hexx/exceptions/runtime_error_spec.rb
@@ -1,16 +0,0 @@
1
- require_relative "runtime_error"
2
-
3
- module Hexx
4
-
5
- # An exception to be raised by the <tt>Hexx::Models#validate!</tt> method.
6
- #
7
- # It is expected, that the object stores error messages in its <tt>errors</tt>
8
- # collection.
9
- #
10
- class RecordInvalid < RuntimeError
11
-
12
- def message
13
- "Record invalid: #{ errors.inspect }"
14
- end
15
- end
16
- end
@@ -1,17 +0,0 @@
1
- require "spec_helper"
2
- require "support/exception_matchers"
3
-
4
- module Hexx
5
- describe RecordInvalid do
6
-
7
- subject { RecordInvalid }
8
-
9
- it "inherits RuntimeError" do
10
- expect(subject).to inherit Hexx::RuntimeError
11
- end
12
-
13
- it "has customized message" do
14
- expect(subject).to have_error_message "Record invalid"
15
- end
16
- end
17
- end