active_record_compose 0.11.2 → 0.12.0
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.
- checksums.yaml +4 -4
- data/.yardopts +4 -0
- data/CHANGELOG.md +29 -0
- data/README.md +6 -5
- data/lib/active_record_compose/attributes/attribute_predicate.rb +29 -0
- data/lib/active_record_compose/attributes/delegation.rb +46 -0
- data/lib/active_record_compose/attributes/querying.rb +62 -0
- data/lib/active_record_compose/attributes.rb +153 -0
- data/lib/active_record_compose/callbacks.rb +22 -0
- data/lib/active_record_compose/composed_collection.rb +16 -9
- data/lib/active_record_compose/model.rb +355 -17
- data/lib/active_record_compose/persistence.rb +13 -19
- data/lib/active_record_compose/transaction_support.rb +12 -10
- data/lib/active_record_compose/validations.rb +4 -0
- data/lib/active_record_compose/version.rb +1 -1
- data/lib/active_record_compose/wrapped_model.rb +4 -2
- data/lib/active_record_compose.rb +4 -0
- data/sig/_internal/package_private.rbs +56 -34
- data/sig/active_record_compose.rbs +3 -7
- metadata +9 -6
- data/lib/active_record_compose/attribute_querying.rb +0 -67
- data/lib/active_record_compose/delegate_attribute.rb +0 -95
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.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hamajyotan
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activerecord
|
@@ -38,15 +38,18 @@ extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
40
|
- ".rubocop.yml"
|
41
|
+
- ".yardopts"
|
41
42
|
- CHANGELOG.md
|
42
43
|
- CODE_OF_CONDUCT.md
|
43
44
|
- LICENSE.txt
|
44
45
|
- README.md
|
45
46
|
- lib/active_record_compose.rb
|
46
|
-
- lib/active_record_compose/
|
47
|
+
- lib/active_record_compose/attributes.rb
|
48
|
+
- lib/active_record_compose/attributes/attribute_predicate.rb
|
49
|
+
- lib/active_record_compose/attributes/delegation.rb
|
50
|
+
- lib/active_record_compose/attributes/querying.rb
|
47
51
|
- lib/active_record_compose/callbacks.rb
|
48
52
|
- lib/active_record_compose/composed_collection.rb
|
49
|
-
- lib/active_record_compose/delegate_attribute.rb
|
50
53
|
- lib/active_record_compose/model.rb
|
51
54
|
- lib/active_record_compose/persistence.rb
|
52
55
|
- lib/active_record_compose/transaction_support.rb
|
@@ -62,7 +65,7 @@ metadata:
|
|
62
65
|
homepage_uri: https://github.com/hamajyotan/active_record_compose
|
63
66
|
source_code_uri: https://github.com/hamajyotan/active_record_compose
|
64
67
|
changelog_uri: https://github.com/hamajyotan/active_record_compose/blob/main/CHANGELOG.md
|
65
|
-
documentation_uri: https://
|
68
|
+
documentation_uri: https://hamajyotan.github.io/active_record_compose/
|
66
69
|
rubygems_mfa_required: 'true'
|
67
70
|
rdoc_options: []
|
68
71
|
require_paths:
|
@@ -78,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
81
|
- !ruby/object:Gem::Version
|
79
82
|
version: '0'
|
80
83
|
requirements: []
|
81
|
-
rubygems_version: 3.6.
|
84
|
+
rubygems_version: 3.6.7
|
82
85
|
specification_version: 4
|
83
86
|
summary: activemodel form object pattern
|
84
87
|
test_files: []
|
@@ -1,67 +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
|
-
include ActiveModel::AttributeMethods
|
45
|
-
|
46
|
-
included do
|
47
|
-
attribute_method_suffix "?", parameters: false
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def attribute?(attr_name)
|
53
|
-
value = public_send(attr_name)
|
54
|
-
|
55
|
-
case value
|
56
|
-
when true then true
|
57
|
-
when false, nil then false
|
58
|
-
else
|
59
|
-
if value.respond_to?(:zero?)
|
60
|
-
!value.zero?
|
61
|
-
else
|
62
|
-
value.present?
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,95 +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
|
-
include ActiveModel::Attributes
|
40
|
-
|
41
|
-
# steep:ignore:start
|
42
|
-
if defined?(Data)
|
43
|
-
Delegation = Data.define(:attribute, :to, :allow_nil) do
|
44
|
-
def reader = attribute.to_s
|
45
|
-
def writer = "#{attribute}="
|
46
|
-
end
|
47
|
-
else
|
48
|
-
Delegation = Struct.new(:attribute, :to, :allow_nil, keyword_init: true) do
|
49
|
-
def reader = attribute.to_s
|
50
|
-
def writer = "#{attribute}="
|
51
|
-
end
|
52
|
-
end
|
53
|
-
# steep:ignore:end
|
54
|
-
|
55
|
-
included do
|
56
|
-
# @type self: Class
|
57
|
-
class_attribute :delegated_attributes, instance_writer: false
|
58
|
-
end
|
59
|
-
|
60
|
-
module ClassMethods
|
61
|
-
# Defines the reader and writer for the specified attribute.
|
62
|
-
#
|
63
|
-
def delegate_attribute(*attributes, to:, allow_nil: nil)
|
64
|
-
delegations = attributes.map { Delegation.new(attribute: _1.to_s, to:, allow_nil:) }
|
65
|
-
|
66
|
-
delegations.map do |delegation|
|
67
|
-
delegate delegation.reader, delegation.writer, to: delegation.to, allow_nil: delegation.allow_nil
|
68
|
-
define_attribute_methods delegation.attribute
|
69
|
-
end
|
70
|
-
|
71
|
-
self.delegated_attributes = (delegated_attributes.to_a + delegations).reverse.uniq { _1.attribute }.reverse
|
72
|
-
end
|
73
|
-
|
74
|
-
# Returns a array of attribute name.
|
75
|
-
# Attributes declared with `delegate_attribute` are also merged.
|
76
|
-
#
|
77
|
-
# @return [Array<String>] array of attribute name.
|
78
|
-
def attribute_names = super + delegated_attributes.to_a.map { _1.attribute }
|
79
|
-
end
|
80
|
-
|
81
|
-
# Returns a array of attribute name.
|
82
|
-
# Attributes declared with `delegate_attribute` are also merged.
|
83
|
-
#
|
84
|
-
# @return [Array<String>] array of attribute name.
|
85
|
-
def attribute_names = super + delegated_attributes.to_a.map { _1.attribute }
|
86
|
-
|
87
|
-
# Returns a hash with the attribute name as key and the attribute value as value.
|
88
|
-
# Attributes declared with `delegate_attribute` are also merged.
|
89
|
-
#
|
90
|
-
# @return [Hash] hash with the attribute name as key and the attribute value as value.
|
91
|
-
def attributes
|
92
|
-
super.merge(delegated_attributes.to_a.map { _1.attribute }.to_h { [ _1, public_send(_1) ] })
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|