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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f096ba9408679e014443f6ebc3ac2f2083b1f081ac923c97a485d9ea1c67784
4
- data.tar.gz: cee283b7050b2d532d10ce998102c42aad399739f3841e025cf41a4cc92afdc7
3
+ metadata.gz: e4bbaec93fbf90269c9af4f2667fd6fa8a13c8c840046d3e87341433a7e6daca
4
+ data.tar.gz: 1b6069a04c93b612237f02b0ecb079d76ac283f4d909a14560575febeaf52a11
5
5
  SHA512:
6
- metadata.gz: 755641d26b589a199cb05e36c9c6063ece293010fedac1e6c34d7229e51bd0692d32e32420ce42cd1a2fb2491b83d279aa022cb234f406335f559d66fd6a2150
7
- data.tar.gz: 8089ca663ee9a24b144497d2dc462a76e802989f15e3bce9af75e42f1cbba6328349a0d87aac37b8cc183d8680fd62a4c0d6bcf6c1be922f80a42bd1f3c50780
6
+ metadata.gz: dc3e0058a40aec51c219f24a6fe90f478a607c7f4ea8888133cd382c6abf8dab48cbae9340adf8facf6027e618f53b74b70e8bf2ac67eca777276642de46f251
7
+ data.tar.gz: 171cb38223df688c0c0cb8596628e339e235403ef15f531f7d0fa70204382e1918feed00dd8cce7f1543789ae6cb78016bb998df6c746e3dcea17f44f9fabf27
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,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
@@ -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,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! *self.class.attribute_set.keys.map(&:to_s)
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.keys.each do |attr|
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
- if attributes[attr_name].nil?
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
@@ -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.3'
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'
@@ -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.1
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-03-04 00:00:00.000000000 Z
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