active_record_compose 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +9 -0
- data/CHANGELOG.md +11 -0
- data/lib/active_record_compose/delegate_attribute.rb +14 -7
- data/lib/active_record_compose/inner_model.rb +14 -3
- data/lib/active_record_compose/inner_model_collection.rb +5 -3
- data/lib/active_record_compose/model.rb +3 -17
- data/lib/active_record_compose/transaction_support.rb +33 -0
- data/lib/active_record_compose/version.rb +1 -1
- data/sig/active_record_compose.rbs +28 -20
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d71146cd07fcae3e2ba02f5a3130d3eab0cba06dc405584793fa30d4add22ad
|
4
|
+
data.tar.gz: 88e70c5d2c6f8327c03c658304d6b036636f9b9ef4f342a45dd2afc7cc1cda4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76b5dd94c5246ca424564721a9f185488c1d51dcb12177a08c1d103110e05fd510b87f1cb2811256ab1d0d30425f327e8e1179e4c7e62ea3377fd155a708be9e
|
7
|
+
data.tar.gz: 7e03b2ea7f423047f5eda1ea89b80b586d7ab3c4ad7d616562a6aa9a555e846336f67f30e5e4b45a28e68913091ea5f3e00aee360f22c0689263380b734d0f3f
|
data/.rubocop.yml
CHANGED
@@ -6,9 +6,15 @@ AllCops:
|
|
6
6
|
Style/ArgumentsForwarding:
|
7
7
|
Enabled: false
|
8
8
|
|
9
|
+
Style/CommentedKeyword:
|
10
|
+
Enabled: false
|
11
|
+
|
9
12
|
Style/Documentation:
|
10
13
|
Enabled: false
|
11
14
|
|
15
|
+
Style/NumericPredicate:
|
16
|
+
Enabled: false
|
17
|
+
|
12
18
|
Style/StringLiterals:
|
13
19
|
Enabled: true
|
14
20
|
|
@@ -27,6 +33,9 @@ Style/TrailingCommaInArrayLiteral:
|
|
27
33
|
Style/TrailingCommaInHashLiteral:
|
28
34
|
EnforcedStyleForMultiline: comma
|
29
35
|
|
36
|
+
Layout/LeadingCommentSpace:
|
37
|
+
Enabled: false
|
38
|
+
|
30
39
|
Layout/LineLength:
|
31
40
|
Max: 120
|
32
41
|
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.2] - 2024-04-10
|
4
|
+
|
5
|
+
- support `ActiveRecord::Base#with_connection`
|
6
|
+
- rbs maintained.
|
7
|
+
- relax context proc arity.
|
8
|
+
|
9
|
+
## [0.3.1] - 2024-03-17
|
10
|
+
|
11
|
+
- purge nodoc definitions from type signature
|
12
|
+
- support `ActiveRecord::Base#lease_connection`
|
13
|
+
|
3
14
|
## [0.3.0] - 2024-02-24
|
4
15
|
|
5
16
|
- strictify type checking
|
@@ -38,23 +38,26 @@ module ActiveRecordCompose
|
|
38
38
|
extend ActiveSupport::Concern
|
39
39
|
|
40
40
|
included do
|
41
|
-
class_attribute :delegated_attributes, instance_writer: false
|
41
|
+
__skip__ = class_attribute :delegated_attributes, instance_writer: false
|
42
42
|
end
|
43
43
|
|
44
|
-
|
44
|
+
module ClassMethods
|
45
45
|
# Defines the reader and writer for the specified attribute.
|
46
46
|
#
|
47
47
|
def delegate_attribute(*attributes, to:, **options)
|
48
48
|
delegates = attributes.flat_map do |attribute|
|
49
|
-
reader = attribute
|
49
|
+
reader = attribute.to_s
|
50
50
|
writer = "#{attribute}="
|
51
51
|
|
52
52
|
[reader, writer]
|
53
53
|
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
__skip__ =
|
56
|
+
begin
|
57
|
+
delegate(*delegates, to:, **options)
|
58
|
+
delegated_attributes = (self.delegated_attributes ||= [])
|
59
|
+
attributes.each { delegated_attributes.push(_1.to_s) }
|
60
|
+
end
|
58
61
|
end
|
59
62
|
end
|
60
63
|
|
@@ -64,7 +67,11 @@ module ActiveRecordCompose
|
|
64
67
|
# @return [Hash] hash with the attribute name as key and the attribute value as value.
|
65
68
|
def attributes
|
66
69
|
attrs = __skip__ = defined?(super) ? super : {}
|
67
|
-
|
70
|
+
delegates = __skip__ = delegated_attributes
|
71
|
+
|
72
|
+
# @type var attrs: Hash[String, untyped]
|
73
|
+
# @type var delegates: Array[String]
|
74
|
+
attrs.merge(delegates.to_h { [_1, public_send(_1)] })
|
68
75
|
end
|
69
76
|
end
|
70
77
|
end
|
@@ -15,9 +15,20 @@ module ActiveRecordCompose
|
|
15
15
|
delegate :errors, to: :model
|
16
16
|
|
17
17
|
# @return [Symbol] :save or :destroy
|
18
|
-
def context
|
18
|
+
def context #: ActiveRecordCompose::context
|
19
19
|
c = @context
|
20
|
-
ret =
|
20
|
+
ret =
|
21
|
+
if c.is_a?(Proc)
|
22
|
+
if c.arity == 0
|
23
|
+
# @type var c: ^() -> context
|
24
|
+
c.call
|
25
|
+
else
|
26
|
+
# @type var c: ^(_ARLike) -> context
|
27
|
+
c.call(model)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
c
|
31
|
+
end
|
21
32
|
ret.presence_in(%i[save destroy]) || :save
|
22
33
|
end
|
23
34
|
|
@@ -65,7 +76,7 @@ module ActiveRecordCompose
|
|
65
76
|
# @return [Boolean]
|
66
77
|
def ==(other)
|
67
78
|
return false unless self.class == other.class
|
68
|
-
return false unless __raw_model == other.__raw_model
|
79
|
+
return false unless (__skip__ = (__raw_model == other.__raw_model))
|
69
80
|
return false unless context == other.context
|
70
81
|
|
71
82
|
true
|
@@ -14,7 +14,7 @@ module ActiveRecordCompose
|
|
14
14
|
def each
|
15
15
|
return enum_for(:each) unless block_given?
|
16
16
|
|
17
|
-
models.each { yield _1.__raw_model }
|
17
|
+
__skip__ = models.each { yield _1.__raw_model }
|
18
18
|
self
|
19
19
|
end
|
20
20
|
|
@@ -73,7 +73,7 @@ module ActiveRecordCompose
|
|
73
73
|
def __each_by_wrapped
|
74
74
|
return enum_for(:__each_by_wrapped) unless block_given?
|
75
75
|
|
76
|
-
models.each { yield _1 if _1.__raw_model }
|
76
|
+
__skip__ = models.each { yield _1 if _1.__raw_model }
|
77
77
|
self
|
78
78
|
end
|
79
79
|
|
@@ -82,9 +82,11 @@ module ActiveRecordCompose
|
|
82
82
|
def models = @models ||= []
|
83
83
|
|
84
84
|
def wrap(model, context:)
|
85
|
-
if model.is_a?(ActiveRecordCompose::InnerModel)
|
85
|
+
if (__skip__ = model.is_a?(ActiveRecordCompose::InnerModel))
|
86
|
+
# @type var model: ActiveRecordCompose::InnerModel
|
86
87
|
model
|
87
88
|
else
|
89
|
+
# @type var model: ActiveRecordCompose::_ARLike
|
88
90
|
ActiveRecordCompose::InnerModel.new(model, context:)
|
89
91
|
end
|
90
92
|
end
|
@@ -2,15 +2,16 @@
|
|
2
2
|
|
3
3
|
require 'active_record_compose/delegate_attribute'
|
4
4
|
require 'active_record_compose/inner_model_collection'
|
5
|
+
require 'active_record_compose/transaction_support'
|
5
6
|
|
6
7
|
module ActiveRecordCompose
|
7
8
|
class Model
|
8
9
|
include ActiveModel::Model
|
9
10
|
include ActiveModel::Validations::Callbacks
|
10
11
|
include ActiveModel::Attributes
|
11
|
-
include ActiveRecord::Transactions
|
12
12
|
|
13
13
|
include ActiveRecordCompose::DelegateAttribute
|
14
|
+
include ActiveRecordCompose::TransactionSupport
|
14
15
|
|
15
16
|
define_model_callbacks :save
|
16
17
|
define_model_callbacks :create
|
@@ -18,12 +19,6 @@ module ActiveRecordCompose
|
|
18
19
|
|
19
20
|
validate :validate_models
|
20
21
|
|
21
|
-
# for ActiveRecord::Transactions
|
22
|
-
class << self
|
23
|
-
def connection = ActiveRecord::Base.connection
|
24
|
-
__skip__ = def composite_primary_key? = false
|
25
|
-
end
|
26
|
-
|
27
22
|
def initialize(attributes = {})
|
28
23
|
__skip__ = super(attributes)
|
29
24
|
end
|
@@ -151,20 +146,11 @@ module ActiveRecordCompose
|
|
151
146
|
end || raise_on_save_error
|
152
147
|
end
|
153
148
|
|
154
|
-
# for ActiveRecord::Transactions
|
155
|
-
__skip__ = def id = nil
|
156
|
-
|
157
|
-
# for ActiveRecord::Transactions
|
158
|
-
__skip__ = def trigger_transactional_callbacks? = true
|
159
|
-
|
160
|
-
# for ActiveRecord::Transactions
|
161
|
-
__skip__ = def restore_transaction_record_state(_force_restore_state) = nil
|
162
|
-
|
163
149
|
private
|
164
150
|
|
165
151
|
def models = @__models ||= ActiveRecordCompose::InnerModelCollection.new
|
166
152
|
|
167
|
-
def wrapped_models = models.__each_by_wrapped
|
153
|
+
def wrapped_models = (__skip__ = models.__each_by_wrapped)
|
168
154
|
|
169
155
|
def validate_models = wrapped_models.select { _1.invalid? }.each { errors.merge!(_1) }
|
170
156
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecordCompose
|
4
|
+
module TransactionSupport
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
include ActiveRecord::Transactions
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def lease_connection
|
10
|
+
if ar_class.respond_to?(:lease_connection)
|
11
|
+
__skip__ = ar_class.lease_connection
|
12
|
+
else
|
13
|
+
ar_class.connection
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def connection = ar_class.connection
|
18
|
+
|
19
|
+
def with_connection(&) = __skip__ = ar_class.with_connection(&)
|
20
|
+
|
21
|
+
def composite_primary_key? = false
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def ar_class = ActiveRecord::Base
|
26
|
+
end
|
27
|
+
|
28
|
+
def id = nil
|
29
|
+
|
30
|
+
def trigger_transactional_callbacks? = true
|
31
|
+
def restore_transaction_record_state(_force_restore_state = false) = nil # rubocop:disable Style/OptionalBooleanParameter
|
32
|
+
end
|
33
|
+
end
|
@@ -5,7 +5,6 @@ module ActiveRecordCompose
|
|
5
5
|
VERSION: String
|
6
6
|
|
7
7
|
interface _ARLike
|
8
|
-
def is_a?: (untyped) -> bool
|
9
8
|
def save: -> bool
|
10
9
|
def save!: -> untyped
|
11
10
|
def destroy: -> bool
|
@@ -13,24 +12,20 @@ module ActiveRecordCompose
|
|
13
12
|
def invalid?: -> bool
|
14
13
|
def valid?: -> bool
|
15
14
|
def errors: -> untyped
|
16
|
-
def ==: (untyped) -> bool
|
17
15
|
end
|
18
16
|
|
19
17
|
type attribute_name = (String | Symbol)
|
20
|
-
type
|
21
|
-
type context_proc = ^(_ARLike) ->
|
22
|
-
type context = context_types | context_proc
|
18
|
+
type context = (:save | :destroy)
|
19
|
+
type context_proc = ((^() -> context) | (^(_ARLike) -> context))
|
23
20
|
|
24
21
|
module DelegateAttribute
|
25
22
|
extend ActiveSupport::Concern
|
26
23
|
|
27
|
-
def delegated_attribute: -> Array[untyped]
|
28
24
|
def attributes: -> Hash[String, untyped]
|
29
|
-
def delegated_attributes: -> Array[untyped]
|
30
|
-
def delegated_attributes=: (Array[untyped]) -> void
|
31
25
|
|
32
|
-
|
33
|
-
|
26
|
+
module ClassMethods
|
27
|
+
def delegate_attribute: (*untyped methods, to: untyped?, **untyped) -> untyped
|
28
|
+
end
|
34
29
|
end
|
35
30
|
|
36
31
|
class InnerModelCollection
|
@@ -39,28 +34,26 @@ module ActiveRecordCompose
|
|
39
34
|
|
40
35
|
def each: () { (_ARLike) -> void } -> InnerModelCollection | () -> Enumerator[_ARLike, self]
|
41
36
|
def <<: (_ARLike) -> self
|
42
|
-
def push: (_ARLike, ?context: context) -> self
|
37
|
+
def push: (_ARLike, ?context: (context | context_proc)) -> self
|
43
38
|
def empty?: -> bool
|
44
39
|
def clear: -> self
|
45
|
-
def delete: (_ARLike | InnerModel, ?context:
|
46
|
-
def __each_by_wrapped: () { (InnerModel) -> void } -> InnerModelCollection | () -> Enumerator[InnerModel, self]
|
40
|
+
def delete: (_ARLike | InnerModel, ?context: (context | context_proc)) -> InnerModelCollection?
|
47
41
|
|
48
42
|
private
|
49
43
|
def models: -> Array[InnerModel]
|
50
|
-
def wrap: (_ARLike | InnerModel, context: context) -> InnerModel
|
44
|
+
def wrap: (_ARLike | InnerModel, context: (context | context_proc)) -> InnerModel
|
51
45
|
end
|
52
46
|
|
53
47
|
class InnerModel
|
54
|
-
@context: context
|
48
|
+
@context: (context | context_proc)
|
55
49
|
|
56
|
-
def initialize: (_ARLike, ?context: context) -> void
|
50
|
+
def initialize: (_ARLike, ?context: (context | context_proc)) -> void
|
57
51
|
def context: -> context
|
58
52
|
def save: -> bool
|
59
53
|
def save!: -> untyped
|
60
54
|
def invalid?: -> bool
|
61
55
|
def valid?: -> bool
|
62
56
|
def ==: (untyped) -> bool
|
63
|
-
def __raw_model: -> _ARLike
|
64
57
|
|
65
58
|
private
|
66
59
|
attr_reader model: _ARLike
|
@@ -70,9 +63,12 @@ module ActiveRecordCompose
|
|
70
63
|
extend ActiveModel::Callbacks
|
71
64
|
include ActiveModel::Model
|
72
65
|
include ActiveModel::Validations::Callbacks
|
66
|
+
extend ActiveModel::Validations::ClassMethods
|
73
67
|
include ActiveModel::Attributes
|
74
|
-
include ActiveRecord::Transactions
|
75
68
|
include DelegateAttribute
|
69
|
+
extend DelegateAttribute::ClassMethods
|
70
|
+
include TransactionSupport
|
71
|
+
extend TransactionSupport::ClassMethods
|
76
72
|
|
77
73
|
@__models: InnerModelCollection
|
78
74
|
|
@@ -92,8 +88,20 @@ module ActiveRecordCompose
|
|
92
88
|
def raise_validation_error: -> bot
|
93
89
|
def raise_on_save_error: -> bot
|
94
90
|
def raise_on_save_error_message: -> String
|
91
|
+
end
|
92
|
+
|
93
|
+
module TransactionSupport
|
94
|
+
include ActiveRecord::Transactions
|
95
|
+
|
96
|
+
def id: -> untyped
|
97
|
+
|
98
|
+
module ClassMethods
|
99
|
+
def connection: -> ActiveRecord::ConnectionAdapters::AbstractAdapter
|
100
|
+
def lease_connection: -> ActiveRecord::ConnectionAdapters::AbstractAdapter
|
101
|
+
def with_connection: [T] () { () -> T } -> T
|
95
102
|
|
96
|
-
|
97
|
-
|
103
|
+
private
|
104
|
+
def ar_class: -> singleton(ActiveRecord::Base)
|
105
|
+
end
|
98
106
|
end
|
99
107
|
end
|
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.3.
|
4
|
+
version: 0.3.2
|
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-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- lib/active_record_compose/inner_model.rb
|
43
43
|
- lib/active_record_compose/inner_model_collection.rb
|
44
44
|
- lib/active_record_compose/model.rb
|
45
|
+
- lib/active_record_compose/transaction_support.rb
|
45
46
|
- lib/active_record_compose/version.rb
|
46
47
|
- sig/active_record_compose.rbs
|
47
48
|
homepage: https://github.com/hamajyotan/active_record_compose
|
@@ -51,7 +52,7 @@ metadata:
|
|
51
52
|
homepage_uri: https://github.com/hamajyotan/active_record_compose
|
52
53
|
source_code_uri: https://github.com/hamajyotan/active_record_compose
|
53
54
|
changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
|
54
|
-
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.3.
|
55
|
+
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.3.2
|
55
56
|
rubygems_mfa_required: 'true'
|
56
57
|
post_install_message:
|
57
58
|
rdoc_options: []
|