act_form 0.4.1 → 0.4.4

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
  SHA256:
3
- metadata.gz: 1f096ba9408679e014443f6ebc3ac2f2083b1f081ac923c97a485d9ea1c67784
4
- data.tar.gz: cee283b7050b2d532d10ce998102c42aad399739f3841e025cf41a4cc92afdc7
3
+ metadata.gz: d93ee18c0209358b5bfdd5f8bab5c31b9065ed223a901de2b5a45db192c7eb7a
4
+ data.tar.gz: 6db591154d02ccb5407ee4cb615de83ef1337891cd8dda2f611c7bb6fe4b8522
5
5
  SHA512:
6
- metadata.gz: 755641d26b589a199cb05e36c9c6063ece293010fedac1e6c34d7229e51bd0692d32e32420ce42cd1a2fb2491b83d279aa022cb234f406335f559d66fd6a2150
7
- data.tar.gz: 8089ca663ee9a24b144497d2dc462a76e802989f15e3bce9af75e42f1cbba6328349a0d87aac37b8cc183d8680fd62a4c0d6bcf6c1be922f80a42bd1f3c50780
6
+ metadata.gz: 5baa5bcdd152bbc018e1d9faa6df224290af67c98064297636e3fedc69b38c626dd0a442076a32ccac199a88915a7a4725ed076c97af8fce7a16f48565b1b78e
7
+ data.tar.gz: dccf8fa34e9e10965207b87227ba687926732c27086f59bbe0e5e82bd2d056521ef6399fd7558457a2b3cb9ad8b36cf72dd3489d667ac5ee90dd68634dd7daa9
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /*.gem
11
+ .DS_Store
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,23 @@
1
+ # 0.4.4
2
+ * [Fix] fix `setup` run before `required` validation
3
+
4
+ # 0.4.3
5
+ * [Feature] issue#6 add `setup` method
6
+ ```ruby
7
+ class SomeCommand < Act::Command
8
+ # use setup to pre-inialize some actions.
9
+ setup do
10
+ @ins = 1
11
+ end
12
+
13
+ def perform
14
+ @ins # use @ins
15
+ end
16
+ end
17
+ ```
18
+
19
+ # 0.4.2 [yank]
20
+
1
21
  # 0.4.1
2
22
  * fix issue#5
3
23
 
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
@@ -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
- alias_method :attr, :attribute
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
- _value = ActiveModel::Type.lookup(cast_type).deserialize(value)
52
- @attributes = attributes.merge({name => _value})
53
- _value
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
@@ -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
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActForm
2
- module Merge
4
+ module Merge # rubocop:disable Style/Documentation
3
5
  extend ActiveSupport::Concern
4
6
 
5
7
  class_methods do
@@ -10,6 +12,5 @@ module ActForm
10
12
  end
11
13
  end
12
14
  end
13
-
14
15
  end
15
16
  end
@@ -1,38 +1,43 @@
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
12
+ include ActiveModel::Validations::Callbacks
10
13
  include Attributes
11
14
  include Merge
12
15
 
13
16
  included do
14
- set_callback :validate, :before, :validate_required_attributes
17
+ set_callback :validation, :before, :validate_required_attributes
15
18
  end
16
19
 
17
- def initialize(attrs={})
20
+ def initialize(attrs = {})
18
21
  super attrs.select { |k, _| respond_to?("#{k}=") }
19
22
  end
20
23
 
21
24
  def record=(record)
22
25
  raise ArgumentError, 'Record must respond to attributes method!' unless record.respond_to?(:attributes)
26
+
23
27
  @record = record
24
28
  end
25
29
 
26
30
  # Record must respond_to attributes method
27
31
  def init_by(record, **attrs)
28
32
  self.record = record
29
- _attrs = record.attributes.extract! *self.class.attribute_set.keys.map(&:to_s)
33
+ _attrs = record.attributes.extract!(*self.class.attribute_set.keys.map(&:to_s))
30
34
  assign_attributes _attrs.merge(attrs)
31
35
  end
32
36
 
33
37
  def sync(target)
34
- self.class.attribute_set.keys.each do |attr|
38
+ self.class.attribute_set.each_key do |attr|
35
39
  next unless target.respond_to?(attr)
40
+
36
41
  target.public_send "#{attr}=", public_send(attr)
37
42
  end
38
43
  end
@@ -57,8 +62,9 @@ module ActForm
57
62
  self.class.attribute_set.each do |attr_name, arr|
58
63
  _, options = arr
59
64
  next if options.key?(:default)
60
- next if !options[:required]
61
- if attributes[attr_name].nil?
65
+ next if !options[:required] # rubocop:disable Style/NegatedIf
66
+
67
+ if attributes[attr_name].nil? # rubocop:disable Style/IfUnlessModifier
62
68
  errors.add(attr_name, :required)
63
69
  end
64
70
  end
@@ -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
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model/type'
2
4
 
3
5
  module ActForm
4
6
  module Type
7
+ # Add +Object+ type
5
8
  class Object < ActiveModel::Type::Value
6
9
  def type
7
10
  :object
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActForm
2
- VERSION = '0.4.1'
4
+ VERSION = '0.4.4'
3
5
  end
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'
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.1
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - zires
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-04 00:00:00.000000000 Z
11
+ date: 2022-11-02 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