active_record_compose 0.11.1 → 0.11.3

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.
@@ -1,10 +1,46 @@
1
1
  module ActiveRecordCompose
2
- module AttributeQuerying
2
+ module Attributes
3
3
  extend ActiveSupport::Concern
4
- extend ActiveModel::AttributeMethods::ClassMethods
4
+ include ActiveModel::Attributes
5
+ include Querying
5
6
 
6
- private
7
- def attribute?: (attribute_name) -> untyped
7
+ def delegated_attributes: () -> Array[Delegation]
8
+
9
+ module ClassMethods : Module
10
+ include ActiveModel::Attributes::ClassMethods
11
+ include ActiveModel::AttributeMethods::ClassMethods
12
+
13
+ def delegate_attribute: (*untyped methods, to: untyped, ?allow_nil: bool) -> untyped
14
+ def delegated_attributes: () -> Array[Delegation]
15
+ def delegated_attributes=: (Array[Delegation]) -> untyped
16
+ end
17
+
18
+ class Delegation
19
+ def initialize: (attribute: String, to: Symbol, ?allow_nil: bool) -> void
20
+ def attribute: () -> Symbol
21
+ def attribute_name: () -> String
22
+ def attribute_hash: (Object model) -> Hash[String, untyped]
23
+ def define_delegated_attribute: ((Module & ActiveModel::AttributeMethods::ClassMethods) klass) -> void
24
+
25
+ @attribute: Symbol
26
+ @to: Symbol
27
+ @allow_nil: bool
28
+
29
+ private
30
+ def to: () -> Symbol
31
+ def allow_nil: () -> bool
32
+ def reader: () -> String
33
+ def writer: () -> String
34
+ end
35
+
36
+ module Querying
37
+ include ActiveModel::AttributeMethods
38
+ extend ActiveSupport::Concern
39
+ extend ActiveModel::AttributeMethods::ClassMethods
40
+
41
+ private
42
+ def attribute?: (attribute_name) -> untyped
43
+ end
8
44
  end
9
45
 
10
46
  module Callbacks
@@ -36,26 +72,12 @@ module ActiveRecordCompose
36
72
  include PackagePrivate
37
73
  end
38
74
 
39
- module DelegateAttribute : ActiveModel::Attributes
40
- extend ActiveSupport::Concern
41
-
42
- def delegated_attributes: () -> Array[String]
43
-
44
- module ClassMethods : Module
45
- include ActiveModel::AttributeMethods::ClassMethods
46
-
47
- def delegate_attribute: (*untyped methods, to: untyped, ?allow_nil: untyped?) -> untyped
48
- def delegated_attributes: () -> Array[String]
49
- def delegated_attributes=: (Array[String]) -> untyped
50
- end
51
- end
52
-
53
75
  class Model
54
- include DelegateAttribute
55
- extend DelegateAttribute::ClassMethods
76
+ include Attributes
77
+ extend Attributes::ClassMethods
56
78
  include TransactionSupport
57
79
  extend TransactionSupport::ClassMethods
58
- include ActiveRecordCompose::Callbacks
80
+ include Callbacks
59
81
 
60
82
  @__models: ComposedCollection
61
83
 
@@ -79,7 +101,27 @@ module ActiveRecordCompose
79
101
  end
80
102
  end
81
103
 
104
+ module Persistence
105
+ include Callbacks
106
+ include TransactionSupport
107
+ extend TransactionSupport::ClassMethods
108
+
109
+ def save: (**untyped options) -> bool
110
+ def save!: (**untyped options) -> untyped
111
+ def update: (?Hash[attribute_name, untyped]) -> bool
112
+ def update!: (?Hash[attribute_name, untyped]) -> untyped
113
+
114
+ private
115
+ def models: -> ComposedCollection
116
+ def save_models: (bang: bool, **untyped options) -> bool
117
+ def raise_on_save_error: -> bot
118
+ def raise_on_save_error_message: -> String
119
+ end
120
+
82
121
  module Validations : Model
122
+ extend ActiveSupport::Concern
123
+ extend ActiveModel::Validations::ClassMethods
124
+
83
125
  def save: (**untyped options) -> bool
