active_record_compose 0.5.0 → 0.6.1

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: 7a3112ceb79671ca112aa5dde2222e10a0bd4daed96f82ebaa641746165d8991
4
- data.tar.gz: e021f41a5e3334b0e91f170712c011679023a384bbfe17c3df2a97858747323e
3
+ metadata.gz: a87013307c9d2f0532ca4365765d94dc1423705c9d7df309af4f0c856d5803a6
4
+ data.tar.gz: fc650023b00d26306a222800f16949ffdf65c096028e07ccd855bed5b16258f9
5
5
  SHA512:
6
- metadata.gz: 56cb8c6d615cbefb58fc774e96caa9add4cb72a8ca34188ce2d713f6b4f32ef75b7df4f273da2bd6966d08c1a8f1aadbeef838f3c5b436e5f1122efb7af5a961
7
- data.tar.gz: 0aec57e80f57d0c3d1bf1d45e4cb39c3cebcae47c53a35b630817de592bd3893604ff58563eed2c6b23f4199d2f27bc39804a1a9e6fe20bbdb809defaae30e28
6
+ metadata.gz: 95b6fdee6e5977ab27a7973aeb241a48f9dede2d05aa3685586b549db1f74dc5e4bf8df9e49e66fbe8e1bcf8be3f1dfaca4ff183dc67bd42771d10db64a371be
7
+ data.tar.gz: 410b89dd9af2e0878aa6646996d2ad48b33297b1b932c1dfb2caf21b284e7a2cb2a2fd7559d55a135984484719b44304086ae280692188a5001d1cd2759e3dcb
data/.rubocop.yml CHANGED
@@ -9,6 +9,9 @@ Style/ArgumentsForwarding:
9
9
  Style/CommentedKeyword:
10
10
  Enabled: false
11
11
 
12
+ Style/ClassAndModuleChildren:
13
+ Enabled: false
14
+
12
15
  Style/Documentation:
13
16
  Enabled: false
14
17
 
@@ -24,6 +27,9 @@ Style/StringLiterals:
24
27
  Style/StringLiteralsInInterpolation:
25
28
  Enabled: true
26
29
 
30
+ Style/SuperArguments:
31
+ Enabled: false
32
+
27
33
  Style/SymbolProc:
28
34
  Enabled: false
29
35
 
@@ -48,6 +54,9 @@ Metrics/AbcSize:
48
54
  Metrics/BlockLength:
49
55
  Enabled: false
50
56
 
57
+ Metrics/ClassLength:
58
+ Enabled: false
59
+
51
60
  Metrics/MethodLength:
52
61
  Enabled: false
53
62
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.1] - 2024-12-23
4
+
5
+ - refactor: reorganize the overall structure of the test. Change from rspec to minitest
6
+ - ci: fix CI for rails new version.
7
+
8
+ ## [0.6.0] - 2024-11-11
9
+
10
+ - refactor: limit the scope of methods needed only for internal library purposes.
11
+ - support rails 8.0.x
12
+ - add optional value `if` to exclude from save (or destroy).
13
+
3
14
  ## [0.5.0] - 2024-10-09
4
15
 
5
16
  - remove `:context` option. use `:destroy` option instead.
data/README.md CHANGED
@@ -37,7 +37,7 @@ If the callbacks are written in a class that inherits from `ActiveRecordCompose:
37
37
  class AccountRegistration < ActiveRecordCompose::Model
38
38
  def initialize(account = Account.new, attributes = {})
39
39
  @account = account
40
- super(attributes)
40
+ super(attributes) # When overrides `#initialize`, be sure to call `super`.
41
41
 
42
42
  # By including AR instance in models, AR instance itself is saved when this model is saved.
43
43
  models.push(account)
@@ -7,9 +7,11 @@ module ActiveRecordCompose
7
7
  # @param model [Object] the model instance.
8
8
  # @param destroy [Boolean] given true, destroy model.
9
9
  # @param destroy [Proc] when proc returning true, destroy model.
10
- def initialize(model, destroy: false)
10
+ # @param if [Proc] evaluation result is false, it will not be included in the renewal.
11
+ def initialize(model, destroy: false, if: nil)
11
12
  @model = model
12
13
  @destroy_context_type = destroy
14
+ @if_option = binding.local_variable_get(:if)
13
15
  end
14
16
 
15
17
  delegate :errors, to: :model
@@ -38,6 +40,22 @@ module ActiveRecordCompose
38
40
  end
39
41
  end
40
42
 
43
+ # Returns a boolean indicating whether or not to exclude the user from the update.
44
+ #
45
+ # @return [Boolean] if true, exclude from update.
46
+ def ignore?
47
+ i = if_option
48
+ if i.nil?
49
+ false
50
+ elsif i.arity == 0
51
+ # @type var i: ^() -> bool
52
+ !i.call
53
+ else
54
+ # @type var i: ^(_ARLike) -> bool
55
+ !i.call(model)
56
+ end
57
+ end
58
+
41
59
  # Execute save or destroy. Returns true on success, false on failure.
