service_objects 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.metrics +1 -0
  4. data/.travis.yml +9 -1
  5. data/.yardopts +1 -1
  6. data/Gemfile +1 -1
  7. data/Guardfile +29 -8
  8. data/LICENSE +1 -1
  9. data/README.md +179 -342
  10. data/Rakefile +3 -3
  11. data/config/metrics/churn.yml +1 -1
  12. data/config/metrics/flay.yml +1 -1
  13. data/config/metrics/metric_fu.yml +1 -0
  14. data/config/metrics/rubocop.yml +4 -4
  15. data/config/metrics/simplecov.yml +1 -1
  16. data/lib/service_objects.rb +6 -9
  17. data/lib/service_objects/base.rb +190 -17
  18. data/lib/service_objects/listener.rb +21 -75
  19. data/lib/service_objects/message.rb +15 -96
  20. data/lib/service_objects/version.rb +1 -1
  21. data/service_objects.gemspec +11 -9
  22. data/spec/lib/base_spec.rb +247 -0
  23. data/spec/lib/listener_spec.rb +96 -0
  24. data/spec/lib/message_spec.rb +48 -0
  25. data/spec/spec_helper.rb +8 -6
  26. metadata +56 -93
  27. data/bin/service +0 -17
  28. data/config/metrics/pippi.yml +0 -3
  29. data/lib/service_objects/cli.rb +0 -117
  30. data/lib/service_objects/cli/locale.erb +0 -20
  31. data/lib/service_objects/cli/service.erb +0 -125
  32. data/lib/service_objects/cli/spec.erb +0 -87
  33. data/lib/service_objects/helpers.rb +0 -17
  34. data/lib/service_objects/helpers/dependable.rb +0 -63
  35. data/lib/service_objects/helpers/exceptions.rb +0 -64
  36. data/lib/service_objects/helpers/messages.rb +0 -95
  37. data/lib/service_objects/helpers/parameterized.rb +0 -85
  38. data/lib/service_objects/helpers/parameters.rb +0 -71
  39. data/lib/service_objects/helpers/validations.rb +0 -54
  40. data/lib/service_objects/invalid.rb +0 -55
  41. data/lib/service_objects/null.rb +0 -26
  42. data/lib/service_objects/parsers.rb +0 -13
  43. data/lib/service_objects/parsers/dependency.rb +0 -69
  44. data/lib/service_objects/parsers/notification.rb +0 -85
  45. data/lib/service_objects/rspec.rb +0 -75
  46. data/lib/service_objects/utils/normal_hash.rb +0 -34
  47. data/spec/tests/base_spec.rb +0 -43
  48. data/spec/tests/bin/service_spec.rb +0 -18
  49. data/spec/tests/cli_spec.rb +0 -179
  50. data/spec/tests/helpers/dependable_spec.rb +0 -77
  51. data/spec/tests/helpers/exceptions_spec.rb +0 -112
  52. data/spec/tests/helpers/messages_spec.rb +0 -64
  53. data/spec/tests/helpers/parameterized_spec.rb +0 -136
  54. data/spec/tests/helpers/parameters_spec.rb +0 -71
  55. data/spec/tests/helpers/validations_spec.rb +0 -60
  56. data/spec/tests/invalid_spec.rb +0 -69
  57. data/spec/tests/listener_spec.rb +0 -73
  58. data/spec/tests/message_spec.rb +0 -191
  59. data/spec/tests/null_spec.rb +0 -17
  60. data/spec/tests/parsers/dependency_spec.rb +0 -29
  61. data/spec/tests/parsers/notification_spec.rb +0 -84
  62. data/spec/tests/rspec_spec.rb +0 -86
  63. data/spec/tests/utils/normal_hash_spec.rb +0 -16
