formally 0.2.1 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1536db0a598695bcb4f89e17e2d4da26ec093965
4
- data.tar.gz: 6ec27350face39a27b2ce03b3c289e5668e8cb1a
3
+ metadata.gz: 7cae6a8d45efd906fe3fee47da740ab7960a9d67
4
+ data.tar.gz: 951c117c80878ca2cdb9d1c8cd7191fdc3f12377
5
5
  SHA512:
6
- metadata.gz: b0c3a07f5439a46900abbe48f8ee181b97ff0305f1e573557c0434800a498fa3d8dec8131bbeeaa4bb28dacc3ded988c52af43d62e39ae4ed72a346558c9a258
7
- data.tar.gz: fef80e39a6aefe89a34b02e7b22a6b51ff3de41653f4b5e49c954ea4216b55d53f8e5fada23fef91907327cd000842b295b69ee6cbc73e14be56370cc8167c79
6
+ metadata.gz: 72de5baac3d75105f9d9993c130f633f12c0df688b5105fbcb65c6f501e5313ebcef759fd07b28b15d98ba51d7ae66bb9165b3d8bd75ab2f5d86ab916910b241
7
+ data.tar.gz: fe169bb5dc01808bf9c801e18e9cb6e0c9d129248ee6be587c38e5dd8903f6aa2897c3c54a3ede566d5be8acc32a0d9d97d29bbfc96ebe333f58838be35679d3
data/README.md CHANGED
@@ -1,28 +1,58 @@
1
1
  # Formally
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/formally`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A slim wrapper around `dry-validations` that makes it easier to write stateful form objects
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
5
+ ## Usage
8
6
 
9
- Add this line to your application's Gemfile:
7
+ Add a schema to your class
10
8
 
11
- ```ruby
12
- gem 'formally'
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
- And then execute:
36
+ and use it
16
37
 
17
- $ bundle
38
+ ```
39
+ form = UserUpdateForm.new(user: ...)
40
+ form.fill(name: 'James Dabbs', password: 'hunter2').save!
41
+ ```
18
42
 
19
- Or install it yourself as:
43
+ ## API
20
44
 
21
- $ gem install formally
45
+ Note that `Formally` does _prepend_ and so alters the following methods:
22
46
 
23
- ## Usage
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
- TODO: Write usage instructions here
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 included base
19
- base.extend Formally::ClassMethods
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
- attr_writer :formally
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
@@ -8,11 +8,5 @@ module Formally
8
8
  end
9
9
  @formally
10
10
  end
11
-
12
- def build **opts
13
- new(**opts).tap do |instance|
14
- instance.formally = formally.build(**opts)
15
- end
16
- end
17
11
  end
18
12
  end
@@ -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 build **opts
5
+ def new instance
6
6
  Formally::State.new \
7
- schema: schema_for(**opts),
7
+ schema: schema_for(instance),
8
8
  transaction: transaction
9
9
  end
10
10
 
11
11
  private
12
12
 
13
- def schema_for **opts
14
- if opts.none? && schema.arity.zero?
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 opts
17
+ build_schema instance
18
18
  end
19
19
  end
20
20
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Formally
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
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.2.1
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-02-22 00:00:00.000000000 Z
11
+ date: 2018-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-validation