active_record_compose 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd2ae31a400b17c7ab14c90bd1440060676590c23f91a61f7f54e9a7decf0294
4
- data.tar.gz: 4a599ee2da8a5dcc3b0d9279a360b611b2e38f72304e8a837271f01e2b9821b2
3
+ metadata.gz: bff1c2fd72a225a89ada1c0cab86b6d32f31c0052d8857e665eb2796f4a40883
4
+ data.tar.gz: 307e8ecf50282b9a19e224870796944de47d37f8802d0ffe63cca465eb663cc4
5
5
  SHA512:
6
- metadata.gz: 18f3632810ffc48ed4bd10f78c4e54ff3c383d34231289f9639e488f270bbfd623a5f002b9f4afc653762611621c6f96b85b52498c2ba34e942d0c7c7288bfe4
7
- data.tar.gz: 45fc861a9d223da11b841800dbc0ab689160cabdfdfda3a69433cb1d98884f76f30c742319ffc1ad3601527e9464b322456e44b73c6f098355f898909b5e340c
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,11 @@
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
+
3
9
  ## [0.1.6] - 2024-01-14
4
10
 
5
11
  - add doc for `#save` and `#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
@@ -36,7 +36,6 @@ module ActiveRecordCompose
36
36
  # Execute save or destroy. Unlike #save, an exception is raises on failure.
37
37
  # Whether save or destroy is executed depends on the value of context.
38
38
  #
39
- # @return [InnerModel] self
40
39
  def save!
41
40
  case context
42
41
  when :destroy
@@ -61,7 +60,8 @@ module ActiveRecordCompose
61
60
 
62
61
  # Returns true if equivalent.
63
62
  #
64
- # @return [Object] other
63
+ # @param [Object] other
64
+ # @return [Boolean]
65
65
  def ==(other)
66
66
  return false unless self.class == other.class
67
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 [InnerModelCollection] self
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 [InnerModelCollection] self
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 [InnerModelCollection] self
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 [InnerModelCollection] self
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 [InnerModelCollection] Successful deletion
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 [InnerModelCollection] self
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
 
@@ -31,7 +31,7 @@ module ActiveRecordCompose
31
31
  def save
32
32
  return false if invalid?
33
33
 
34
- save_in_transaction { run_callbacks(:save) { save_models } }
34
+ save_in_transaction { save_models }
35
35
  end
36
36
 
37
37
  # Save the models that exist in models.
@@ -42,7 +42,7 @@ module ActiveRecordCompose
42
42
  def save!
43
43
  valid? || raise_validation_error
44
44
 
45
- save_in_transaction { run_callbacks(:save) { save_models } } || raise_on_save_error
45
+ save_in_transaction { save_models } || raise_on_save_error
46
46
  end
47
47
 
48
48
  # Behavior is same to `#save`, but `before_create` and `after_create` hooks fires.
@@ -74,7 +74,7 @@ module ActiveRecordCompose
74
74
  assign_attributes(attributes)
75
75
  return false if invalid?
76
76
 
77
- save_in_transaction { run_callbacks(:save) { run_callbacks(:create) { save_models } } }
77
+ save_in_transaction { run_callbacks(:create) { save_models } }
78
78
  end
79
79
 
80
80
  # Behavior is same to `#create`, but raises an exception prematurely on failure.
@@ -83,7 +83,7 @@ module ActiveRecordCompose
83
83
  assign_attributes(attributes)
84
84
  valid? || raise_validation_error
85
85
 
86
- save_in_transaction { run_callbacks(:save) { run_callbacks(:create) { save_models } } } || raise_on_save_error
86
+ save_in_transaction { run_callbacks(:create) { save_models } } || raise_on_save_error
87
87
  end
88
88
 
89
89
  # Behavior is same to `#save`, but `before_update` and `after_update` hooks fires.
@@ -115,7 +115,7 @@ module ActiveRecordCompose
115
115
  assign_attributes(attributes)
116
116
  return false if invalid?
117
117
 
118
- save_in_transaction { run_callbacks(:save) { run_callbacks(:update) { save_models } } }
118
+ save_in_transaction { run_callbacks(:update) { save_models } }
119
119
  end
120
120
 
121
121
  # Behavior is same to `#update`, but raises an exception prematurely on failure.
@@ -124,7 +124,7 @@ module ActiveRecordCompose
124
124
  assign_attributes(attributes)
125
125
  valid? || raise_validation_error
126
126
 
127
- save_in_transaction { run_callbacks(:save) { run_callbacks(:update) { save_models } } } || raise_on_save_error
127
+ save_in_transaction { run_callbacks(:update) { save_models } } || raise_on_save_error
128
128
  end
129
129
 
130
130
  private
@@ -135,10 +135,10 @@ module ActiveRecordCompose
135
135
 
136
136
  def validate_models = wrapped_models.select { _1.invalid? }.each { errors.merge!(_1) }
137
137
 
138
- def save_in_transaction
138
+ def save_in_transaction(&block)
139
139
  run_callbacks(:commit) do
140
140
  result = ::ActiveRecord::Base.transaction do
141
- raise ActiveRecord::Rollback unless yield
141
+ raise ActiveRecord::Rollback unless run_callbacks(:save, &block)
142
142
 
143
143
  true
144
144
  end
@@ -153,6 +153,8 @@ module ActiveRecordCompose
153
153
 
154
154
  def raise_validation_error = raise ActiveRecord::RecordInvalid, self
155
155
 
156
- def raise_on_save_error = raise ActiveRecord::RecordNotSaved.new('Failed to save the model.', self)
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.'
157
159
  end
158
160
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordCompose
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - hamajyotan
@@ -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.6
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: []