slayer 0.5.0.beta → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/release.yml +0 -1
- data/.github/workflows/test.yml +1 -1
- data/README.md +61 -0
- data/lib/slayer/command.rb +14 -6
- data/lib/slayer/compat/compat_040.rb +0 -12
- data/lib/slayer/compat/minitest_compat_040.rb +12 -0
- data/lib/slayer/compat/rspec_compat_040.rb +8 -0
- data/lib/slayer/rspec.rb +23 -0
- data/lib/slayer/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8afcb532ebe21d444e89baf763a76b20cf4ab19ad1bf51d45bfbd29042f3ec16
|
4
|
+
data.tar.gz: 5f77a3a44acf2cd3a2888f38e67963b22866c5c81a44a628be1c28128d128812
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f383e130f4d8bef5d42f9de29db7890f585c8116f3f6ea093cb83fc3c8b29b608ee82b16e881130527adedaf36761e216985d8b13208abac93bfef74739d53c7
|
7
|
+
data.tar.gz: 2b96b3b9473ad470033138e8f201a6fc5cdc893c2724fa4e0de1a0c7e820af93256d91569fe67f2a36dc66191f0956cf5ba45c11394e6d7778d015ee2633e7cb
|
data/.github/workflows/test.yml
CHANGED
data/README.md
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
|
7
7
|
Slayer is intended to operate as a minimal service layer for your ruby application. To achieve this, Slayer provides base classes for business logic.
|
8
8
|
|
9
|
+
**Slayer is still under development, and not yet ready for production use. We are targetting a stable API with the 0.4.0 launch, so expect breaking changes until then.**
|
10
|
+
|
9
11
|
## Application Structure
|
10
12
|
|
11
13
|
Slayer provides 2 base classes for organizing your business logic: `Forms` and `Commands`. These each have a distinct role in your application's structure.
|
@@ -259,6 +261,39 @@ RSpec.describe RSpecCommand do
|
|
259
261
|
end
|
260
262
|
```
|
261
263
|
|
264
|
+
#### Stubbing Command Results
|
265
|
+
|
266
|
+
The RSpec helpers provide two utility functions for use in your tests which should simplify testing commands with stubbed results. This can be useful when you want
|
267
|
+
test a Rails controller, and your command is already tested separately. In this case, you only really care about the logic in your matching blocks --- not in the command itself.
|
268
|
+
|
269
|
+
Put another way: this is useful when you want to test the success or failure conditions of your commands.
|
270
|
+
|
271
|
+
```ruby
|
272
|
+
RSpec.describe FooController, type: :controller do do
|
273
|
+
context 'successful command' do
|
274
|
+
let(:foo) { create(:foo) }
|
275
|
+
let(:fake_res) { fake_result(ok: true, message: 'foo updated') }
|
276
|
+
|
277
|
+
describe '#update' do
|
278
|
+
# Foo will not be called, instead we will get back the stubbed response
|
279
|
+
# from the let block above, allowing us to bypass the command logic and
|
280
|
+
# test only the controller logic
|
281
|
+
stub_command_response(UpdateFooCommand, fake_res)
|
282
|
+
post :update, params: { id: foo.id }
|
283
|
+
expect(response).to have_http_status :ok
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
```
|
289
|
+
|
290
|
+
This method --- `stub_command_response` --- can take the return value as either a second argument, or as a block:
|
291
|
+
|
292
|
+
```ruby
|
293
|
+
stub_command_response(UpdateFooCommand, fake_res) # => fake result as an argument
|
294
|
+
stub_command_response(UpdateFooCommand) { fake_res } # => fake result as a block
|
295
|
+
```
|
296
|
+
|
262
297
|
### Minitest
|
263
298
|
|
264
299
|
To use with Minitest, update your 'test_helper' file to include:
|
@@ -366,6 +401,24 @@ $ bin/rails g slayer:form foo_form
|
|
366
401
|
$ bin/rails g slayer:command foo_command
|
367
402
|
```
|
368
403
|
|
404
|
+
## Compatability
|
405
|
+
|
406
|
+
Backwards compatability with previous versions requires additional includes.
|
407
|
+
|
408
|
+
```ruby
|
409
|
+
require 'slayer/compat/compat_040'
|
410
|
+
```
|
411
|
+
|
412
|
+
If you use test matchers, you will have to separately require the compatability layer for your test runner:
|
413
|
+
|
414
|
+
```ruby
|
415
|
+
require 'slayer/compat/minitest_compat_040'
|
416
|
+
|
417
|
+
# OR
|
418
|
+
|
419
|
+
require 'slayer/compat/rspec_compat_040'
|
420
|
+
```
|
421
|
+
|
369
422
|
## Development
|
370
423
|
|
371
424
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -389,3 +442,11 @@ Any PRs should be accompanied with documentation in `README.md`, and changes doc
|
|
389
442
|
## License
|
390
443
|
|
391
444
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
445
|
+
|
446
|
+
---
|
447
|
+
|
448
|
+
# Built by Apsis
|
449
|
+
|
450
|
+
[![apsis](https://s3-us-west-2.amazonaws.com/apsiscdn/apsis.png)](https://www.apsis.io)
|
451
|
+
|
452
|
+
`slayer` was built by Apsis Labs. We love sharing what we build! Check out our [other libraries on Github](https://github.com/apsislabs), and if you like our work you can [hire us](https://www.apsis.io/work-with-us/) to build your vision.
|
data/lib/slayer/command.rb
CHANGED
@@ -4,15 +4,11 @@ module Slayer
|
|
4
4
|
def call(*args, &block)
|
5
5
|
instance = self.new
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
rescue ResultFailureError => e
|
10
|
-
res = e.result
|
11
|
-
end
|
7
|
+
res = __get_result(instance, *args, &block)
|
8
|
+
handle_match(res, instance, block) if block_given?
|
12
9
|
|
13
10
|
raise CommandNotImplementedError unless res.is_a? Result
|
14
11
|
|
15
|
-
handle_match(res, instance, block) if block_given?
|
16
12
|
return res
|
17
13
|
end
|
18
14
|
ruby2_keywords :call if respond_to?(:ruby2_keywords, true)
|
@@ -30,6 +26,18 @@ module Slayer
|
|
30
26
|
raise ResultFailureError, err(value: value, status: status, message: message)
|
31
27
|
end
|
32
28
|
|
29
|
+
def __get_result(instance, *args, &block)
|
30
|
+
res = nil
|
31
|
+
|
32
|
+
begin
|
33
|
+
res = instance.call(*args, &block)
|
34
|
+
rescue ResultFailureError => e
|
35
|
+
res = e.result
|
36
|
+
end
|
37
|
+
|
38
|
+
res
|
39
|
+
end
|
40
|
+
|
33
41
|
private
|
34
42
|
|
35
43
|
def handle_match(res, instance, block)
|
@@ -1,6 +1,4 @@
|
|
1
1
|
# :nocov:
|
2
|
-
require 'minitest/assertions'
|
3
|
-
require 'rspec/expectations'
|
4
2
|
|
5
3
|
module Slayer
|
6
4
|
class Command
|
@@ -51,14 +49,4 @@ module Slayer
|
|
51
49
|
end
|
52
50
|
end
|
53
51
|
|
54
|
-
module Minitest::Assertions
|
55
|
-
alias assert_success assert_ok
|
56
|
-
alias refute_failed assert_ok
|
57
|
-
alias assert_failed refute_ok
|
58
|
-
alias refute_success refute_ok
|
59
|
-
end
|
60
|
-
|
61
|
-
RSpec::Matchers.alias_matcher :be_failed_result, :be_err_result
|
62
|
-
RSpec::Matchers.alias_matcher :be_success_result, :be_ok_result
|
63
|
-
|
64
52
|
# :nocov:
|
data/lib/slayer/rspec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'rspec/mocks'
|
1
2
|
require 'rspec/expectations'
|
2
3
|
|
3
4
|
# rubocop:disable Metrics/BlockLength
|
@@ -92,3 +93,25 @@ RSpec::Matchers.define :be_err_result do
|
|
92
93
|
end
|
93
94
|
|
94
95
|
# rubocop:enable Metrics/BlockLength
|
96
|
+
|
97
|
+
module Slayer
|
98
|
+
module RspecHelpers
|
99
|
+
def stub_command_response(klass, res = nil, &block)
|
100
|
+
res = block.call if block_given? && res.nil?
|
101
|
+
|
102
|
+
expect(klass).to receive(:__get_result).and_return(res)
|
103
|
+
end
|
104
|
+
|
105
|
+
# rubocop:disable Naming/MethodParameterName
|
106
|
+
def fake_result(ok: true, value: nil, message: nil, status: nil)
|
107
|
+
res = Slayer::Result.new(value, status, message)
|
108
|
+
res.fail unless ok == true
|
109
|
+
|
110
|
+
res
|
111
|
+
end
|
112
|
+
|
113
|
+
# rubocop:enable Naming/MethodParameterName
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
RSpec.configure { |config| config.include Slayer::RspecHelpers }
|
data/lib/slayer/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slayer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wyatt Kirby
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: virtus
|
@@ -183,6 +183,8 @@ files:
|
|
183
183
|
- lib/slayer.rb
|
184
184
|
- lib/slayer/command.rb
|
185
185
|
- lib/slayer/compat/compat_040.rb
|
186
|
+
- lib/slayer/compat/minitest_compat_040.rb
|
187
|
+
- lib/slayer/compat/rspec_compat_040.rb
|
186
188
|
- lib/slayer/cops/return_matcher.rb
|
187
189
|
- lib/slayer/errors.rb
|
188
190
|
- lib/slayer/form.rb
|
@@ -208,9 +210,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
208
210
|
version: '0'
|
209
211
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
210
212
|
requirements:
|
211
|
-
- - "
|
213
|
+
- - ">="
|
212
214
|
- !ruby/object:Gem::Version
|
213
|
-
version:
|
215
|
+
version: '0'
|
214
216
|
requirements: []
|
215
217
|
rubygems_version: 3.2.3
|
216
218
|
signing_key:
|