service_objects 0.1.0 → 1.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.
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,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe "$ service new", :sandbox, :capture do
4
-
5
- let(:argv) { %w(foo) }
6
-
7
- before { try_in_sandbox { `service new #{ argv.join(" ") }` } }
8
-
9
- it "runs a scaffolder" do
10
- %w(
11
- app/services/foo.rb
12
- config/locales/services/foo/en.yml
13
- config/locales/services/foo/ru.yml
14
- spec/tests/services/foo_spec.rb
15
- ).each { |file| expect(file).to be_present_in_sandbox }
16
- end
17
-
18
- end # describe $ service new
@@ -1,179 +0,0 @@
1
- # encoding: utf-8
2
- require "service_objects/cli"
3
-
4
- describe ServiceObjects::CLI, :sandbox, :capture do
5
-
6
- subject { try_in_sandbox { described_class.start options } }
7
-
8
- shared_examples "adding an object" do
9
-
10
- let(:file) { "app/#{ folder }/foo.rb" }
11
- let(:content) { read_in_sandbox(file) }
12
-
13
- it "[creates file]" do
14
- expect(file).to be_present_in_sandbox
15
- end
16
-
17
- it "[declares a service]" do
18
- expect(content).to include "class Foo < ServiceObjects::Base"
19
- end
20
-
21
- end # examples
22
-
23
- shared_examples "adding a specification" do
24
-
25
- it "[creates file]" do
26
- expect("spec/tests/#{ folder }/foo_spec.rb").to be_present_in_sandbox
27
- end
28
-
29
- end # examples
30
-
31
- shared_examples "adding translations" do |locales|
32
-
33
- it "[creates files]" do
34
- locales.each do |locale|
35
- file = "config/locales/#{ folder }/foo/#{ locale }.yml"
36
- expect(file).to be_present_in_sandbox
37
- end
38
- end
39
-
40
- it "[adds content]" do
41
- locales.each do |locale|
42
- file = "config/locales/#{ folder }/foo/#{ locale }.yml"
43
- content = read_in_sandbox(file)
44
-
45
- expect(content).to include("# #{ locale }:")
46
- expect(content).to include("#{ folder }/foo:")
47
- end
48
- end
49
-
50
- end # examples
51
-
52
- shared_examples "using namespaces" do |modules|
53
-
54
- let(:file) { "app/#{ folder }/foo.rb" }
55
- let(:content) { read_in_sandbox(file) }
56
-
57
- it "[adds modules]" do
58
- modules.each do |item|
59
- expect(content).to include "module #{ item }"
60
- end
61
- end
62
-
63
- end # examples
64
-
65
- context "foo" do
66
-
67
- let(:options) { %w(foo) }
68
- let(:folder) { "services" }
69
-
70
- before { subject }
71
-
72
- it_behaves_like "adding an object"
73
- it_behaves_like "using namespaces", %w(Services)
74
- it_behaves_like "adding a specification"
75
- it_behaves_like "adding translations", %w(en ru)
76
-
77
- end # context
78
-
79
- context "foo -p bar baz" do
80
-
81
- let(:options) { %w(foo -p bar baz) }
82
- let(:folder) { "services" }
83
-
84
- before { subject }
85
-
86
- it_behaves_like "adding an object"
87
- it_behaves_like "using namespaces", %w(Services)
88
- it_behaves_like "adding a specification"
89
- it_behaves_like "adding translations", %w(en ru)
90
-
91
- it "uses parameters" do
92
- content = read_in_sandbox("app/services/foo.rb")
93
- expect(content).to include "allows_params :bar, :baz"
94
- end
95
-
96
- end # context
97
-
98
- context "foo -n foo:bar:baz" do
99
-
100
- let(:options) { %w(foo -n foo:bar:baz cad:cam:messages) }
101
- let(:folder) { "services" }
102
-
103
- before { subject }
104
-
105
- it_behaves_like "adding an object"
106
- it_behaves_like "using namespaces", %w(Services)
107
- it_behaves_like "adding a specification"
108
- it_behaves_like "adding translations", %w(en ru)
109
-
110
- it "uses notifications" do
111
- content = read_in_sandbox("app/services/foo.rb")
112
- expect(content).to include "publish :foo, bar, baz"
113
- end
114
-
115
- end # context
116
-
117
- context "foo -f bar baz" do
118
-
119
- let(:options) { %w(foo -f bar baz) }
120
- let(:folder) { "bar/baz" }
121
-
122
- before { subject }
123
-
124
- it_behaves_like "adding an object"
125
- it_behaves_like "using namespaces", %w(Bar Baz)
126
- it_behaves_like "adding a specification"
127
- it_behaves_like "adding translations", %w(en ru)
128
-
129
- end # context
130
-
131
- context "foo -f bar/baz" do
132
-
133
- let(:options) { %w(foo -f bar/baz) }
134
- let(:folder) { "bar/baz" }
135
-
136
- before { subject }
137
-
138
- it_behaves_like "adding an object"
139
- it_behaves_like "using namespaces", %w(Bar Baz)
140
- it_behaves_like "adding a specification"
141
- it_behaves_like "adding translations", %w(en ru)
142
-
143
- end # context
144
-
145
- context "foo -l jp ua" do
146
-
147
- let(:options) { %w(foo -l jp ua) }
148
- let(:folder) { "services" }
149
-
150
- before { subject }
151
-
152
- it_behaves_like "adding an object"
153
- it_behaves_like "using namespaces", %w(Services)
154
- it_behaves_like "adding a specification"
155
- it_behaves_like "adding translations", %w(jp ua)
156
-
157
- end # context
158
-
159
- context "foo -d get_item{AddItem}" do
160
-
161
- let(:options) { %w(foo -d get_item{AddItem}) }
162
- let(:folder) { "services" }
163
-
164
- before { subject }
165
-
166
- it_behaves_like "adding an object"
167
- it_behaves_like "using namespaces", %w(Services)
168
- it_behaves_like "adding a specification"
169
- it_behaves_like "adding translations", %w(en ru)
170
-
171
- it "uses dependencies" do
172
- content = read_in_sandbox("app/services/foo.rb")
173
- expect(content).to include "depends_on get_item, default: AddItem"
174
- expect(content).to include "GetItemListener"
175
- end
176
-
177
- end # context
178
-
179
- end # describe
@@ -1,77 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Helpers::Dependable do
4
-
5
- let(:test_class) { Class.new }
6
- subject { test_class.new }
7
-
8
- before { test_class.extend described_class }
9
-
10
- describe ".depends_on" do
11
-
12
- shared_examples "dependency declarator" do
13
-
14
- it "[defines dependency getter]" do
15
- expect(subject).to respond_to dependency_name
16
- end
17
-
18
- it "[defines dependency sette]r" do
19
- expect(subject).to respond_to("#{ dependency_name }=").with(1).argument
20
- end
21
-
22
- it "[sets default dependency]" do
23
- expect(subject.send dependency_name).to eq default_value
24
- end
25
-
26
- it "[resets dependency to default implementation]" do
27
- expect { subject.send "#{ dependency_name }=", nil }
28
- .not_to change { subject.send dependency_name }
29
- end
30
-
31
- end # examples definition
32
-
33
- context "without arguments" do
34
-
35
- it "fails" do
36
- expect { test_class.depends_on }.to raise_error
37
- end
38
-
39
- end # context
40
-
41
- context "with one argument" do
42
-
43
- let(:default_value) { ServiceObjects::NULL }
44
- let(:dependency_name) { :get_item }
45
-
46
- before { test_class.depends_on :get_item }
47
-
48
- it_behaves_like "dependency declarator"
49
-
50
- end # context
51
-
52
- context "with two arguments" do
53
-
54
- let(:default_value) { Class.new }
55
- let(:dependency_name) { :get_item }
56
-
57
- before { test_class.depends_on :get_item, default: default_value }
58
-
59
- it_behaves_like "dependency declarator"
60
-
61
- end # context
62
-
63
- context "repeatedly" do
64
-
65
- let(:default_value) { Class.new }
66
- let(:dependency_name) { :get_item }
67
-
68
- before { test_class.depends_on :get_item }
69
- before { test_class.depends_on :get_item, default: default_value }
70
-
71
- it_behaves_like "dependency declarator"
72
-
73
- end # context
74
-
75
- end # describe .depends_on
76
-
77
- end # describe ServiceObject::Helpers::Dependable
@@ -1,112 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Helpers::Exceptions do
4
-
5
- let(:service_invalid) { ServiceObjects::Invalid }
6
- let(:messages_class) { ServiceObjects::Message }
7
- let(:messages_module) { ServiceObjects::Helpers::Messages }
8
- let(:test_class) { Class.new }
9
-
10
- before { test_class.include described_class }
11
-
12
- it "includes ServiceObjects::Helpers::Messages" do
13
- expect(test_class).to include messages_module
14
- end
15
-
16
- describe "#escape" do
17
-
18
- subject { test_class.new }
19
-
20
- def escape_from_foo(with_exception: nil)
21
- subject.escape do
22
- fail with_exception if with_exception
23
- "foo"
24
- end
25
- end
26
-
27
- context "when the block raises nothing" do
28
-
29
- let(:result) { escape_from_foo }
30
-
31
- it "yields the block" do
32
- expect(result).to eq "foo"
33
- end
34
-
35
- end
36
-
37
- context "when ServiceObjects::Invalid raised by the object" do
38
-
39
- let(:exception) { service_invalid.new subject }
40
-
41
- it "re-raises Invalid error" do
42
- expect { escape_from_foo with_exception: exception }
43
- .to raise_error service_invalid
44
- end
45
-
46
- it "doesn't mutate the object" do
47
- expect { escape_from_foo with_exception: exception rescue nil }
48
- .not_to change { subject }
49
- end
50
-
51
- end
52
-
53
- context "when ServiceObjects::Invalid raised by another object" do
54
-
55
- let(:another_object) { test_class.new }
56
- let(:exception) { service_invalid.new another_object }
57
-
58
- before { another_object.add_message type: "error", text: "bar" }
59
-
60
- it "raises Invalid error" do
61
- expect { escape_from_foo with_exception: exception }
62
- .to raise_error service_invalid
63
- end
64
-
65
- it "raises the exception of its own" do
66
- begin
67
- escape_from_foo with_exception: exception
68
- rescue => err
69
- expect(err.object).to eq subject
70
- end
71
- end
72
-
73
- it "populates messages from the exception to self" do
74
- escape_from_foo with_exception: exception rescue nil
75
-
76
- expect(subject.messages).to eq another_object.messages
77
- end
78
-
79
- end
80
-
81
- context "when the block raises StandardError" do
82
-
83
- let(:exception) { StandardError.new("text") }
84
- let(:expected_message) { messages_class.new type: "error", text: "text" }
85
-
86
- it "raises a SerivceObjects::Invalid" do
87
- expect { escape_from_foo with_exception: exception }
88
- .to raise_error service_invalid
89
- end
90
-
91
- it "adds an error to the messages" do
92
- escape_from_foo with_exception: exception rescue nil
93
-
94
- expect(subject.messages).to contain_exactly expected_message
95
- end
96
-
97
- end
98
-
99
- context "when the block raises another exception" do
100
-
101
- let(:exception) { SyntaxError.new }
102
-
103
- it "re-raises the exception unchanged" do
104
- expect { escape_from_foo with_exception: exception }
105
- .to raise_error exception
106
- end
107
-
108
- end
109
-
110
- end # describe .escape
111
-
112
- end # describe ServiceObjects::Exceptions
@@ -1,64 +0,0 @@
1
- # encoding: utf-8
2
-
3
- describe ServiceObjects::Helpers::Messages do
4
-
5
- let(:messages_class) { ServiceObjects::Message }
6
- let(:test_class) { Class.new }
7
-
8
- before { ServiceObjects::Test = test_class }
9
- before { test_class.include described_class }
10
- after { ServiceObjects.send :remove_const, :Test }
11
-
12
- subject { test_class.new }
13
-
14
- describe "#translate" do
15
-
16
- let(:scope) { %w(activemodel messages models service_objects/test) }
17
- let(:traslation) { I18n.t(:text, scope: scope, name: "name") }
18
-
19
- it "translates symbols in the service's scope" do
20
- expect(subject.translate(:text, name: "name")).to eq traslation
21
- end
22
-
23
- it "doesn't translate strings" do
24
- expect(subject.translate("text")).to eq "text"
25
- end
26
-
27
- it "converts non-symbolic argument to string" do
28
- expect(subject.translate nil).to eq ""
29
- expect(subject.translate 1).to eq "1"
30
- end
31
-
32
- end # #translate
33
-
34
- describe "#messages" do
35
-
36
- it "returns an array" do
37
- expect(subject.messages).to be_kind_of Array
38
- end
39
-
40
- end # #messages
41
-
42
- describe "#add_message" do
43
-
44
- let(:message) { subject.messages.first }
45
-
46
- it "adds a new message to the #messages" do
47
- subject.add_message text: "foo", type: "bar", priority: 5, baz: 0
48
-
49
- expect(message).to be_kind_of messages_class
50
- expect(message.type).to eq "bar"
51
- expect(message.text).to eq "foo"
52
- expect(message.priority).to eq 5.0
53
- end
54
-
55
- it "translates a symbol" do
56
- subject.add_message text: :foo, type: :bar, priority: 5, baz: 0
57
-
58
- translation = subject.translate :foo, baz: 0
59
- expect(message.text).to eq translation
60
- end
61
-
62
- end # #add_message
63
-
64
- end # ServiceObjects::Helpers::Messages