formally 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/README.md +43 -13
- data/lib/formally.rb +43 -8
- data/lib/formally/class_methods.rb +0 -6
- data/lib/formally/config.rb +6 -6
- data/lib/formally/overrides.rb +0 -34
- data/lib/formally/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: 7cae6a8d45efd906fe3fee47da740ab7960a9d67
|
4
|
+
data.tar.gz: 951c117c80878ca2cdb9d1c8cd7191fdc3f12377
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72de5baac3d75105f9d9993c130f633f12c0df688b5105fbcb65c6f501e5313ebcef759fd07b28b15d98ba51d7ae66bb9165b3d8bd75ab2f5d86ab916910b241
|
7
|
+
data.tar.gz: fe169bb5dc01808bf9c801e18e9cb6e0c9d129248ee6be587c38e5dd8903f6aa2897c3c54a3ede566d5be8acc32a0d9d97d29bbfc96ebe333f58838be35679d3
|
data/README.md
CHANGED
@@ -1,28 +1,58 @@
|
|
1
1
|
# Formally
|
2
2
|
|
3
|
-
|
3
|
+
A slim wrapper around `dry-validations` that makes it easier to write stateful form objects
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
## Installation
|
5
|
+
## Usage
|
8
6
|
|
9
|
-
Add
|
7
|
+
Add a schema to your class
|
10
8
|
|
11
|
-
```
|
12
|
-
|
9
|
+
```
|
10
|
+
class UserUpdateForm
|
11
|
+
prepend Formally
|
12
|
+
formally do |f|
|
13
|
+
required(:name) { str? }
|
14
|
+
required(:password) { eql? f.user.password }
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :user
|
18
|
+
|
19
|
+
def initialize user
|
20
|
+
@user = user
|
21
|
+
end
|
22
|
+
|
23
|
+
# This method will only ever be called with data validated by the
|
24
|
+
# schema above
|
25
|
+
def fill data
|
26
|
+
@user.name = data.fetch(:name)
|
27
|
+
end
|
28
|
+
|
29
|
+
# This method is wrapped in a (configurable) transaction
|
30
|
+
def save
|
31
|
+
# ...
|
32
|
+
end
|
33
|
+
end
|
13
34
|
```
|
14
35
|
|
15
|
-
|
36
|
+
and use it
|
16
37
|
|
17
|
-
|
38
|
+
```
|
39
|
+
form = UserUpdateForm.new(user: ...)
|
40
|
+
form.fill(name: 'James Dabbs', password: 'hunter2').save!
|
41
|
+
```
|
18
42
|
|
19
|
-
|
43
|
+
## API
|
20
44
|
|
21
|
-
|
45
|
+
Note that `Formally` does _prepend_ and so alters the following methods:
|
22
46
|
|
23
|
-
|
47
|
+
* fill(data) - only ever called with valid data; always returns the form
|
48
|
+
* save - wrapped in a transaction; returns true or false
|
49
|
+
* save! - calls `save` or raises an error
|
50
|
+
|
51
|
+
It also adds
|
24
52
|
|
25
|
-
|
53
|
+
* formally - contains most form metadata
|
54
|
+
* errors - to retrieve errors after `fill`ing
|
55
|
+
* valid? - to check if the form was `fill`ed with valid data
|
26
56
|
|
27
57
|
## Development
|
28
58
|
|
data/lib/formally.rb
CHANGED
@@ -15,9 +15,8 @@ module Formally
|
|
15
15
|
class << self
|
16
16
|
attr_accessor :config
|
17
17
|
|
18
|
-
def
|
19
|
-
base.extend
|
20
|
-
base.prepend Formally::Overrides
|
18
|
+
def prepended base
|
19
|
+
base.extend Formally::ClassMethods
|
21
20
|
base.formally = Formally.config.with(klass: base)
|
22
21
|
end
|
23
22
|
|
@@ -38,13 +37,49 @@ module Formally
|
|
38
37
|
predicates: [],
|
39
38
|
transaction: ->(&block) { block.call }
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
def formally
|
44
|
-
@formally ||= self.class.formally.build
|
45
|
-
end
|
40
|
+
attr_reader :formally
|
46
41
|
|
47
42
|
extend Forwardable
|
48
43
|
|
49
44
|
def_delegators :@formally, :errors, :valid?
|
45
|
+
|
46
|
+
def initialize *args
|
47
|
+
super
|
48
|
+
@formally = self.class.formally.new self
|
49
|
+
end
|
50
|
+
|
51
|
+
def fill data={}
|
52
|
+
if data.respond_to?(:permit!)
|
53
|
+
# Assume ActionController::Parameters or similar
|
54
|
+
# The schema will handle whitelisting allowed attributes
|
55
|
+
data = data.permit!.to_h.deep_symbolize_keys
|
56
|
+
end
|
57
|
+
|
58
|
+
formally.call data
|
59
|
+
|
60
|
+
if formally.valid?
|
61
|
+
super formally.data
|
62
|
+
end
|
63
|
+
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
def save
|
68
|
+
return false unless formally.valid?
|
69
|
+
formally.transaction do
|
70
|
+
super
|
71
|
+
end
|
72
|
+
formally.callbacks(:after_commit).each(&:call)
|
73
|
+
true
|
74
|
+
end
|
75
|
+
|
76
|
+
def save!
|
77
|
+
unless save
|
78
|
+
ex = Formally::Invalid.new
|
79
|
+
ex.form = self
|
80
|
+
ex.errors = errors
|
81
|
+
raise ex
|
82
|
+
end
|
83
|
+
true
|
84
|
+
end
|
50
85
|
end
|
data/lib/formally/config.rb
CHANGED
@@ -2,19 +2,19 @@ module Formally
|
|
2
2
|
class Config < Manioc.mutable(:base, :predicates, :transaction, :klass)
|
3
3
|
attr_accessor :schema
|
4
4
|
|
5
|
-
def
|
5
|
+
def new instance
|
6
6
|
Formally::State.new \
|
7
|
-
schema: schema_for(
|
7
|
+
schema: schema_for(instance),
|
8
8
|
transaction: transaction
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
12
12
|
|
13
|
-
def schema_for
|
14
|
-
if
|
15
|
-
@_cached_schema ||= build_schema
|
13
|
+
def schema_for instance
|
14
|
+
if schema.arity.zero?
|
15
|
+
@_cached_schema ||= build_schema instance
|
16
16
|
else
|
17
|
-
build_schema
|
17
|
+
build_schema instance
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
data/lib/formally/overrides.rb
CHANGED
@@ -1,38 +1,4 @@
|
|
1
1
|
module Formally
|
2
2
|
module Overrides
|
3
|
-
def fill data={}
|
4
|
-
if data.respond_to?(:permit!)
|
5
|
-
# Assume ActionController::Parameters or similar
|
6
|
-
# The schema will handle whitelisting allowed attributes
|
7
|
-
data = data.permit!.to_h.deep_symbolize_keys
|
8
|
-
end
|
9
|
-
|
10
|
-
formally.call data
|
11
|
-
|
12
|
-
if formally.valid?
|
13
|
-
super formally.data
|
14
|
-
end
|
15
|
-
|
16
|
-
self
|
17
|
-
end
|
18
|
-
|
19
|
-
def save
|
20
|
-
return false unless formally.valid?
|
21
|
-
formally.transaction do
|
22
|
-
super
|
23
|
-
end
|
24
|
-
formally.callbacks(:after_commit).each(&:call)
|
25
|
-
true
|
26
|
-
end
|
27
|
-
|
28
|
-
def save!
|
29
|
-
unless save
|
30
|
-
ex = Formally::Invalid.new
|
31
|
-
ex.form = self
|
32
|
-
ex.errors = errors
|
33
|
-
raise ex
|
34
|
-
end
|
35
|
-
true
|
36
|
-
end
|
37
3
|
end
|
38
4
|
end
|
data/lib/formally/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formally
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Dabbs
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-validation
|