42
60
  # Whether save or destroy is executed depends on the value of `#destroy_context?`.
43
61
  #
@@ -61,20 +79,31 @@ module ActiveRecordCompose
61
79
  # @return [Boolean]
62
80
  def ==(other)
63
81
  return false unless self.class == other.class
64
- return false unless __raw_model == other.__raw_model # steep:ignore
82
+ return false unless model == other.model
65
83
 
66
84
  true
67
85
  end
68
86
 
69
- # @private
70
- # Returns a model instance of raw, but it should
71
- # be noted that application developers are not expected to use this interface.
72
- #
73
- # @return [Object] raw model instance
74
- def __raw_model = model
87
+ protected
88
+
89
+ attr_reader :model
75
90
 
76
91
  private
77
92
 
78
- attr_reader :model, :destroy_context_type
93
+ attr_reader :destroy_context_type, :if_option
94
+
95
+ # @private
96
+ # steep:ignore:start
97
+ module PackagePrivate
98
+ refine InnerModel do
99
+ # @private
100
+ # Returns a model instance of raw, but it should
101
+ # be noted that application developers are not expected to use this interface.
102
+ #
103
+ # @return [Object] raw model instance
104
+ def __raw_model = model
105
+ end
106
+ end
107
+ # steep:ignore:end
79
108
  end
80
109
  end
@@ -3,6 +3,8 @@
3
3
  require 'active_record_compose/inner_model'
4
4
 
5
5
  module ActiveRecordCompose
6
+ using InnerModel::PackagePrivate # steep:ignore
7
+
6
8
  class InnerModelCollection
7
9
  include Enumerable
8
10
 
@@ -38,9 +40,11 @@ module ActiveRecordCompose
38
40
  # @param destroy [Boolean] given true, destroy model.
39
41
  # @param destroy [Proc] when proc returning true, destroy model.
40
42
  # @param destroy [Symbol] applies boolean value of result of sending a message to `owner` to evaluation.
43
+ # @param if [Proc] evaluation result is false, it will not be included in the renewal.
44
+ # @param if [Symbol] applies boolean value of result of sending a message to `owner` to evaluation.
41
45
  # @return [self] returns itself.
42
- def push(model, destroy: false)
43
- models << wrap(model, destroy:)
46
+ def push(model, destroy: false, if: nil)
47
+ models << wrap(model, destroy:, if:)
44
48
  self
45
49
  end
46
50
 
@@ -70,25 +74,11 @@ module ActiveRecordCompose
70
74
  self
71
75
  end
72
76
 
73
- # @private
74
- # Enumerates model objects, but it should be noted that
75
- # application developers are not expected to use this interface.
76
- #
77
- # @yieldparam [InnerModel] rawpped model instance.
78
- # @return [Enumerator] when not block given.
79
- # @return [self] when block given, returns itself.
80
- def __each_by_wrapped
81
- return enum_for(:__each_by_wrapped) unless block_given?
82
-
83
- models.each { yield _1 if _1.__raw_model } # steep:ignore
84
- self
85
- end
86
-
87
77
  private
88
78
 
89
79
  attr_reader :owner, :models
90
80
 
91
- def wrap(model, destroy: false)
81
+ def wrap(model, destroy: false, if: nil)
92
82
  if model.is_a?(ActiveRecordCompose::InnerModel) # steep:ignore
93
83
  # @type var model: ActiveRecordCompose::InnerModel
94
84
  model
@@ -97,9 +87,26 @@ module ActiveRecordCompose
97
87
  method = destroy
98
88
  destroy = -> { owner.__send__(method) }
99
89
  end
100
- # @type var model: ActiveRecordCompose::_ARLike
101
- ActiveRecordCompose::InnerModel.new(model, destroy:)
90
+ if_option = binding.local_variable_get(:if)
91
+ if if_option.is_a?(Symbol)
92
+ method = if_option
93
+ if_option = -> { owner.__send__(method) }
94
+ end
95
+ ActiveRecordCompose::InnerModel.new(model, destroy:, if: if_option)
96
+ end
97
+ end
98
+
99
+ # @private
100
+ # steep:ignore:start
101
+ module PackagePrivate
102
+ refine InnerModelCollection do
103
+ # Returns array of wrapped model instance.
104
+ #
105
+ # @private
106
+ # @return [Array[InnerModel] array of wrapped model instance.
107
+ def __wrapped_models = models.reject { _1.ignore? }.select { _1.__raw_model }
102
108
  end
103
109
  end
110
+ # steep:ignore:end
104
111
  end
105
112
  end
