active_record_compose 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +6 -0
- data/lib/active_record_compose/inner_model.rb +38 -9
- data/lib/active_record_compose/inner_model_collection.rb +26 -19
- data/lib/active_record_compose/model.rb +10 -4
- data/lib/active_record_compose/version.rb +1 -1
- data/sig/active_record_compose.rbs +7 -4
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 701c396babd7d30d3e13779c5237bd1fbaf2fb719a75439154d17a167dc645dc
|
4
|
+
data.tar.gz: 72cf835561b903fc913e9feadff380f8ca986f621320b1fdd1ebd7dc341539cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fddabb884fb62e215cc5a67abe823b563cf5be201f2e05b8057871e2c188025e04d46b5953b84f23e7cdc064840291882cb3f77a93efb221bed3b0c99a6dfe71
|
7
|
+
data.tar.gz: 7b550abe98e5391de53a9b017b8ae214270844f8d6536dadb012587c1d553e4e01cab99f480ec59ae01eaf3d0b742c612ea676546cc7ce82ea945d0e99e0168d
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.6.0] - 2024-11-11
|
4
|
+
|
5
|
+
- refactor: limit the scope of methods needed only for internal library purposes.
|
6
|
+
- support rails 8.0.x
|
7
|
+
- add optional value `if` to exclude from save (or destroy).
|
8
|
+
|
3
9
|
## [0.5.0] - 2024-10-09
|
4
10
|
|
5
11
|
- remove `:context` option. use `:destroy` option instead.
|
@@ -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
|
-
|
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
|
82
|
+
return false unless model == other.model
|
65
83
|
|
66
84
|
true
|
67
85
|
end
|
68
86
|
|
69
|
-
|
70
|
-
|
71
|
-
|
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 :
|
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
|
-
|
101
|
-
|
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
|
154
|
-
|
155
|
-
|
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:)
|
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
|
|
@@ -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.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hamajyotan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-11-11 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.
|
55
|
+
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.6.0
|
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.
|
72
|
+
rubygems_version: 3.5.21
|
73
73
|
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: activemodel form object pattern
|