84
126
  def save!: (**untyped options) -> untyped
85
127
  def valid?: (?validation_context context) -> bool
@@ -87,7 +129,6 @@ module ActiveRecordCompose
87
129
  @context_for_override_validation: OverrideValidationContext
88
130
 
89
131
  private
90
-
91
132
  def perform_validations: (::Hash[untyped, untyped]) -> bool
92
133
  def raise_validation_error: -> bot
93
134
  def context_for_override_validation: -> OverrideValidationContext
@@ -71,7 +71,7 @@ module ActiveRecordCompose
71
71
  def self.after_commit: (*callback[instance], ?if: condition[instance], ?unless: condition[instance], **untyped) ?{ () [self: instance] -> void } -> void
72
72
  def self.after_rollback: (*callback[instance], ?if: condition[instance], ?unless: condition[instance], **untyped) ?{ () [self: instance] -> void } -> void
73
73
 
74
- def self.delegate_attribute: (*untyped methods, to: untyped, ?allow_nil: untyped?) -> untyped
74
+ def self.delegate_attribute: (*untyped methods, to: untyped, ?allow_nil: bool) -> untyped
75
75
  def self.connection: -> ActiveRecord::ConnectionAdapters::AbstractAdapter
76
76
  def self.lease_connection: -> ActiveRecord::ConnectionAdapters::AbstractAdapter
77
77
  def self.with_connection: [T] () { () -> T } -> T
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - hamajyotan
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-06-10 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activerecord
@@ -16,6 +16,9 @@ dependencies:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
18
  version: '7.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '8.1'
19
22
  type: :runtime
20
23
  prerelease: false
21
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -23,6 +26,9 @@ dependencies:
23
26
  - - ">="
24
27
  - !ruby/object:Gem::Version
25
28
  version: '7.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '8.1'
26
32
  description: activemodel form object pattern. it embraces multiple AR models and provides
27
33
  a transparent interface as if they were a single model.
28
34
  email:
@@ -32,16 +38,19 @@ extensions: []
32
38
  extra_rdoc_files: []
33
39
  files:
34
40
  - ".rubocop.yml"
41
+ - ".yardopts"
35
42
  - CHANGELOG.md
36
43
  - CODE_OF_CONDUCT.md
37
44
  - LICENSE.txt
38
45
  - README.md
39
46
  - lib/active_record_compose.rb
40
- - lib/active_record_compose/attribute_querying.rb
47
+ - lib/active_record_compose/attributes.rb
48
+ - lib/active_record_compose/attributes/delegation.rb
49
+ - lib/active_record_compose/attributes/querying.rb
41
50
  - lib/active_record_compose/callbacks.rb
42
51
  - lib/active_record_compose/composed_collection.rb
43
- - lib/active_record_compose/delegate_attribute.rb
44
52
  - lib/active_record_compose/model.rb
53
+ - lib/active_record_compose/persistence.rb
45
54
  - lib/active_record_compose/transaction_support.rb
46
55
  - lib/active_record_compose/validations.rb
47
56
  - lib/active_record_compose/version.rb
@@ -55,7 +64,7 @@ metadata:
55
64
  homepage_uri: https://github.com/hamajyotan/active_record_compose
56
65
  source_code_uri: https://github.com/hamajyotan/active_record_compose
57
66
  changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
58
- documentation_uri: https://www.rubydoc.info/gems/active_record_compose/0.11.1
67
+ documentation_uri: https://hamajyotan.github.io/active_record_compose/
59
68
  rubygems_mfa_required: 'true'
60
69
  rdoc_options: []
61
70
  require_paths:
@@ -71,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
80
  - !ruby/object:Gem::Version
72
81
  version: '0'
73
82
  requirements: []
74
- rubygems_version: 3.6.2
83
+ rubygems_version: 3.6.7
75
84
  specification_version: 4
76
85
  summary: activemodel form object pattern
