active_record_compose 0.4.0 → 0.4.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: 8d9e12c3cb3d2e8571d418957576a8cf5b5d3dbedb9e83e2bcd25f172def43f3
4
- data.tar.gz: a991715192e6cb62e8b794860422667811b1fb1f985089e415b444f0b7bc0d4a
3
+ metadata.gz: 5fe788a613275af9e9784ba51b84af314792e0a5761e0c8d5e985602dfe7d586
4
+ data.tar.gz: 0621ff28f43377240efc94a97b12ffde861e71142f6ba2ac349d4d9a165d6401
5
5
  SHA512:
6
- metadata.gz: 837b3d35c903eb0ff4400e5c83c438255d9ee02515407792fd19d24ab9a5a63cea1f9faa1d12a5fd5184d52fee2d662625e7c0d0b2a79e27ebb0866054a6b2a4
7
- data.tar.gz: e245d8380a2d20f0c9bbbc1f24712f1d7982697bf90b172886dcd2c9bd666ace8ddd3b29a20b9bcc709d8a236a1395a41c0f53ccdb3869f66a527d5ad6be08dc
6
+ metadata.gz: d67bf25365ccad6f049a17cbc311cbf19c48ee4719e51afefc149fb0227989ec8f1c55684ab26328839ae7c79897fed27ac6b5c7db90c42bf0b81e3bc4437c97
7
+ data.tar.gz: ab71a4b2bd61619136f8685b0b1346d4ad111fec0717a1527ce714985f5eaee91f0c91dd9713ba835a2633acbdc2a197f2ae2c77767d2028b13eb4bcb5f21887
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.1] - 2024-09-20
4
+
5
+ - Omitted optional argument for `InnerModelCollection#destroy`.
6
+ `InnerModel` equivalence is always performed based on the instance of the inner `model`.
7
+ Since there are no use cases that depend on the original behavior.
8
+
3
9
  ## [0.4.0] - 2024-09-15
4
10
 
5
11
  - support `destrpy` option. and deprecated `context` option.
@@ -7,10 +7,9 @@ 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(owner, model, destroy: false, context: nil)
11
- @owner = owner
10
+ def initialize(model, destroy: false, context: nil)
12
11
  @model = model
13
- @destroy =
12
+ @destroy_context_type =
14
13
  if context
15
14
  c = context
16
15
 
@@ -55,17 +54,15 @@ module ActiveRecordCompose
55
54
  delegate :errors, to: :model
56
55
 
57
56
  def destroy_context?
58
- d = destroy
57
+ d = destroy_context_type
59
58
  if d.is_a?(Proc)
60
59
  if d.arity == 0
61
60
  # @type var d: ^() -> (bool | context)
62
- d.call
61
+ !!d.call
63
62
  else
64
63
  # @type var d: ^(_ARLike) -> (bool | context)
65
- d.call(model)
64
+ !!d.call(model)
66
65
  end
67
- elsif d.is_a?(Symbol)
68
- owner.send(d)
69
66
  else
70
67
  !!d
71
68
  end
@@ -94,28 +91,21 @@ module ActiveRecordCompose
94
91
  # @return [Boolean]
95
92
  def ==(other)
96
93
  return false unless self.class == other.class
97
- return false unless __raw_model == other.__raw_model
98
- return false unless __destroy == other.__destroy
94
+ return false unless __raw_model == other.__raw_model # steep:ignore
99
95
 
100
96
  true
101
97
  end
102
98
 
99
+ # @private
103
100
  # Returns a model instance of raw, but it should
104
101
  # be noted that application developers are not expected to use this interface.
105
102
  #
106
103
  # @return [Object] raw model instance
107
104
  def __raw_model = model
108
105
 
109
- # Returns a model instance of raw, but it should
110
- # be noted that application developers are not expected to use this interface.
111
- #
112
- # @return [Boolean] raw destroy instance
113
- # @return [Proc] raw destroy instance
114
- def __destroy = destroy
115
-
116
106
  private
117
107
 
118
- attr_reader :owner, :model, :destroy
108
+ attr_reader :model, :destroy_context_type
119
109
 
120
110
  def deprecator
121
111
  if ActiveRecord.respond_to?(:deprecator)
@@ -8,6 +8,7 @@ module ActiveRecordCompose
8
8
 
9
9
  def initialize(owner)
10
10
  @owner = owner
11
+ @models = []
11
12
  end
12
13
 
13
14
  # Enumerates model objects.
@@ -35,7 +36,8 @@ module ActiveRecordCompose
35
36
  #
36
37
  # @param model [Object] the model instance
37
38
  # @param destroy [Boolean] given true, destroy model.
38
- # @param context [Symbol] :save or :destroy
39
+ # @param destroy [Proc] when proc returning true, destroy model.
40
+ # @param destroy [Symbol] applies boolean value of result of sending a message to `owner` to evaluation.
39
41
  # @return [self] returns itself.
40
42
  def push(model, destroy: false, context: nil)
41
43
  models << wrap(model, destroy:, context:)
@@ -59,17 +61,25 @@ module ActiveRecordCompose
59
61
  # Returns nil if the deletion fails, self if it succeeds.
60
62
  #
61
63
  # @param model [Object] the model instance
62
- # @param destroy [Boolean] given true, destroy model.
63
- # @param context [Symbol] :save or :destroy
64
64
  # @return [self] Successful deletion
65
65
  # @return [nil] If deletion fails
