hexx 6.0.3 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
data/lib/hexx/service/invalid.rb
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
require_relative "message"
|
2
|
-
|
3
|
-
module Hexx
|
4
|
-
class Service
|
5
|
-
|
6
|
-
# The exception to be raised by invalid services.
|
7
|
-
#
|
8
|
-
# @example
|
9
|
-
# service GetItem < Hexx::Service
|
10
|
-
# allow_params :uuid
|
11
|
-
# validates :uuid, presence: true
|
12
|
-
# def run
|
13
|
-
# validate!
|
14
|
-
# end
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# service = GetItem.new
|
18
|
-
# service.run # => fails with the {Hexx::Service::Invalid}.
|
19
|
-
class Invalid < ::RuntimeError
|
20
|
-
|
21
|
-
# @!attribute [r] service
|
22
|
-
# @return [Hexx::Service] The invalid service object.
|
23
|
-
attr_reader :service
|
24
|
-
|
25
|
-
# @!scope class
|
26
|
-
# @!method new(service)
|
27
|
-
# Initializes the exception.
|
28
|
-
#
|
29
|
-
# @example
|
30
|
-
# fail Hexx::Service::Invalid.new service
|
31
|
-
#
|
32
|
-
# @param [Hexx::Service] service The invalid service.
|
33
|
-
# @raise [ArgumentError] if the argument is not a service object.
|
34
|
-
# @return [Hexx::Service::Invalid] The exception.
|
35
|
-
|
36
|
-
# @api hide
|
37
|
-
def initialize(service)
|
38
|
-
@service = service
|
39
|
-
fail ArgumentError unless self.service.is_a? Hexx::Service
|
40
|
-
end
|
41
|
-
|
42
|
-
# @!attribute [r] message
|
43
|
-
# Returns a default text message for the exception.
|
44
|
-
#
|
45
|
-
# @example
|
46
|
-
# error = Hexx::Service::Invalid.new service
|
47
|
-
# error.message # => "Service invalid: #<Hexx::Service... >"
|
48
|
-
#
|
49
|
-
# @return [String] The message.
|
50
|
-
def message
|
51
|
-
"Service invalid: #{ service.inspect }"
|
52
|
-
end
|
53
|
-
|
54
|
-
# @!attribute [r] messages
|
55
|
-
# Returns a list of error messages from the service.
|
56
|
-
#
|
57
|
-
# @example
|
58
|
-
# service.errors.add :base, "some error"
|
59
|
-
#
|
60
|
-
# error = Hexx::Service::Invalid.new service
|
61
|
-
# error.messages
|
62
|
-
# # => [#<Hexx::Service::Message @type="error" @text="some error" >]
|
63
|
-
#
|
64
|
-
# @return [Array<Hexx::Service::Message>] The list of error messages.
|
65
|
-
def messages
|
66
|
-
errors.map { |text| Message.new type: "error", text: text }
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
|
71
|
-
def errors
|
72
|
-
service.errors.messages.values.flatten
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
data/lib/hexx/service/message.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
module Hexx
|
2
|
-
class Service
|
3
|
-
|
4
|
-
# A message to be returned by services.
|
5
|
-
class Message
|
6
|
-
include Comparable
|
7
|
-
|
8
|
-
# @!attribute [r] type
|
9
|
-
# The type of the message
|
10
|
-
#
|
11
|
-
# @example
|
12
|
-
# message = Message.new type: :error, text: "message"
|
13
|
-
# message.type = "error"
|
14
|
-
#
|
15
|
-
# @return [String] The type of the message.
|
16
|
-
attr_reader :type
|
17
|
-
|
18
|
-
# @!attribute [r] text
|
19
|
-
# The text of the message
|
20
|
-
#
|
21
|
-
# @example
|
22
|
-
# message = Message.new type: :error, text: "message"
|
23
|
-
# message.text = "message"
|
24
|
-
#
|
25
|
-
# @return [String] The text of the message.
|
26
|
-
attr_reader :text
|
27
|
-
|
28
|
-
# @!scope class
|
29
|
-
# @!method new(options)
|
30
|
-
# Constructs the message with type and text.
|
31
|
-
#
|
32
|
-
# @example
|
33
|
-
# Message.new type: "success", text: "Object created."
|
34
|
-
#
|
35
|
-
# @param [Hash] options The list of the message attributes.
|
36
|
-
# @option options [String, Symbol] :type The type of the message.
|
37
|
-
# @option options [String, Symbol] :text The text of the message.
|
38
|
-
# @return [Hexx::Service::Message] The message.
|
39
|
-
|
40
|
-
# @api hide
|
41
|
-
def initialize(type:, text:)
|
42
|
-
@type, @text = type.to_s, text.to_s
|
43
|
-
end
|
44
|
-
|
45
|
-
# Distinguishes two messages by type and text.
|
46
|
-
#
|
47
|
-
# @example
|
48
|
-
# a = Message.new(type: "a", text: "a")
|
49
|
-
# b = Message.new(type: "a", text: "a")
|
50
|
-
# c = Message.new(type: "b", text: "a")
|
51
|
-
# d = Message.new(type: "a", text: "b")
|
52
|
-
#
|
53
|
-
# a == b # => true
|
54
|
-
# a == c # => false
|
55
|
-
# a == d # => false
|
56
|
-
#
|
57
|
-
# @param [Object] other The object for the comparison.
|
58
|
-
# @return [Boolean] The result of the comparison.
|
59
|
-
def ==(other)
|
60
|
-
return false unless other.is_a? self.class
|
61
|
-
[type, text] == [other.type, other.text]
|
62
|
-
end
|
63
|
-
|
64
|
-
# Compares messages by type and text.
|
65
|
-
#
|
66
|
-
# @example
|
67
|
-
# ab = Message.new(type: "a", text: "b")
|
68
|
-
# ba = Message.new(type: "b", text: "a")
|
69
|
-
# ab < ba # => true
|
70
|
-
#
|
71
|
-
# @param [Object] other The object for the comparison.
|
72
|
-
# @return [-1, 0, 1] The result of the comparison if the argument is
|
73
|
-
# comparable with the message.
|
74
|
-
# @return [nil] if the result is incomparable with the message.
|
75
|
-
def <=>(other)
|
76
|
-
fail ArgumentError unless other.is_a? self.class
|
77
|
-
[type, text] <=> [other.type, other.text]
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
module Hexx
|
4
|
-
class Service
|
5
|
-
describe Invalid do
|
6
|
-
|
7
|
-
describe ".new" do
|
8
|
-
|
9
|
-
subject { Invalid }
|
10
|
-
|
11
|
-
it "fails if no arguments given" do
|
12
|
-
expect { subject.new }.to raise_error { ArgumentError }
|
13
|
-
end
|
14
|
-
|
15
|
-
it "fails unless given argument is a service" do
|
16
|
-
expect { subject.new "wrong" }.to raise_error { ArgumentError }
|
17
|
-
end
|
18
|
-
|
19
|
-
it "initializes the #service" do
|
20
|
-
expect { subject.new Service.new }.not_to raise_error
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
describe "#messages" do
|
25
|
-
|
26
|
-
let(:service) { Service.new }
|
27
|
-
subject { Invalid.new(service).messages }
|
28
|
-
let(:message) { subject.first }
|
29
|
-
|
30
|
-
it "returns an array" do
|
31
|
-
expect(subject).to be_kind_of Array
|
32
|
-
end
|
33
|
-
|
34
|
-
it "returns error messages" do
|
35
|
-
service.errors.send :add, :base, "some error"
|
36
|
-
expected = Service::Message.new(type: "error", text: "some error")
|
37
|
-
expect(message).to eq expected
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "#message" do
|
42
|
-
|
43
|
-
let(:service) { Service.new }
|
44
|
-
subject { Invalid.new(service).message }
|
45
|
-
|
46
|
-
it "returns a proper message" do
|
47
|
-
expect(subject).to eq "Service invalid: #{ service.inspect }"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
module Hexx
|
4
|
-
class Service
|
5
|
-
describe Message do
|
6
|
-
|
7
|
-
describe ".new" do
|
8
|
-
|
9
|
-
subject { Message }
|
10
|
-
|
11
|
-
it "requires type" do
|
12
|
-
expect { subject.new text: "" }.to raise_error
|
13
|
-
end
|
14
|
-
|
15
|
-
it "requires text" do
|
16
|
-
expect { subject.new type: "" }.to raise_error
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "#type" do
|
21
|
-
|
22
|
-
subject { Message.new type: "info", text: "text" }
|
23
|
-
|
24
|
-
it "returns message type" do
|
25
|
-
expect(subject.type).to eq "info"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
describe "#text" do
|
30
|
-
|
31
|
-
subject { Message.new type: "info", text: "text" }
|
32
|
-
|
33
|
-
it "returns message text" do
|
34
|
-
expect(subject.text).to eq "text"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "#==" do
|
39
|
-
|
40
|
-
let!(:params) { { type: "info", text: "text" } }
|
41
|
-
|
42
|
-
it "returns true if type and text of two messages are the same" do
|
43
|
-
expect(Message.new params).to eq(Message.new params)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "returns false if types of two messages are different" do
|
47
|
-
expect(Message.new params)
|
48
|
-
.not_to eq(Message.new params.merge(type: "error"))
|
49
|
-
end
|
50
|
-
|
51
|
-
it "returns false if texts of two messages are different" do
|
52
|
-
expect(Message.new params)
|
53
|
-
.not_to eq(Message.new params.merge(text: "another"))
|
54
|
-
end
|
55
|
-
|
56
|
-
it "returns false when a message compares to non-message" do
|
57
|
-
expect(Message.new params).not_to eq("text")
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe "#<=>" do
|
62
|
-
|
63
|
-
let!(:aa) { Message.new type: "a", text: "a" }
|
64
|
-
let!(:ab) { Message.new type: "a", text: "b" }
|
65
|
-
let!(:ba) { Message.new type: "b", text: "a" }
|
66
|
-
|
67
|
-
it "orders messages by type" do
|
68
|
-
expect(aa < ba).to be_truthy
|
69
|
-
end
|
70
|
-
|
71
|
-
it "orders messages by text" do
|
72
|
-
expect(aa < ab).to be_truthy
|
73
|
-
end
|
74
|
-
|
75
|
-
it "orders messages first by type" do
|
76
|
-
expect(ab < ba).to be_truthy
|
77
|
-
end
|
78
|
-
|
79
|
-
it "fails when compared with non-message" do
|
80
|
-
expect { ab < "" }.to raise_error
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|