77
86
  test_files: []
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecordCompose
4
- # = Attribute \Querying
5
- #
6
- # This provides predicate methods based on the attributes.
7
- #
8
- # class AccountRegistration < ActiveRecordCompose::Model
9
- # def initialize
10
- # @account = Account.new
11
- # super()
12
- # models << account
13
- # end
14
- #
15
- # attribute :original_attr
16
- # delegate_attribute :name, :email, to: :account
17
- #
18
- # private
19
- #
20
- # attr_reader :account
21
- # end
22
- #
23
- # model = AccountRegistration.new
24
- #
25
- # model.name #=> nil
26
- # model.name? #=> false
27
- # model.name = 'Alice'
28
- # model.name? #=> true
29
- #
30
- # model.original_attr = "Bob"
31
- # model.original_attr? #=> true
32
- # model.original_attr = ""
33
- # model.original_attr? #=> false
34
- #
35
- # # If the value is numeric, it returns the result of checking whether it is zero or not.
36
- # # This behavior is consistent with `ActiveRecord::AttributeMethods::Query`.
37
- # model.original_attr = 123
38
- # model.original_attr? #=> true
39
- # model.original_attr = 0
40
- # model.original_attr? #=> false
41
- #
42
- module AttributeQuerying
43
- extend ActiveSupport::Concern
44
-
45
- included do
46
- attribute_method_suffix '?', parameters: false
47
- end
48
-
49
- private
50
-
51
- def attribute?(attr_name)
52
- value = public_send(attr_name)
53
-
54
- case value
55
- when true then true
56
- when false, nil then false
57
- else
58
- if value.respond_to?(:zero?)
59
- !value.zero?
60
- else
61
- value.present?
62
- end
63
- end
64
- end
65
- end
66
- end
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecordCompose
4
- # = Delegate \Attribute
5
- #
6
- # It provides a macro description that expresses access to the attributes of the AR model through delegation.
7
- #
8
- # class AccountRegistration < ActiveRecordCompose::Model
9
- # def initialize(account, attributes = {})
10
- # @account = account
11
- # super(attributes)
12
- # models.push(account)
13
- # end
14
- #
15
- # attribute :original_attribute, :string, default: 'qux'
16
- #
17
- # # like a `delegate :name, :name=, to: :account`
18
- # delegate_attribute :name, to: :account
19
- #
20
- # private
21
- #
22
- # attr_reader :account
23
- # end
24
- #
25
- # account = Account.new
26
- # account.name = 'foo'
27
- #
28
- # registration = AccountRegistration.new(account)
29
- # registration.name #=> 'foo' # delegate to account#name
30
- #
31
- # registration.name = 'bar' # delegate to account#name=
32
- # account.name #=> 'bar'
33
- #
34
- # # Attributes defined in delegate_attribute will be included in the original `#attributes`.
35
- # registration.attributes #=> { 'original_attribute' => 'qux', 'name' => 'bar' }
36
- #
37
- module DelegateAttribute
38
- extend ActiveSupport::Concern
39
-
40
- included do
41
- # @type self: Class
42
- class_attribute :delegated_attributes, instance_writer: false
43
- end
44
-
45
- module ClassMethods
46
- # Defines the reader and writer for the specified attribute.
47
- #
48
- def delegate_attribute(*attributes, to:, allow_nil: nil)
49
- delegates = attributes.flat_map do |attribute|
50
- reader = attribute.to_s
51
- writer = "#{attribute}="
52
-
53
- [reader, writer]
54
- end
55
-
56
- delegate(*delegates, to:, allow_nil:)
57
- attributes.each { define_attribute_methods _1 }
58
- self.delegated_attributes = delegated_attributes.to_a + attributes.map { _1.to_s }
59
- end
60
- end
61
-
62
- # Returns a array of attribute name.
63
- # Attributes declared with `delegate_attribute` are also merged.
64
- #
65
- # @return [Array<String>] array of attribute name.
66
- def attribute_names = super + delegated_attributes
67
-
68
- # Returns a hash with the attribute name as key and the attribute value as value.
69
- # Attributes declared with `delegate_attribute` are also merged.
70
- #
71
- # @return [Hash] hash with the attribute name as key and the attribute value as value.
72
- def attributes
73
- super.merge(delegated_attributes.to_h { [_1, public_send(_1)] })
74
- end
75
- end
76
- end