act_form 0.4.0 → 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: 6dc112ffc52fbda2d69a6f676564bddd36aa6f428fe554c3cbd66e3d05042a47
4
- data.tar.gz: 47845d8f3eaafa6595d1b088273f39075387f5ac342bb421040133af62a8aa49
3
+ metadata.gz: e4bbaec93fbf90269c9af4f2667fd6fa8a13c8c840046d3e87341433a7e6daca
4
+ data.tar.gz: 1b6069a04c93b612237f02b0ecb079d76ac283f4d909a14560575febeaf52a11
5
5
  SHA512:
6
- metadata.gz: 305fa4843044061a4ee4fb299c767cd16f1cb5d09beb0f70476586e4fb959831e723c9e56908a0712aeb0845baa387849dc979e59ed548b5f90e752cc0c09e5b
7
- data.tar.gz: 870fb9bbfe96eb6b33325b9c04192517954708b81b96fbd68724f6bd421073efb7ff33edbb8488a0d900a7275dd404273a6aa9cc6bd20e6fab063c8971245c26
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,6 +1,26 @@
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
+
18
+ # 0.4.1
19
+ * fix issue#5
20
+
1
21
  # 0.4.0
2
22
  * update bundler dependency to `2.1.x`
3
23
  * update spec syntex
4
24
  * support rails `6.0`
5
25
  * add shortcut alias method `attr` for `attribute`
6
- * fix issue#2
26
+ * fix issue#2
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,25 +37,28 @@ 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
- define_method(name) { attributes[name] || get_default(default, NO_DEFAULT_PROVIDED) }
43
+ define_method(name) do
44
+ if attributes.key?(name)
45
+ attributes[name]
46
+ else
47
+ get_default(default, NO_DEFAULT_PROVIDED)
48
+ end
49
+ end
41
50
  end
42
51
 
43
52
  def define_writer_method(name, cast_type)
44
53
  define_method("#{name}=") do |value|
45
- _value = ActiveModel::Type.lookup(cast_type).deserialize(value)
46
- @attributes = attributes.merge({name => _value})
47
- _value
54
+ val = ActiveModel::Type.lookup(cast_type).deserialize(value)
55
+ @attributes = attributes.merge({ name => val })
56
+ val
48
57
  end
49
58
  end
50
59
 
51
- private
52
-
53
60
  NO_DEFAULT_PROVIDED = Object.new
54
61
  private_constant :NO_DEFAULT_PROVIDED
55
-
56
- end # class_methods
62
+ end
57
63
  end
58
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.0'
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.0
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - zires
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-26 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
@@ -81,7 +82,7 @@ homepage: https://github.com/simple-and-powerful/act-form
81
82
  licenses:
82
83
  - MIT
83
84
  metadata: {}
84
- post_install_message:
85
+ post_install_message:
85
86
  rdoc_options: []
86
87
  require_paths:
87
88
  - lib
@@ -96,9 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
99
  requirements: []
99
- rubyforge_project:
100
- rubygems_version: 2.7.7
101
- signing_key:
100
+ rubygems_version: 3.0.3
101
+ signing_key:
102
102
  specification_version: 4
103
103
  summary: A simple way to create form/command/service objects.
104
104
  test_files: []