slayer 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/README.md +80 -3
- data/lib/slayer/command.rb +5 -4
- data/lib/slayer/result_matcher.rb +13 -10
- data/lib/slayer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 324ed5c2712dfb7b91ab54d26035264be046c975
|
4
|
+
data.tar.gz: 9cbef2f5c7393d18d8323a19a12d94972e417806
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50c3825dce992223f71114a6f0db3aac21534e5d24332422549f687470a07acaf45e0750303e1cf66a4ee716c1e84c457a092bc7aa5ee68cdf6624abc409a20a
|
7
|
+
data.tar.gz: b86843b30033d24661fcb01a4572a3d8f16054b59ff1bcd1e422c5bed386f53d0f166a39ec6b532abf46c5331227899f4440220f03cd2bc6bc371912ec070d82
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -44,11 +44,88 @@ Or install it yourself as:
|
|
44
44
|
$ gem install slayer
|
45
45
|
```
|
46
46
|
|
47
|
+
## Rails Integration
|
48
|
+
|
49
|
+
While Slayer is independent of any framework, we do offer a first-class integration with Ruby on Rails. To install the Rails extensions, add this line to your application's Gemfile:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
gem 'slayer_rails'
|
53
|
+
```
|
54
|
+
|
55
|
+
And then execute:
|
56
|
+
|
57
|
+
```sh
|
58
|
+
$ bundle
|
59
|
+
```
|
60
|
+
|
61
|
+
And that's it. The integration provides a small handful of features that make your life easier when working with Ruby on Rails.
|
62
|
+
|
63
|
+
### Form Validations
|
64
|
+
|
65
|
+
With `slayer_rails`, `Slayer::Form` objects are automatically extended with `ActiveRecord` validations. You can use the same validations you would on your `ActiveRecord` models, but directly on your forms.
|
66
|
+
|
67
|
+
### Form Creation
|
68
|
+
|
69
|
+
With `slayer_rails` there are two new methods for instantiating `Slayer::Form` objects: `from_params` and `from_model`. These make it easier to populate forms with data while in your Rails controllers.
|
70
|
+
|
71
|
+
Take the following example for a `FooController`:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
class FooController < ApplicationController
|
75
|
+
def new
|
76
|
+
@foo_form = FooForm.new
|
77
|
+
end
|
78
|
+
|
79
|
+
def edit
|
80
|
+
@foo = Foo.find(params[:id])
|
81
|
+
@foo_form = FooForm.from_model(@foo)
|
82
|
+
end
|
83
|
+
|
84
|
+
def create
|
85
|
+
@foo_form = FooForm.from_params(foo_params)
|
86
|
+
end
|
87
|
+
|
88
|
+
def update
|
89
|
+
@foo_form = FooForm.from_params(foo_params)
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def foo_params
|
95
|
+
params.require(:foo).permit(:bar, :baz)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
### Transactions
|
101
|
+
|
102
|
+
`Slayer::Command` and `Slayer::Service` objects are extended with access to `ActiveRecord` transactions. Anywhere in your `Command` or `Service` objects, you can execute a `transaction` block, which will let you bundle database interactions.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
class FooCommand < Slayer::Command
|
106
|
+
def call
|
107
|
+
transaction do
|
108
|
+
# => database interactions
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
### Generators
|
115
|
+
|
116
|
+
Use generators to make sure your `Slayer` objects are always in the right place. `slayer_rails` includes generators for `Slayer::Form`, `Slayer::Command`, and `Slayer::Service` objects.
|
117
|
+
|
118
|
+
```sh
|
119
|
+
$ bin/rails g slayer:form foo_form
|
120
|
+
$ bin/rails g slayer:command foo_command
|
121
|
+
$ bin/rails g slayer:service foo_service
|
122
|
+
```
|
123
|
+
|
47
124
|
## Usage
|
48
125
|
|
49
126
|
### Commands
|
50
127
|
|
51
|
-
Slayer Commands should implement `call`, which will `pass` or `fail` the service based on input. Commands return a `Slayer::Result` which has a predictable interface for determining `success?` or `failure?`, a
|
128
|
+
Slayer Commands should implement `call`, which will `pass` or `fail` the service based on input. Commands return a `Slayer::Result` which has a predictable interface for determining `success?` or `failure?`, a 'value' payload object, a 'status' value, and a user presentable `message`.
|
52
129
|
|
53
130
|
```ruby
|
54
131
|
# A Command that passes when given the string "foo"
|
@@ -56,9 +133,9 @@ Slayer Commands should implement `call`, which will `pass` or `fail` the service
|
|
56
133
|
class FooCommand < Slayer::Command
|
57
134
|
def call(foo:)
|
58
135
|
if foo == "foo"
|
59
|
-
pass!
|
136
|
+
pass! value: foo, message: "Passing FooCommand"
|
60
137
|
else
|
61
|
-
fail!
|
138
|
+
fail! value: foo, message: "Failing FooCommand"
|
62
139
|
end
|
63
140
|
end
|
64
141
|
end
|
data/lib/slayer/command.rb
CHANGED
@@ -56,14 +56,15 @@ module Slayer
|
|
56
56
|
end
|
57
57
|
|
58
58
|
# Fail the Command
|
59
|
-
|
60
|
-
|
59
|
+
|
60
|
+
def fail!(value: nil, status: :default, message: nil)
|
61
|
+
@result = Result.new(value, status, message)
|
61
62
|
@result.fail!
|
62
63
|
end
|
63
64
|
|
64
65
|
# Pass the Command
|
65
|
-
def pass!(
|
66
|
-
@result = Result.new(
|
66
|
+
def pass!(value: nil, status: :default, message: nil)
|
67
|
+
@result = Result.new(value, status, message)
|
67
68
|
end
|
68
69
|
|
69
70
|
# Call the command
|
@@ -195,23 +195,26 @@ module Slayer
|
|
195
195
|
# @api private
|
196
196
|
def execute_matching_block
|
197
197
|
if @matching_block != false # nil should pass this test
|
198
|
-
@matching_block
|
199
|
-
# safe navigation (&.)
|
198
|
+
run_block(@matching_block)
|
200
199
|
elsif @matching_all != false
|
201
|
-
@matching_all
|
200
|
+
run_block(@matching_all)
|
202
201
|
elsif @default_block != false
|
203
|
-
@default_block
|
202
|
+
run_block(@default_block)
|
204
203
|
elsif @default_all
|
205
|
-
@default_all
|
204
|
+
run_block(@default_all)
|
206
205
|
end
|
207
206
|
end
|
208
207
|
|
208
|
+
# Executes the ensure block if one exists.
|
209
|
+
# @api private
|
209
210
|
def execute_ensure_block
|
210
|
-
#
|
211
|
-
if @ensure_block != false # nil should pass this test
|
212
|
-
@ensure_block.call(@result, @command)
|
213
|
-
end
|
214
|
-
# rubocop:enable Style/IfUnlessModifier
|
211
|
+
run_block(@ensure_block) if @ensure_block != false # nil should pass this test
|
215
212
|
end
|
213
|
+
|
214
|
+
private
|
215
|
+
|
216
|
+
def run_block(block)
|
217
|
+
block.call(@result.value, @result, @command) if block # explicit nil should fail this test
|
218
|
+
end
|
216
219
|
end
|
217
220
|
end
|
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.
|
4
|
+
version: 0.3.0
|
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: 2017-02-
|
12
|
+
date: 2017-02-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: virtus
|