writ 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +61 -0
- data/lib/writ.rb +58 -0
- data/lib/writ/version.rb +3 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 67c0b4c57a61a29b4036ee1f1e7f216d537849e2
|
4
|
+
data.tar.gz: 3c38c672cfbf783f3c3351de5cb2f73af181797e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08b642aec1d49003c8d4aa10b826b651eb1e33c35dd121e631417d6bcf0e0142d8ad7f9f63a67f370918073db6559d307504e790046d7d56dc9e220fac47fb1a
|
7
|
+
data.tar.gz: e48fba03b082eb90965d6f812cc675096ea4dc87ac09dc7c50cc2ff3a53ce101175992a7c56f153bc33d0ca9eebbfd3ea504709330f098104c5e7fe9d14f484c
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Nicolas Sanguinetti <hi@nicolassanguinetti.info>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Writ: A minimal command pattern implementation
|
2
|
+
|
3
|
+
> **writ**, _noun_:<br>
|
4
|
+
> a form of written command in the name of a court or other legal authority to act, or abstain from acting, in some way.
|
5
|
+
|
6
|
+
`Writ` is a minimal [command pattern][pattern] implementation, including input validation, built on top of [Scrivener][scrivener].
|
7
|
+
|
8
|
+
[pattern]: http://wiki.c2.com/?CommandPattern
|
9
|
+
[scrivener]: https://github.com/soveran/scrivener
|
10
|
+
|
11
|
+
# Example
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
# Define your commands
|
15
|
+
class LogIn < Writ
|
16
|
+
attr_accessor :email
|
17
|
+
attr_accessor :password
|
18
|
+
|
19
|
+
def validate
|
20
|
+
assert_email :email
|
21
|
+
assert_present :password
|
22
|
+
end
|
23
|
+
|
24
|
+
def run
|
25
|
+
user = User.authenticate(email, password)
|
26
|
+
assert user, [:email, :not_valid]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# And use them
|
31
|
+
outcome = LogIn.run(req.params)
|
32
|
+
|
33
|
+
if outcome.success?
|
34
|
+
session[:user_id] = outcome.value.id
|
35
|
+
else
|
36
|
+
outcome.input.password = nil # Don't leak it when rendering the form again
|
37
|
+
render "auth/sign_in", errors: outcome.errors, form: outcome.input
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Install
|
42
|
+
|
43
|
+
gem install writ
|
44
|
+
|
45
|
+
## Validations
|
46
|
+
|
47
|
+
As with [scrivener][], you should define a `validate` method that defines all validations for the user input. You're also welcome to use validations inside of `run`, when an error can come from the process itself. The example above, where the email/password combination yields no user, is a good example. Another example would be having to make an API call that returns an error.
|
48
|
+
|
49
|
+
Take a look at the default [validations](https://github.com/soveran/scrivener#assertions) that come from scrivener, or feel free to define custom ones to suit your domain.
|
50
|
+
|
51
|
+
## Why
|
52
|
+
|
53
|
+
When using [scrivener][], I tend to add a `run` method to the objects to alter state based on user input alongside the validations. Some people like calling these objects "services", "use cases", "commands", or "interactions".
|
54
|
+
|
55
|
+
I find this useful to decouple complex actions from the actors involved in them, and to keep models "thin" by only caring about expressing the domain without burdening themselves with expressing how users can interact with the domain, or with concepts like validation and authorization.
|
56
|
+
|
57
|
+
After using this pattern in several production projects, I figured it might as well live in a gem I can reuse instead of copy-pasting code around.
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
This project is shared under the MIT license. See the attached [LICENSE](./LICENSE) file for details.
|
data/lib/writ.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "scrivener"
|
2
|
+
|
3
|
+
class Writ < Scrivener
|
4
|
+
# Public: Run the command with the provided input. Any arguments will be
|
5
|
+
# forwarded to `Writ#new`.
|
6
|
+
#
|
7
|
+
# Returns a Writ::Outcome.
|
8
|
+
def self.run(*args, &block)
|
9
|
+
new(*args, &block).run!
|
10
|
+
end
|
11
|
+
|
12
|
+
# Internal: Runs your business logic, and assigns the return value of this
|
13
|
+
# method as the Outcome's `#value`.
|
14
|
+
#
|
15
|
+
# Returns an Object.
|
16
|
+
def run
|
17
|
+
fail NotImplementedError
|
18
|
+
end
|
19
|
+
|
20
|
+
# Internal: Perform validations, run the business logic, and generate the
|
21
|
+
# outcome to return.
|
22
|
+
#
|
23
|
+
# Returns a Writ::Outcome.
|
24
|
+
def run!
|
25
|
+
result = run if valid?
|
26
|
+
Outcome.new(result, self)
|
27
|
+
end
|
28
|
+
|
29
|
+
class Outcome
|
30
|
+
# Public: Get the return value of your Writ.
|
31
|
+
attr_reader :value
|
32
|
+
|
33
|
+
# Public: Get the Writ object, with the user input as accessors.
|
34
|
+
attr_reader :input
|
35
|
+
|
36
|
+
# Public: Get the errors Hash generated by running Writ.run. This will be an
|
37
|
+
# empty Hash if no errors were returned.
|
38
|
+
attr_reader :errors
|
39
|
+
|
40
|
+
# Public: Initialize the outcome.
|
41
|
+
#
|
42
|
+
# value - The return value from Writ.run
|
43
|
+
# writ - The Writ instance.
|
44
|
+
def initialize(value, writ)
|
45
|
+
@value = value
|
46
|
+
@input = writ
|
47
|
+
@errors = writ.errors
|
48
|
+
end
|
49
|
+
|
50
|
+
# Public: Whether the Writ performed successfully or not (i.e. no errors are
|
51
|
+
# present.)
|
52
|
+
#
|
53
|
+
# Returns Boolean.
|
54
|
+
def success?
|
55
|
+
errors.empty?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/writ/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: writ
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.rc1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Sanguinetti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: scrivener
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cutest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
description: Tiny implementation of the command pattern, including input validation,
|
42
|
+
using Scrivener.
|
43
|
+
email:
|
44
|
+
- contacto@nicolassanguinetti.info
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- lib/writ.rb
|
52
|
+
- lib/writ/version.rb
|
53
|
+
homepage: http://github.com/foca/writ
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 1.3.1
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.5.1
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Minimal implementation of the command pattern.
|
77
|
+
test_files: []
|