formally 0.1.0 → 0.2.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: 4024b86ffa06e7d2c4467601e0f3038f8649ca64
4
- data.tar.gz: 511023d751f711b1c8e605a75b28cab2d301f4d9
3
+ metadata.gz: '03495aa4de454d0566e1a0eebf725504ca271e27'
4
+ data.tar.gz: af8d7f82f44862251357036bff15ff9c170d1003
5
5
  SHA512:
6
- metadata.gz: 7bdfac920b4f1c8a06cb0bec054869abd62acedd4ffb480e724d6f385f69102b30e05654fae25930c051d5e642db072c8f223fe8e76bf0efa176545a49e085f4
7
- data.tar.gz: 98b788cf129f170a8af41fd9ac77c7370b026906985efa0fba67b406d81ecc34c2c5fba9a7ff0a7c113de0e109f59429f98c2a289bf09f9ee2d34da4e80b4f0b
6
+ metadata.gz: f5d5a8394563961024610a0cd6d2fc66f9221236b7f5becdd8252f097b0a77e077baad4714cdf6085ee5fc2dfc9ca54327a4ee5c72703b948bd741b9fd9d371e
7
+ data.tar.gz: c0d732732ae3fe6c3b08257d1c68cc01e7bce64bd2811a88462556274c111c1db4172e4bc59b7302bf1d3520fd3194f290ad53f279155bf1d1d75288588c5ad4
@@ -2,12 +2,17 @@ module Formally
2
2
  module ClassMethods
3
3
  attr_writer :formally
4
4
 
5
- def formally *fields, &block
5
+ def formally &block
6
6
  if block
7
7
  @formally.schema = block
8
- @formally.fields += fields
9
8
  end
10
9
  @formally
11
10
  end
11
+
12
+ def build **opts
13
+ new(**opts).tap do |instance|
14
+ instance.formally = formally.build(**opts)
15
+ end
16
+ end
12
17
  end
13
18
  end
@@ -1,50 +1,28 @@
1
1
  module Formally
2
- class Config < Manioc.mutable(:base, :transaction, :klass, :fields)
2
+ class Config < Manioc.mutable(:base, :predicates, :transaction, :klass)
3
3
  attr_accessor :schema
4
- attr_reader :finalized_schema
5
4
 
6
- def finalize
7
- return if @finalized_schema
8
-
9
- _schema = schema
10
- _fields = fields || []
11
- _predicates = Formally::PredicateFinder.call klass
12
-
13
- @finalized_schema = Dry::Validation.Form base do
14
- configure do
15
- _fields.each do |name|
16
- option name
17
- end
18
-
19
- option :_self
20
-
21
- _predicates.each do |method|
22
- if method.arity == 0
23
- define_method method.name do
24
- method.bind(_self).call
25
- end
26
- else
27
- define_method method.name do |arg|
28
- method.bind(_self).call arg
29
- end
30
- end
31
- end
5
+ def build **opts
6
+ Formally::State.new \
7
+ schema: schema_for(**opts),
8
+ transaction: transaction
9
+ end
32
10
 
33
- # class_exec(&_configure) if _configure
34
- end
11
+ private
35
12
 
36
- instance_exec(&_schema)
13
+ def schema_for **opts
14
+ if opts.none? && schema.arity.zero?
15
+ @_cached_schema ||= build_schema
16
+ else
17
+ build_schema opts
37
18
  end
38
-
39
- freeze
40
19
  end
41
20
 
42
- def new object
43
- unless object.is_a? klass
44
- raise ClassMismatch, "#{object.class} is not a #{@klass}"
21
+ def build_schema *args
22
+ _schema = schema
23
+ Dry::Validation.Form base do
24
+ instance_exec(*args, &_schema)
45
25
  end
46
- finalize
47
- State.new self, object
48
26
  end
49
27
  end
50
28
  end
@@ -1,10 +1,5 @@
1
1
  module Formally
2
2
  module Overrides
3
- def initialize *args
4
- super(*args)
5
- @formally = self.class.formally.new self
6
- end
7
-
8
3
  def fill data={}
9
4
  if data.respond_to?(:permit!)
10
5
  # Assume ActionController::Parameters or similar
