act_form 0.4.1 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +23 -0
- data/CHANGELOG.md +17 -0
- data/README.md +7 -1
- data/lib/act_form/attributes.rb +10 -10
- data/lib/act_form/combinable.rb +6 -5
- data/lib/act_form/merge.rb +3 -2
- data/lib/act_form/model.rb +11 -6
- data/lib/act_form/runnable.rb +10 -2
- data/lib/act_form/type.rb +3 -0
- data/lib/act_form/version.rb +3 -1
- data/lib/act_form.rb +3 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4bbaec93fbf90269c9af4f2667fd6fa8a13c8c840046d3e87341433a7e6daca
|
4
|
+
data.tar.gz: 1b6069a04c93b612237f02b0ecb079d76ac283f4d909a14560575febeaf52a11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc3e0058a40aec51c219f24a6fe90f478a607c7f4ea8888133cd382c6abf8dab48cbae9340adf8facf6027e618f53b74b70e8bf2ac67eca777276642de46f251
|
7
|
+
data.tar.gz: 171cb38223df688c0c0cb8596628e339e235403ef15f531f7d0fa70204382e1918feed00dd8cce7f1543789ae6cb78016bb998df6c746e3dcea17f44f9fabf27
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- "test/**/*"
|
4
|
+
- "act_form.gemspec"
|
5
|
+
- "Rakefile"
|
6
|
+
- "Gemfile"
|
7
|
+
|
8
|
+
|
9
|
+
MethodLength:
|
10
|
+
Max: 20
|
11
|
+
|
12
|
+
Style/Documentation:
|
13
|
+
Exclude:
|
14
|
+
- "lib/act_form.rb"
|
15
|
+
|
16
|
+
Style/RedundantSelf:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/CommentedKeyword:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Lint/UnderscorePrefixedVariableName:
|
23
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
# 0.4.3
|
2
|
+
* [Feature] issue#6 add `setup` method
|
3
|
+
```ruby
|
4
|
+
class SomeCommand < Act::Command
|
5
|
+
# use setup to pre-inialize some actions.
|
6
|
+
setup do
|
7
|
+
@ins = 1
|
8
|
+
end
|
9
|
+
|
10
|
+
def perform
|
11
|
+
@ins # use @ins
|
12
|
+
end
|
13
|
+
end
|
14
|
+
```
|
15
|
+
|
16
|
+
# 0.4.2 [yank]
|
17
|
+
|
1
18
|
# 0.4.1
|
2
19
|
* fix issue#5
|
3
20
|
|
data/README.md
CHANGED
@@ -144,15 +144,21 @@ AdminForm.new(phone: '12345678901').valid? # => true
|
|
144
144
|
|
145
145
|
Command object almost like form object. Command object can't init by `new`, and it has some new features.
|
146
146
|
|
147
|
-
#### API - `perform`, `run`, `success?`, `failure
|
147
|
+
#### API - `perform`, `run`, `success?`, `failure?`, `setup`
|
148
148
|
|
149
149
|
command object must respond to `perform` method.
|
150
150
|
|
151
151
|
```ruby
|
152
152
|
class CreateUserCommand < ActForm::Command
|
153
153
|
combine UserForm
|
154
|
+
|
155
|
+
# Do some pre-inialize actions.
|
156
|
+
setup do
|
157
|
+
@name = 'foo'
|
158
|
+
end
|
154
159
|
|
155
160
|
def perform
|
161
|
+
attributes[:name] = @name
|
156
162
|
User.create(attributes)
|
157
163
|
end
|
158
164
|
end
|
data/lib/act_form/attributes.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'act_form/type'
|
2
4
|
|
3
5
|
module ActForm
|
4
|
-
module Attributes
|
6
|
+
module Attributes # rubocop:disable Style/Documentation
|
5
7
|
extend ActiveSupport::Concern
|
6
8
|
|
7
9
|
included do
|
@@ -17,10 +19,11 @@ module ActForm
|
|
17
19
|
|
18
20
|
def get_default(default, default_provided)
|
19
21
|
return if default == default_provided
|
22
|
+
|
20
23
|
default.respond_to?(:call) ? default.call : default
|
21
24
|
end
|
22
25
|
|
23
|
-
module ClassMethods
|
26
|
+
module ClassMethods # rubocop:disable Style/Documentation
|
24
27
|
# attribute :name, type: :string
|
25
28
|
# or
|
26
29
|
# attribute :name, :string, required: true
|
@@ -34,7 +37,7 @@ module ActForm
|
|
34
37
|
|
35
38
|
name
|
36
39
|
end
|
37
|
-
|
40
|
+
alias attr attribute
|
38
41
|
|
39
42
|
def define_reader_method(name, default: NO_DEFAULT_PROVIDED)
|
40
43
|
define_method(name) do
|
@@ -48,17 +51,14 @@ module ActForm
|
|
48
51
|
|
49
52
|
def define_writer_method(name, cast_type)
|
50
53
|
define_method("#{name}=") do |value|
|
51
|
-
|
52
|
-
@attributes = attributes.merge({name =>
|
53
|
-
|
54
|
+
val = ActiveModel::Type.lookup(cast_type).deserialize(value)
|
55
|
+
@attributes = attributes.merge({ name => val })
|
56
|
+
val
|
54
57
|
end
|
55
58
|
end
|
56
59
|
|
57
|
-
private
|
58
|
-
|
59
60
|
NO_DEFAULT_PROVIDED = Object.new
|
60
61
|
private_constant :NO_DEFAULT_PROVIDED
|
61
|
-
|
62
|
-
end # class_methods
|
62
|
+
end
|
63
63
|
end
|
64
64
|
end
|
data/lib/act_form/combinable.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
4
|
|
3
5
|
module ActForm
|
4
|
-
module Combinable
|
6
|
+
module Combinable # rubocop:disable Style/Documentation
|
5
7
|
extend ActiveSupport::Concern
|
6
8
|
|
7
9
|
included do
|
@@ -17,12 +19,14 @@ module ActForm
|
|
17
19
|
|
18
20
|
def combined_forms_valid?(context)
|
19
21
|
return if _forms.empty?
|
22
|
+
|
20
23
|
_forms.each do |form_class|
|
21
24
|
form = form_class.new(attributes)
|
22
25
|
form.valid?(context)
|
23
26
|
form.errors.details.each do |attr_name, arr|
|
24
27
|
arr.each do |error|
|
25
28
|
next if error[:error] == :required
|
29
|
+
|
26
30
|
errors.add(attr_name, error[:error])
|
27
31
|
end
|
28
32
|
end
|
@@ -30,7 +34,6 @@ module ActForm
|
|
30
34
|
end
|
31
35
|
|
32
36
|
class_methods do
|
33
|
-
|
34
37
|
def combine(*forms)
|
35
38
|
forms.each do |form_class|
|
36
39
|
raise ArgumentError, "can't combine itself" if form_class == self
|
@@ -40,9 +43,7 @@ module ActForm
|
|
40
43
|
self.merge_attribute_set_from(form_class)
|
41
44
|
self._forms << form_class
|
42
45
|
end
|
43
|
-
end
|
44
|
-
|
46
|
+
end # End of combine
|
45
47
|
end
|
46
|
-
|
47
48
|
end
|
48
49
|
end
|
data/lib/act_form/merge.rb
CHANGED
data/lib/act_form/model.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_model'
|
2
4
|
require 'act_form/attributes'
|
3
5
|
require 'act_form/merge'
|
4
6
|
require 'act_form/combinable'
|
5
7
|
|
6
8
|
module ActForm
|
7
|
-
module Model
|
9
|
+
module Model # rubocop:disable Style/Documentation
|
8
10
|
extend ActiveSupport::Concern
|
9
11
|
include ActiveModel::Model
|
10
12
|
include Attributes
|
@@ -14,25 +16,27 @@ module ActForm
|
|
14
16
|
set_callback :validate, :before, :validate_required_attributes
|
15
17
|
end
|
16
18
|
|
17
|
-
def initialize(attrs={})
|
19
|
+
def initialize(attrs = {})
|
18
20
|
super attrs.select { |k, _| respond_to?("#{k}=") }
|
19
21
|
end
|
20
22
|
|
21
23
|
def record=(record)
|
22
24
|
raise ArgumentError, 'Record must respond to attributes method!' unless record.respond_to?(:attributes)
|
25
|
+
|
23
26
|
@record = record
|
24
27
|
end
|
25
28
|
|
26
29
|
# Record must respond_to attributes method
|
27
30
|
def init_by(record, **attrs)
|
28
31
|
self.record = record
|
29
|
-
_attrs = record.attributes.extract!
|
32
|
+
_attrs = record.attributes.extract!(*self.class.attribute_set.keys.map(&:to_s))
|
30
33
|
assign_attributes _attrs.merge(attrs)
|
31
34
|
end
|
32
35
|
|
33
36
|
def sync(target)
|
34
|
-
self.class.attribute_set.
|
37
|
+
self.class.attribute_set.each_key do |attr|
|
35
38
|
next unless target.respond_to?(attr)
|
39
|
+
|
36
40
|
target.public_send "#{attr}=", public_send(attr)
|
37
41
|
end
|
38
42
|
end
|
@@ -57,8 +61,9 @@ module ActForm
|
|
57
61
|
self.class.attribute_set.each do |attr_name, arr|
|
58
62
|
_, options = arr
|
59
63
|
next if options.key?(:default)
|
60
|
-
next if !options[:required]
|
61
|
-
|
64
|
+
next if !options[:required] # rubocop:disable Style/NegatedIf
|
65
|
+
|
66
|
+
if attributes[attr_name].nil? # rubocop:disable Style/IfUnlessModifier
|
62
67
|
errors.add(attr_name, :required)
|
63
68
|
end
|
64
69
|
end
|
data/lib/act_form/runnable.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
4
|
|
3
5
|
module ActForm
|
4
6
|
class RunError < StandardError; end
|
7
|
+
|
8
|
+
# Define runnable behaivor for form object.
|
5
9
|
module Runnable
|
6
10
|
extend ActiveSupport::Concern
|
7
11
|
|
@@ -10,6 +14,10 @@ module ActForm
|
|
10
14
|
end
|
11
15
|
|
12
16
|
class_methods do
|
17
|
+
def setup(&block)
|
18
|
+
self.before_validation(&block)
|
19
|
+
end
|
20
|
+
|
13
21
|
def run(*args)
|
14
22
|
new(*args).run
|
15
23
|
end
|
@@ -19,7 +27,7 @@ module ActForm
|
|
19
27
|
end
|
20
28
|
end
|
21
29
|
|
22
|
-
def has_errors?
|
30
|
+
def has_errors? # rubocop:disable Naming/PredicateName
|
23
31
|
!errors.empty?
|
24
32
|
end
|
25
33
|
|
@@ -32,7 +40,7 @@ module ActForm
|
|
32
40
|
end
|
33
41
|
|
34
42
|
def run!
|
35
|
-
if valid?
|
43
|
+
if valid? # rubocop:disable Style/GuardClause
|
36
44
|
@result = perform
|
37
45
|
@performed = true
|
38
46
|
result
|
data/lib/act_form/type.rb
CHANGED
data/lib/act_form/version.rb
CHANGED
data/lib/act_form.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'act_form/version'
|
2
4
|
require 'act_form/model'
|
3
5
|
require 'act_form/runnable'
|
@@ -9,6 +11,7 @@ module ActForm
|
|
9
11
|
|
10
12
|
class Command
|
11
13
|
include Model
|
14
|
+
include ActiveModel::Validations::Callbacks
|
12
15
|
include Runnable
|
13
16
|
private_class_method :new
|
14
17
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: act_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zires
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -61,6 +61,7 @@ extensions: []
|
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
63
|
- ".gitignore"
|
64
|
+
- ".rubocop.yml"
|
64
65
|
- CHANGELOG.md
|
65
66
|
- CODE_OF_CONDUCT.md
|
66
67
|
- Gemfile
|