66
- def delete(model, destroy: false, context: nil)
67
- wrapped = wrap(model, destroy:, context:)
66
+ def delete(model, destroy: nil, context: nil)
67
+ if !destroy.nil? || !context.nil?
68
+ # steep:ignore:start
69
+ deprecator.warn(
70
+ 'In `InnerModelConnection#destroy`, the option values `destroy` and `context` are ignored. ' \
71
+ 'These options will be removed in 0.5.0.',
72
+ )
73
+ # steep:ignore:end
74
+ end
75
+
76
+ wrapped = wrap(model)
68
77
  return nil unless models.delete(wrapped)
69
78
 
70
79
  self
71
80
  end
72
81
 
82
+ # @private
73
83
  # Enumerates model objects, but it should be noted that
74
84
  # application developers are not expected to use this interface.
75
85
  #
@@ -85,17 +95,27 @@ module ActiveRecordCompose
85
95
 
86
96
  private
87
97
 
88
- attr_reader :owner
89
-
90
- def models = @models ||= []
98
+ attr_reader :owner, :models
91
99
 
92
- def wrap(model, destroy:, context: nil)
100
+ def wrap(model, destroy: false, context: nil)
93
101
  if model.is_a?(ActiveRecordCompose::InnerModel) # steep:ignore
94
102
  # @type var model: ActiveRecordCompose::InnerModel
95
103
  model
96
104
  else
105
+ if destroy.is_a?(Symbol)
106
+ method = destroy
107
+ destroy = -> { owner.__send__(method) }
108
+ end
97
109
  # @type var model: ActiveRecordCompose::_ARLike
98
- ActiveRecordCompose::InnerModel.new(owner, model, destroy:, context:)
110
+ ActiveRecordCompose::InnerModel.new(model, destroy:, context:)
111
+ end
112
+ end
113
+
114
+ def deprecator
115
+ if ActiveRecord.respond_to?(:deprecator)
116
+ ActiveRecord.deprecator # steep:ignore
117
+ else # for rails 7.0.x or lower
118
+ ActiveSupport::Deprecation
99
119
  end
100
120
  end
101
121
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordCompose
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
@@ -3,8 +3,6 @@
3
3
  require 'active_record'
4
4
 
5
5
  require_relative 'active_record_compose/version'
6
- require_relative 'active_record_compose/inner_model'
7
- require_relative 'active_record_compose/inner_model_collection'
8
6
  require_relative 'active_record_compose/model'
9
7
 
10
8
  module ActiveRecordCompose
@@ -17,6 +17,7 @@ module ActiveRecordCompose
17
17
  type attribute_name = (String | Symbol)
18
18
  type context = (:save | :destroy)
19
19
  type context_proc = ((^() -> context) | (^(_ARLike) -> context))
20
+ type destroy_context_type = (bool | Symbol | (^() -> boolish) | (^(_ARLike) -> boolish))
20
21
 
21
22
  module DelegateAttribute
22
23
  extend ActiveSupport::Concern
@@ -30,28 +31,23 @@ module ActiveRecordCompose
30
31
 
31
32
  class InnerModelCollection
32
33
  include ::Enumerable[_ARLike]
33
- @owner: Model
34
- @models: Array[InnerModel]
35
34
 
36
35
  def initialize: (Model) -> void
37
36
  def each: () { (_ARLike) -> void } -> InnerModelCollection | () -> Enumerator[_ARLike, self]
38
37
  def <<: (_ARLike) -> self
39
- def push: (_ARLike, ?destroy: bool, ?context: (nil | context | context_proc)) -> self
38
+ def push: (_ARLike, ?destroy: destroy_context_type, ?context: (nil | context | context_proc)) -> self
40
39
  def empty?: -> bool
41
40
  def clear: -> self
42
- def delete: (_ARLike | InnerModel, ?destroy: bool, ?context: (nil | context | context_proc)) -> InnerModelCollection?
41
+ def delete: (_ARLike | InnerModel, ?destroy: (nil | destroy_context_type), ?context: (nil | context | context_proc)) -> InnerModelCollection?
43
42
 
44
43
  private
45
44
  attr_reader owner: Model
46
- def models: -> Array[InnerModel]
47
- def wrap: (_ARLike | InnerModel, destroy: bool, ?context: (nil | context | context_proc)) -> InnerModel
45
+ attr_reader models: Array[InnerModel]
46
+ def wrap: (_ARLike | InnerModel, ?destroy: destroy_context_type, ?context: (nil | context | context_proc)) -> InnerModel
48
47
  end
49
48
 
50
49
  class InnerModel
51
- @owner: Model
52
- @context: (context | context_proc)
53
-
54
- def initialize: (Model, _ARLike, ?destroy: bool, ?context: (nil | context | context_proc)) -> void
50
+ def initialize: (_ARLike, ?destroy: destroy_context_type, ?context: (nil | context | context_proc)) -> void
55
51
  def destroy_context?: -> bool
56
52
  def save: -> bool
57
53
  def save!: -> untyped
@@ -60,8 +56,8 @@ module ActiveRecordCompose
60
56
  def ==: (untyped) -> bool
61
57
 
62
58
  private
63
- attr_reader owner: Model
64
59
  attr_reader model: _ARLike
60
+ attr_reader destroy_context_type: destroy_context_type
65
61
  end
66
62
 
67
63
  class Model
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.4.0
4
+ version: 0.4.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-09-14 00:00:00.000000000 Z
11
+ date: 2024-09-19 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.4.0
55
+ documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.4.1
56
56
  rubygems_mfa_required: 'true'
57
57
  post_install_message:
58
58
  rdoc_options: []