@@ -1,136 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Helpers::Parameterized do
4
-
5
- let(:parameters) { ServiceObjects::Helpers::Parameters }
6
- let(:test_class) { Class.new }
7
- before { test_class.extend described_class }
8
- subject { test_class.new }
9
-
10
- it "includes ServiceObjects::Helpers::Parameters" do
11
- expect(test_class).to include parameters
12
- end
13
-
14
- describe ".allows_params" do
15
-
16
- # ==========================================================================
17
- # Defines behaviours
18
- # ==========================================================================
19
-
20
- shared_examples "white list" do
21
-
22
- # 'allowed_params' taken from context
23
-
24
- let(:whitelist) { Array(allowed_params).flatten.map(&:to_sym) }
25
-
26
- it "[sets whitelist]" do
27
- subject
28
- expect(test_class.whitelist).to match_array whitelist
29
- end
30
-
31
- it "[returns whitelist]" do
32
- expect(subject).to eq test_class.whitelist
33
- end
34
-
35
- end # behaviour
36
-
37
- shared_examples "attributes creator" do
38
-
39
- # 'allowed_params' taken from context
40
-
41
- let(:whitelist) { Array(allowed_params).flatten.map(&:to_sym) }
42
- let(:object) { test_class.new options }
43
- let(:options) do
44
- whitelist.inject({}) { |a, e| a.merge(e => rand(1..10)) }
45
- end
46
-
47
- before { subject }
48
-
49
- it "[defines setters]" do
50
- whitelist.each { |name| expect(object).to respond_to name }
51
- end
52
-
53
- it "[sets default values]" do
54
- whitelist.each { |name| expect(object.send name).to eq options[name] }
55
- end
56
-
57
- it "[makes getters aliases to params]" do
58
- whitelist.each do |name|
59
- value = ("a".."z").to_a.sample
60
-
61
- expect { object.params[name] = value }
62
- .to change { object.send name }
63
- .to value
64
- end
65
- end
66
-
67
- it "[defines getters]" do
68
- whitelist.each { |name| expect(object).to respond_to "#{ name }=" }
69
- end
70
-
71
- it "[makes setters aliases to params]" do
72
- whitelist.each do |name|
73
- value = ("a".."z").to_a.sample
74
-
75
- expect { object.send "#{ name }=", value }
76
- .to change { object.params[name] }
77
- .to value
78
- end
79
- end
80
-
81
- end # behaviour
82
-
83
- # ==========================================================================
84
- # Tests behaviours
85
- # ==========================================================================
86
-
87
- context "without arguments" do
88
-
89
- let(:allowed_params) { [] }
90
- subject { test_class.allows_params allowed_params }
91
-
92
- it_behaves_like "white list"
93
-
94
- end # context
95
-
96
- context "with one symbolic argument" do
97
-
98
- let(:allowed_params) { :name }
99
- subject { test_class.allows_params allowed_params }
100
-
101
- it_behaves_like "white list"
102
- it_behaves_like "attributes creator"
103
-
104
- end # context
105
-
106
- context "with one string argument" do
107
-
108
- let(:allowed_params) { "name" }
109
- subject { test_class.allows_params allowed_params }
110
-
111
- it_behaves_like "white list"
112
- it_behaves_like "attributes creator"
113
-
114
- end # context
115
-
116
- context "with a list of arguments" do
117
-
118
- let(:allowed_params) { [:name, "code"] }
119
- subject { test_class.allows_params(*allowed_params) }
120
-
121
- it_behaves_like "white list"
122
- it_behaves_like "attributes creator"
123
-
124
- end # context
125
-
126
- context "with an array of arguments" do
127
-
128
- let(:allowed_params) { [:name, "code"] }
129
- subject { test_class.allows_params allowed_params }
130
-
131
- it_behaves_like "white list"
132
- it_behaves_like "attributes creator"
133
-
134
- end # context
135
- end
136
- end
@@ -1,71 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Helpers::Parameters do
4
-
5
- let(:test_class) { Class.new }
6
- before { test_class.include described_class }
7
- subject { test_class.new }
8
-
9
- describe "#params" do
10
-
11
- subject { test_class.new }
12
-
13
- it "returns a hash" do
14
- expect(subject.params).to be_kind_of Hash
15
- end
16
-
17
- end # describe #params
18
-
19
- describe ".whitelist" do
20
-
21
- it "is defined" do
22
- expect(test_class).to respond_to :whitelist
23
- expect { test_class.instance_eval "@whitelist = [:foo]" }
24
- .to change { test_class.whitelist }
25
- .to [:foo]
26
- end
27
-
28
- it "returns empty array by default" do
29
- expect(test_class.whitelist).to eq []
30
- end
31
-
32
- end # describe #whitelist
33
-
34
- describe ".new" do
35
-
36
- before { allow(test_class).to receive(:whitelist) { [:foo, :bar] } }
37
-
38
- it "allows options" do
39
- expect(test_class).to respond_to(:new).with(1).argument
40
- end
41
-
42
- it "makes params optional" do
43
- expect(test_class).to respond_to(:new).with(0).argument
44
- end
45
-
46
- it "assings parameters from options" do
47
- options = { foo: "foo" }
48
-
49
- subject = test_class.new options
50
- expect(subject.params).to eq options
51
- end
52
-
53
- it "symbolizes options keys at any level" do
54
- source_options = { "foo" => "foo", "bar" => { "bar" => "baz" } }
55
- target_options = { foo: "foo", bar: { bar: "baz" } }
56
-
57
- subject = test_class.new(source_options)
58
- expect(subject.params).to eq target_options
59
- end
60
-
61
- it "filters options" do
62
- source_options = { foo: "foo", baz: "baz" }
63
- target_options = { foo: "foo" }
64
-
65
- subject = test_class.new(source_options)
66
- expect(subject.params).to eq target_options
67
- end
68
-
69
- end # describe .new
70
-
71
- end # describe ServiceObjects::Helpers::Whitelist
@@ -1,60 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Helpers::Validations do
4
-
5
- let(:invalid) { ServiceObjects::Invalid }
6
- let(:message_class) { ServiceObjects::Message }
7
- let(:messages_module) { ServiceObjects::Helpers::Messages }
8
- let(:test_class) { Class.new }
9
- let(:validations) { ActiveModel::Validations }
10
-
11
- before { ServiceObjects::Test = test_class }
12
- before { test_class.include described_class }
13
- after { ServiceObjects.send :remove_const, :Test }
14
-
15
- it "includes ActiveModel::Validations" do
16
- expect(test_class).to include validations
17
- end
18
-
19
- it "includes ServiceObjects::Helpers::Messages" do
20
- expect(test_class).to include messages_module
21
- end
22
-
23
- describe "#validate!" do
24
-
25
- subject { test_class.new }
26
-
27
- context "when the service is valid" do
28
-
29
- it "passes" do
30
- expect { subject.validate! }.not_to raise_error
31
- end
32
-
33
- end
34
-
35
- context "when the service is invalid" do
36
-
37
- before { subject.errors.add :base, :invalid }
38
- before { allow(subject).to receive(:valid?) { false } }
39
-
40
- it "fails" do
41
- expect { subject.validate! }.to raise_error invalid
42
- end
43
-
44
- it "populates #messages from errors" do
45
- begin
46
- subject.validate!
47
- rescue => err
48
- message = subject.messages.first
49
- end
50
-
51
- expect(message).to be_kind_of message_class
52
- expect(message.type).to eq "error"
53
- expect(err.messages).to contain_exactly message
54
- end
55
-
56
- end
57
-
58
- end # #validate!
59
-
60
- end # ServiceObjects::Helpers::Validations
@@ -1,69 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Invalid do
4
-
5
- let(:messages) { double :messages }
6
- let(:object) { double :object, messages: messages }
7
-
8
- subject { described_class.new object }
9
-
10
- describe ".new" do
11
-
12
- context "without arguments" do
13
-
14
- subject { described_class.new }
15
-
16
- it "fails with ArgumentError" do
17
- expect { subject }.to raise_error ArgumentError
18
- end
19
- end
20
-
21
- context "with an object that doesn't responds to :messages" do
22
-
23
- let(:object) { double }
24
- subject { described_class.new object }
25
-
26
- it "fails with TypeError" do
27
- expect { subject }.to raise_error TypeError
28
- end
29
-
30
- it "returns a proper message" do
31
- begin
32
- subject
33
- rescue => err
34
- expect(err.message)
35
- .to eq "#{ object.inspect } doesn't respond to #messages"
36
- end
37
- end
38
- end
39
-
40
- context "with an object that responds to :messages" do
41
-
42
- it "doesn't fail" do
43
- expect { subject }.not_to raise_error
44
- end
45
-
46
- it "initializes a RuntimeError" do
47
- expect(subject).to be_kind_of RuntimeError
48
- end
49
- end
50
- end
51
-
52
- describe "#object" do
53
-
54
- it "is initialized" do
55
- expect(subject.object).to eq object
56
- end
57
- end
58
-
59
- describe "#messages" do
60
-
61
- it "returns an array" do
62
- expect(subject.messages).to be_kind_of Array
63
- end
64
-
65
- it "is delegated to the #object" do
66
- expect(subject.messages).to contain_exactly object.messages
67
- end
68
- end
69
- end
@@ -1,73 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Listener do
4
-
5
- let(:object) { double :object, foo: nil }
6
- subject { described_class.new object }
7
-
8
- describe ".new" do
9
-
10
- it "provides a decorator" do
11
- expect(subject).to respond_to :foo
12
- expect(object).to receive(:foo)
13
- subject.foo
14
- end
15
-
16
- end # .new
17
-
18
- describe "#otherwise" do
19
-
20
- it "is defined" do
21
- expect(subject).to respond_to :otherwise
22
- end
23
-
24
- end # #otherwise
25
-
26
- describe "#finalize" do
27
-
28
- before { subject.singleton_class.send(:define_method, :on_success) { nil } }
29
-
30
- shared_examples "calling #otherwise" do
31
- after { subject.finalize }
32
- it { is_expected.to receive :otherwise }
33
- end
34
-
35
- shared_examples "skipping #otherwise" do
36
- after { subject.finalize }
37
- it { is_expected.not_to receive :otherwise }
38
- end
39
-
40
- shared_examples "mutating" do
41
- it "returns self" do
42
- expect(subject.finalize).to eq subject
43
- end
44
- end
45
-
46
- context "when no callbacks has been checked" do
47
-
48
- it_behaves_like "calling #otherwise"
49
- it_behaves_like "mutating"
50
-
51
- end # context
52
-
53
- context "when undefined callback has been checked" do
54
-
55
- before { subject.respond_to? :on_error }
56
-
57
- it_behaves_like "calling #otherwise"
58
- it_behaves_like "mutating"
59
-
60
- end # context
61
-
62
- context "when defined callback has been checked" do
63
-
64
- before { subject.respond_to? :on_success }
65
-
66
- it_behaves_like "skipping #otherwise"
67
- it_behaves_like "mutating"
68
-
69
- end # context
70
-
71
- end # #finalize
72
-
73
- end # ServiceObjects::Listener
@@ -1,191 +0,0 @@
1
- # encoding: utf-8
2
- require "json"
3
-
4
- describe ServiceObjects::Message do
5
-
6
- subject { described_class.new type: :foo, text: :bar }
7
-
8
- it "is comparable" do
9
- expect(subject).to be_kind_of ::Comparable
10
- end
11
-
12
- describe ".new" do
13
-
14
- it "requires the type" do
15
- expect { described_class.new text: :bar }.to raise_error ArgumentError
16
- end
17
-
18
- it "requires the text" do
19
- expect { described_class.new type: :foo }.to raise_error ArgumentError
20
- end
21
-
22
- it "freezes the object" do
23
- expect(subject).to be_frozen
24
- end
25
-
26
- end # .new
27
-
28
- describe "#type" do
29
-
30
- it "is stringified" do
31
- expect(subject.type).to eq "foo"
32
- end
33
-
34
- it "is frozen" do
35
- expect(subject.type).to be_frozen
36
- end
37
-
38
- end # #type
39
-
40
- describe "#text" do
41
-
42
- it "is stringified" do
43
- expect(subject.text).to eq "bar"
44
- end
45
-
46
- it "is frozen" do
47
- expect(subject.text).to be_frozen
48
- end
49
-
50
- end # #text
51
-
52
- describe "#priority" do
53
-
54
- it "is float" do
55
- expect(subject.priority).to be_kind_of Float
56
- end
57
-
58
- it "is customizable" do
59
- subject = described_class.new type: "foo", text: "bar", priority: 1.5
60
- expect(subject.priority).to eq 1.5
61
- end
62
-
63
- it "is set by default to 0.0 for non-errors" do
64
- subject = described_class.new type: "foo", text: "bar"
65
- expect(subject.priority).to eq 0.0
66
- end
67
-
68
- it "is set by default to -1.0 for errors" do
69
- subject = described_class.new type: "error", text: "bar"
70
- expect(subject.priority).to eq(-1.0)
71
- end
72
-
73
- end # #priority
74
-
75
- describe "#inspect" do
76
-
77
- it "returns text, type and priority of the message" do
78
- expect(subject.inspect)
79
- .to eq %W(
80
- #<ServiceObjects::Message:#{ subject.object_id }
81
- type=\"#{ subject.type }\"
82
- text=\"#{ subject.text }\"
83
- priority=#{ subject.priority }>
84
- ).join(" ")
85
- end
86
-
87
- end # #inspect
88
-
89
- describe "#==" do
90
-
91
- it "treats messages with the same type, text and priority as equal" do
92
- a = described_class.new type: "foo", text: "bar"
93
- b = described_class.new type: "foo", text: "bar"
94
- expect(a == b).to be_truthy
95
- end
96
-
97
- it "treats messages with different types as different" do
98
- a = described_class.new type: "bar", text: "foo"
99
- b = described_class.new type: "baz", text: "foo"
100
- expect(a == b).to be_falsey
101
- end
102
-
103
- it "treats messages with different texts as different" do
104
- a = described_class.new type: "foo", text: "bar"
105
- b = described_class.new type: "foo", text: "baz"
106
- expect(a == b).to be_falsey
107
- end
108
-
109
- it "treats messages with different priorities as different" do
110
- a = described_class.new type: "foo", text: "bar", priority: 1
111
- b = described_class.new type: "foo", text: "bar", priority: 1.1
112
- expect(a == b).to be_falsey
113
- end
114
-
115
- it "treats message as different from non-message" do
116
- a = described_class.new type: "foo", text: "bar"
117
- b = Struct.new(:type, :text).new("foo", "bar")
118
- expect(a == b).to be_falsey
119
- end
120
-
121
- end # #==
122
-
123
- describe "#<=>" do
124
-
125
- it "orders messages by priorities" do
126
- a = described_class.new type: "foo", text: "bar", priority: 0
127
- b = described_class.new type: "foo", text: "bar", priority: 0.1
128
- expect(a <=> b).to eq(-1)
129
- expect(b <=> a).to eq(1)
130
- end
131
-
132
- it "orders messages by type" do
133
- a = described_class.new type: "bar", text: "foo"
134
- b = described_class.new type: "baz", text: "foo"
135
- expect(a <=> b).to eq(-1)
136
- expect(b <=> a).to eq(1)
137
- end
138
-
139
- it "orders messages by text" do
140
- a = described_class.new type: "foo", text: "bar"
141
- b = described_class.new type: "foo", text: "baz"
142
- expect(a <=> b).to eq(-1)
143
- expect(b <=> a).to eq(1)
144
- end
145
-
146
- it "orders messages at first by priorities" do
147
- a = described_class.new type: "bar", text: "bar", priority: 0
148
- b = described_class.new type: "baz", text: "baz", priority: 1
149
- expect(a <=> b).to eq(-1)
150
- expect(b <=> a).to eq(1)
151
- end
152
-
153
- it "orders messages at second by types" do
154
- a = described_class.new type: "bar", text: "bar"
155
- b = described_class.new type: "baz", text: "baz"
156
- expect(a <=> b).to eq(-1)
157
- expect(b <=> a).to eq(1)
158
- end
159
-
160
- it "treats messages with the same type, text and priority as equal" do
161
- a = described_class.new type: "foo", text: "bar"
162
- b = described_class.new type: "foo", text: "bar"
163
- expect(a <=> b).to eq 0
164
- expect(b <=> a).to eq 0
165
- end
166
-
167
- it "returns nil if other value is not a message" do
168
- expect(subject <=> 1).to be_nil
169
- end
170
-
171
- end # #<=>
172
-
173
- describe "#to_h" do
174
-
175
- it "serializes type and text" do
176
- hash = { type: subject.type, text: subject.text }
177
- expect(subject.to_h).to eq hash
178
- end
179
-
180
- end # #to_h
181
-
182
- describe "#to_json" do
183
-
184
- it "serializes type and text" do
185
- hash = { type: subject.type, text: subject.text }
186
- expect(subject.to_json).to eq hash.to_json
187
- end
188
-
189
- end # #to_json
190
-
191
- end # ServiceObjects::Message