active_record_compose 0.1.5 → 0.1.7
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 -3
- data/CHANGELOG.md +12 -0
- data/lib/active_record_compose/delegate_attribute.rb +7 -1
- data/lib/active_record_compose/inner_model.rb +19 -2
- data/lib/active_record_compose/inner_model_collection.rb +6 -6
- data/lib/active_record_compose/model.rb +22 -9
- data/lib/active_record_compose/version.rb +1 -1
- metadata +3 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bff1c2fd72a225a89ada1c0cab86b6d32f31c0052d8857e665eb2796f4a40883
|
4
|
+
data.tar.gz: 307e8ecf50282b9a19e224870796944de47d37f8802d0ffe63cca465eb663cc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12f503a644d60932a095a587e8152d37af25b8eccfb0cc5cd32dc5941eb896104880328ff6e3ed2b7fd53ee78fad9f7962e23b5ffc1070bbaa00518e482dccf4
|
7
|
+
data.tar.gz: 91d912f794a250e44e8ad5fea2694f00a52eb98df3f217364e8eb637a6971692998526e86c2d908dc28ce7b0ff99ada394ca287b39342714377af63a1869c4be
|
data/.rubocop.yml
CHANGED
@@ -3,9 +3,6 @@ AllCops:
|
|
3
3
|
NewCops: enable
|
4
4
|
SuggestExtensions: false
|
5
5
|
|
6
|
-
Gemspec/DevelopmentDependencies:
|
7
|
-
Enabled: false
|
8
|
-
|
9
6
|
Style/Documentation:
|
10
7
|
Enabled: false
|
11
8
|
|
@@ -33,5 +30,8 @@ Layout/LineLength:
|
|
33
30
|
Metrics/BlockLength:
|
34
31
|
Enabled: false
|
35
32
|
|
33
|
+
Naming/BlockForwarding:
|
34
|
+
Enabled: false
|
35
|
+
|
36
36
|
Naming/MemoizedInstanceVariableName:
|
37
37
|
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.1.7] - 2024-01-15
|
4
|
+
|
5
|
+
- remove `add_development_dependency` from gemspec
|
6
|
+
- add spec for DelegateAttribute module
|
7
|
+
- add and refactor doc.
|
8
|
+
|
9
|
+
## [0.1.6] - 2024-01-14
|
10
|
+
|
11
|
+
- add doc for `#save` and `#save!`.
|
12
|
+
- implement `#save` for symmetry with `#save!`
|
13
|
+
- add `InnerModel#initialize` doc.
|
14
|
+
|
3
15
|
## [0.1.5] - 2024-01-11
|
4
16
|
|
5
17
|
- when invalid, raises ActiveRecord::RecordInvalid on #save!
|
@@ -42,6 +42,8 @@ module ActiveRecordCompose
|
|
42
42
|
end
|
43
43
|
|
44
44
|
class_methods do
|
45
|
+
# Defines the reader and writer for the specified attribute.
|
46
|
+
#
|
45
47
|
def delegate_attribute(*attributes, to:, **options)
|
46
48
|
delegates = attributes.flat_map do |attribute|
|
47
49
|
reader = attribute
|
@@ -56,8 +58,12 @@ module ActiveRecordCompose
|
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
61
|
+
# Returns a hash with the attribute name as key and the attribute value as value.
|
62
|
+
# Attributes declared with `delegate_attribute` are also merged.
|
63
|
+
#
|
64
|
+
# @return [Hash] hash with the attribute name as key and the attribute value as value.
|
59
65
|
def attributes
|
60
|
-
super.merge(delegated_attributes.to_h { [_1, public_send(_1)] })
|
66
|
+
(defined?(super) ? super : {}).merge(delegated_attributes.to_h { [_1, public_send(_1)] })
|
61
67
|
end
|
62
68
|
end
|
63
69
|
end
|
@@ -6,6 +6,7 @@ module ActiveRecordCompose
|
|
6
6
|
class InnerModel
|
7
7
|
# @param model [Object] the model instance.
|
8
8
|
# @param context [Symbol] :save or :destroy
|
9
|
+
# @param context [Proc] proc returning either :save or :destroy
|
9
10
|
def initialize(model, context: :save)
|
10
11
|
@model = model
|
11
12
|
@context = context
|
@@ -19,7 +20,22 @@ module ActiveRecordCompose
|
|
19
20
|
ret.presence_in(%i[save destroy]) || :save
|
20
21
|
end
|
21
22
|
|
22
|
-
#
|
23
|
+
# Execute save or destroy. Returns true on success, false on failure.
|
24
|
+
# Whether save or destroy is executed depends on the value of context.
|
25
|
+
#
|
26
|
+
# @return [Boolean] returns true on success, false on failure.
|
27
|
+
def save
|
28
|
+
case context
|
29
|
+
when :destroy
|
30
|
+
model.destroy
|
31
|
+
else
|
32
|
+
model.save
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Execute save or destroy. Unlike #save, an exception is raises on failure.
|
37
|
+
# Whether save or destroy is executed depends on the value of context.
|
38
|
+
#
|
23
39
|
def save!
|
24
40
|
case context
|
25
41
|
when :destroy
|
@@ -44,7 +60,8 @@ module ActiveRecordCompose
|
|
44
60
|
|
45
61
|
# Returns true if equivalent.
|
46
62
|
#
|
47
|
-
# @
|
63
|
+
# @param [Object] other
|
64
|
+
# @return [Boolean]
|
48
65
|
def ==(other)
|
49
66
|
return false unless self.class == other.class
|
50
67
|
return false unless __raw_model == other.__raw_model
|
@@ -10,7 +10,7 @@ module ActiveRecordCompose
|
|
10
10
|
#
|
11
11
|
# @yieldparam [Object] the model instance
|
12
12
|
# @return [Enumerator] when not block given.
|
13
|
-
# @return [
|
13
|
+
# @return [self] when block given, returns itself.
|
14
14
|
def each
|
15
15
|
return enum_for(:each) unless block_given?
|
16
16
|
|
@@ -21,7 +21,7 @@ module ActiveRecordCompose
|
|
21
21
|
# Appends model to collection.
|
22
22
|
#
|
23
23
|
# @param model [Object] the model instance
|
24
|
-
# @return [
|
24
|
+
# @return [self] returns itself.
|
25
25
|
def <<(model)
|
26
26
|
models << wrap(model, context: :save)
|
27
27
|
self
|
@@ -31,7 +31,7 @@ module ActiveRecordCompose
|
|
31
31
|
#
|
32
32
|
# @param model [Object] the model instance
|
33
33
|
# @param context [Symbol] :save or :destroy
|
34
|
-
# @return [
|
34
|
+
# @return [self] returns itself.
|
35
35
|
def push(model, context: :save)
|
36
36
|
models << wrap(model, context:)
|
37
37
|
self
|
@@ -44,7 +44,7 @@ module ActiveRecordCompose
|
|
44
44
|
|
45
45
|
# Set to empty.
|
46
46
|
#
|
47
|
-
# @return [
|
47
|
+
# @return [self] returns itself.
|
48
48
|
def clear
|
49
49
|
models.clear
|
50
50
|
self
|
@@ -55,7 +55,7 @@ module ActiveRecordCompose
|
|
55
55
|
#
|
56
56
|
# @param model [Object] the model instance
|
57
57
|
# @param context [Symbol] :save or :destroy
|
58
|
-
# @return [
|
58
|
+
# @return [self] Successful deletion
|
59
59
|
# @return [nil] If deletion fails
|
60
60
|
def delete(model, context: :save)
|
61
61
|
wrapped = wrap(model, context:)
|
@@ -69,7 +69,7 @@ module ActiveRecordCompose
|
|
69
69
|
#
|
70
70
|
# @yieldparam [InnerModel] rawpped model instance.
|
71
71
|
# @return [Enumerator] when not block given.
|
72
|
-
# @return [
|
72
|
+
# @return [self] when block given, returns itself.
|
73
73
|
def __each_by_wrapped
|
74
74
|
return enum_for(:__each_by_wrapped) unless block_given?
|
75
75
|
|
@@ -22,16 +22,27 @@ module ActiveRecordCompose
|
|
22
22
|
super(attributes)
|
23
23
|
end
|
24
24
|
|
25
|
+
# Save the models that exist in models.
|
26
|
+
# Returns false if any of the targets fail, true if all succeed.
|
27
|
+
#
|
28
|
+
# The save is performed within a single transaction.
|
29
|
+
#
|
30
|
+
# @return [Boolean] returns true on success, false on failure.
|
25
31
|
def save
|
26
32
|
return false if invalid?
|
27
33
|
|
28
|
-
save_in_transaction {
|
34
|
+
save_in_transaction { save_models }
|
29
35
|
end
|
30
36
|
|
37
|
+
# Save the models that exist in models.
|
38
|
+
# Unlike #save, an exception is raises on failure.
|
39
|
+
#
|
40
|
+
# Saving, like `#save`, is performed within a single transaction.
|
41
|
+
#
|
31
42
|
def save!
|
32
43
|
valid? || raise_validation_error
|
33
44
|
|
34
|
-
save_in_transaction {
|
45
|
+
save_in_transaction { save_models } || raise_on_save_error
|
35
46
|
end
|
36
47
|
|
37
48
|
# Behavior is same to `#save`, but `before_create` and `after_create` hooks fires.
|
@@ -63,7 +74,7 @@ module ActiveRecordCompose
|
|
63
74
|
assign_attributes(attributes)
|
64
75
|
return false if invalid?
|
65
76
|
|
66
|
-
save_in_transaction { run_callbacks(:
|
77
|
+
save_in_transaction { run_callbacks(:create) { save_models } }
|
67
78
|
end
|
68
79
|
|
69
80
|
# Behavior is same to `#create`, but raises an exception prematurely on failure.
|
@@ -72,7 +83,7 @@ module ActiveRecordCompose
|
|
72
83
|
assign_attributes(attributes)
|
73
84
|
valid? || raise_validation_error
|
74
85
|
|
75
|
-
save_in_transaction { run_callbacks(:
|
86
|
+
save_in_transaction { run_callbacks(:create) { save_models } } || raise_on_save_error
|
76
87
|
end
|
77
88
|
|
78
89
|
# Behavior is same to `#save`, but `before_update` and `after_update` hooks fires.
|
@@ -104,7 +115,7 @@ module ActiveRecordCompose
|
|
104
115
|
assign_attributes(attributes)
|
105
116
|
return false if invalid?
|
106
117
|
|
107
|
-
save_in_transaction { run_callbacks(:
|
118
|
+
save_in_transaction { run_callbacks(:update) { save_models } }
|
108
119
|
end
|
109
120
|
|
110
121
|
# Behavior is same to `#update`, but raises an exception prematurely on failure.
|
@@ -113,7 +124,7 @@ module ActiveRecordCompose
|
|
113
124
|
assign_attributes(attributes)
|
114
125
|
valid? || raise_validation_error
|
115
126
|
|
116
|
-
save_in_transaction { run_callbacks(:
|
127
|
+
save_in_transaction { run_callbacks(:update) { save_models } } || raise_on_save_error
|
117
128
|
end
|
118
129
|
|
119
130
|
private
|
@@ -124,10 +135,10 @@ module ActiveRecordCompose
|
|
124
135
|
|
125
136
|
def validate_models = wrapped_models.select { _1.invalid? }.each { errors.merge!(_1) }
|
126
137
|
|
127
|
-
def save_in_transaction
|
138
|
+
def save_in_transaction(&block)
|
128
139
|
run_callbacks(:commit) do
|
129
140
|
result = ::ActiveRecord::Base.transaction do
|
130
|
-
raise ActiveRecord::Rollback unless
|
141
|
+
raise ActiveRecord::Rollback unless run_callbacks(:save, &block)
|
131
142
|
|
132
143
|
true
|
133
144
|
end
|
@@ -142,6 +153,8 @@ module ActiveRecordCompose
|
|
142
153
|
|
143
154
|
def raise_validation_error = raise ActiveRecord::RecordInvalid, self
|
144
155
|
|
145
|
-
def raise_on_save_error = raise ActiveRecord::RecordNotSaved.new(
|
156
|
+
def raise_on_save_error = raise ActiveRecord::RecordNotSaved.new(raise_on_save_error_message, self)
|
157
|
+
|
158
|
+
def raise_on_save_error_message = 'Failed to save the model.'
|
146
159
|
end
|
147
160
|
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.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hamajyotan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01-
|
11
|
+
date: 2024-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '6.1'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
27
|
description: activemodel form object pattern
|
42
28
|
email:
|
43
29
|
- hamajyotan@gmail.com
|
@@ -65,7 +51,7 @@ metadata:
|
|
65
51
|
homepage_uri: https://github.com/hamajyotan/active_record_compose
|
66
52
|
source_code_uri: https://github.com/hamajyotan/active_record_compose
|
67
53
|
changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
|
68
|
-
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.1.
|
54
|
+
documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.1.7
|
69
55
|
rubygems_mfa_required: 'true'
|
70
56
|
post_install_message:
|
71
57
|
rdoc_options: []
|