@@ -26,6 +21,7 @@ module Formally
26
21
  formally.transaction do
27
22
  super
28
23
  end
24
+ formally.callbacks(:after_commit).each(&:call)
29
25
  true
30
26
  end
31
27
 
@@ -1,16 +1,12 @@
1
1
  module Formally
2
2
  class State
3
- def initialize config, object
4
- @config, @object = config, object
3
+ def initialize schema:, transaction:
4
+ @schema, @transaction = schema, transaction
5
+ @callbacks = { after_commit: [] }
5
6
  end
6
7
 
7
8
  def call data
8
- injections = { _self: @object }
9
- @config.fields.each do |name|
10
- injections[name] = @object.send name
11
- end
12
-
13
- result = @config.finalized_schema.with(injections).call data
9
+ result = @schema.call data
14
10
  @data = result.output
15
11
  @errors = result.errors
16
12
  @filled = true
@@ -29,7 +25,15 @@ module Formally
29
25
  end
30
26
 
31
27
  def transaction &block
32
- @config.transaction.call(&block)
28
+ @transaction.call(&block)
29
+ end
30
+
31
+ def after_commit &block
32
+ @callbacks[:after_commit].push block
33
+ end
34
+
35
+ def callbacks key
36
+ @callbacks.fetch key, []
33
37
  end
34
38
  end
35
39
  end
@@ -1,3 +1,3 @@
1
1
  module Formally
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/formally.rb CHANGED
@@ -7,36 +7,42 @@ require 'formally/version'
7
7
  require 'formally/class_methods'
8
8
  require 'formally/config'
9
9
  require 'formally/overrides'
10
- require 'formally/predicate_finder'
11
10
  require 'formally/state'
12
11
 
13
12
  require 'formally/railtie' if defined? Rails::Railtie
14
13
 
15
14
  module Formally
16
- module Predicates
17
- end
18
-
19
15
  class << self
20
16
  attr_accessor :config
21
17
 
22
- def predicates &block
23
- Predicates.class_exec(&block)
24
- end
25
-
26
18
  def included base
27
19
  base.extend Formally::ClassMethods
28
20
  base.prepend Formally::Overrides
29
21
  base.formally = Formally.config.with(klass: base)
30
22
  end
23
+
24
+ def predicates with: []
25
+ _predicates = config.predicates
26
+ Module.new do
27
+ include Dry::Logic::Predicates
28
+ (_predicates + with).each do |predicate|
29
+ instance_exec(&predicate)
30
+ end
31
+ end
32
+ end
31
33
  end
32
34
 
33
35
  self.config = Formally::Config.new \
34
36
  klass: nil, # will be completed at include time
35
37
  base: nil,
36
- fields: [],
38
+ predicates: [],
37
39
  transaction: ->(&block) { block.call }
38
40
 
39
- attr_reader :formally
41
+ attr_writer :formally
42
+
43
+ def formally
44
+ @formally ||= self.class.formally.build
45
+ end
40
46
 
41
47
  extend Forwardable
42
48
 
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.1.0
4
+ version: 0.2.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: 2017-11-08 00:00:00.000000000 Z
11
+ date: 2017-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-validation
@@ -131,7 +131,6 @@ files:
131
131
  - lib/formally/config.rb
132
132
  - lib/formally/errors.rb
133
133
  - lib/formally/overrides.rb
134
- - lib/formally/predicate_finder.rb
135
134
  - lib/formally/railtie.rb
136
135
  - lib/formally/state.rb
137
136
  - lib/formally/version.rb
@@ -1,21 +0,0 @@
1
- module Formally
2
- module PredicateFinder
3
- def self.all_method_names klass
4
- (klass.instance_methods + klass.private_instance_methods).grep(/\?$/)
5
- end
6
-
7
- IGNORED_METHODS = all_method_names Object
8
-
9
- def self.call klass
10
- predicates = []
11
- (all_method_names(klass) - IGNORED_METHODS).each do |name|
12
- method = klass.instance_method name
13
- predicates.push method if method.arity == 0 || method.arity == 1
14
- end
15
- Formally::Predicates.instance_methods.each do |name|
16
- predicates.push Formally::Predicates.instance_method name
17
- end
18
- predicates
19
- end
20
- end
21
- end