@@ -5,6 +5,8 @@ require 'active_record_compose/inner_model_collection'
5
5
  require 'active_record_compose/transaction_support'
6
6
 
7
7
  module ActiveRecordCompose
8
+ using InnerModelCollection::PackagePrivate # steep:ignore
9
+
8
10
  class Model
9
11
  include ActiveModel::Model
10
12
  include ActiveModel::Validations::Callbacks
@@ -150,11 +152,15 @@ module ActiveRecordCompose
150
152
 
151
153
  def models = @__models ||= ActiveRecordCompose::InnerModelCollection.new(self)
152
154
 
153
- def wrapped_models = models.__each_by_wrapped # steep:ignore
154
-
155
- def validate_models = wrapped_models.select { _1.invalid? }.each { errors.merge!(_1) }
155
+ def validate_models
156
+ wms = models.__wrapped_models # steep:ignore
157
+ wms.select { _1.invalid? }.each { errors.merge!(_1) }
158
+ end
156
159
 
157
- def save_models(bang:) = wrapped_models.all? { bang ? _1.save! : _1.save }
160
+ def save_models(bang:)
161
+ wms = models.__wrapped_models # steep:ignore
162
+ wms.all? { bang ? _1.save! : _1.save }
163
+ end
158
164
 
159
165
  def raise_validation_error = raise ActiveRecord::RecordInvalid, self
160
166
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordCompose
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.1'
5
5
  end
@@ -12,10 +12,12 @@ module ActiveRecordCompose
12
12
  def invalid?: -> bool
13
13
  def valid?: -> bool
14
14
  def errors: -> untyped
15
+ def ==: (untyped) -> bool
15
16
  end
16
17
 
17
18
  type attribute_name = (String | Symbol)
18
19
  type destroy_context_type = (bool | Symbol | (^() -> boolish) | (^(_ARLike) -> boolish))
20
+ type condition_type = ((^() -> boolish) | (^(_ARLike) -> boolish))
19
21
 
20
22
  module DelegateAttribute
21
23
  extend ActiveSupport::Concern
@@ -33,7 +35,7 @@ module ActiveRecordCompose
33
35
  def initialize: (Model) -> void
34
36
  def each: () { (_ARLike) -> void } -> InnerModelCollection | () -> Enumerator[_ARLike, self]
35
37
  def <<: (_ARLike) -> self
36
- def push: (_ARLike, ?destroy: destroy_context_type) -> self
38
+ def push: (_ARLike, ?destroy: destroy_context_type, ?if: (nil | Symbol | condition_type)) -> self
37
39
  def empty?: -> bool
38
40
  def clear: -> self
39
41
  def delete: (_ARLike | InnerModel) -> InnerModelCollection?
@@ -41,12 +43,13 @@ module ActiveRecordCompose
41
43
  private
42
44
  attr_reader owner: Model
43
45
  attr_reader models: Array[InnerModel]
44
- def wrap: (_ARLike | InnerModel, ?destroy: destroy_context_type) -> InnerModel
46
+ def wrap: (_ARLike | InnerModel, ?destroy: destroy_context_type, ?if: (nil | Symbol | condition_type)) -> InnerModel
45
47
  end
46
48
 
47
49
  class InnerModel
48
- def initialize: (_ARLike, ?destroy: destroy_context_type) -> void
50
+ def initialize: (_ARLike, ?destroy: destroy_context_type, ?if: (nil | condition_type)) -> void
49
51
  def destroy_context?: -> bool
52
+ def ignore?: -> bool
50
53
  def save: -> bool
51
54
  def save!: -> untyped
52
55
  def invalid?: -> bool
@@ -56,6 +59,7 @@ module ActiveRecordCompose
56
59
  private
57
60
  attr_reader model: _ARLike
58
61
  attr_reader destroy_context_type: destroy_context_type
62
+ attr_reader if_option: (nil | condition_type)
59
63
  end
60
64
 
61
65
  class Model
@@ -81,7 +85,6 @@ module ActiveRecordCompose
81
85
 
82
86
  private
83
87
  def models: -> InnerModelCollection
84
- def wrapped_models: -> Enumerator[InnerModel, InnerModelCollection]
85
88
  def validate_models: -> void
86
89
  def save_models: (bang: bool) -> bool
87
90
  def raise_validation_error: -> bot
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hamajyotan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-09 00:00:00.000000000 Z
11
+ date: 2024-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -52,7 +52,7 @@ metadata:
52
52
  homepage_uri: https://github.com/hamajyotan/active_record_compose
53
53
  source_code_uri: https://github.com/hamajyotan/active_record_compose
54
54
  changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
55
- documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.5.0
55
+ documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.6.1
56
56
  rubygems_mfa_required: 'true'
57
57
  post_install_message:
58
58
  rdoc_options: []
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.5.11
72
+ rubygems_version: 3.5.21
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: activemodel form object pattern