associate_jsonb 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/associate_jsonb.rb +66 -27
- data/lib/associate_jsonb/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 847b6c8d5410dca5511a9ab2537ea3d2d9ba5c40e681ad42a63f0e752604d1ff
|
4
|
+
data.tar.gz: 935adff8c21a94b4d7593a236c9e56eec29c31340a897c82851b3662b0da880e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c614936df446af3b0bd1301037954214e973cc4522637c3578f47f775065eb9085f08b668b11bf619ba3ca7968b14c078e192579be56eee489d5bf0780097552
|
7
|
+
data.tar.gz: 5243289a848da4cc15d03204a6d5bc6a502d810e6f839fb2b26aabc264bea63553b040a8508cf6f6d0722d82be5ea905c9bb75caa5e9ac1038b2e9d85e535122
|
data/lib/associate_jsonb.rb
CHANGED
@@ -20,64 +20,106 @@ loader.collapse("#{__dir__}/associate_jsonb/connection_adapters/schema_definitio
|
|
20
20
|
loader.setup # ready!
|
21
21
|
|
22
22
|
module AssociateJsonb
|
23
|
-
mattr_accessor :jsonb_hash_types,
|
24
|
-
mattr_accessor :
|
23
|
+
mattr_accessor :jsonb_hash_types, default: []
|
24
|
+
mattr_accessor :jsonb_set_added, default: [] # :nodoc:
|
25
|
+
mattr_accessor :jsonb_set_removed, default: [] # :nodoc:
|
25
26
|
mattr_accessor :jsonb_set_enabled, default: false
|
26
|
-
mattr_accessor :jsonb_delete_nil,
|
27
|
+
mattr_accessor :jsonb_delete_nil, default: false
|
27
28
|
private_class_method :jsonb_hash_types=
|
28
29
|
private_class_method :jsonb_set_enabled=
|
29
30
|
|
31
|
+
def self.jsonb_oid_class # :nodoc:
|
32
|
+
:default
|
33
|
+
end
|
34
|
+
private_class_method :jsonb_oid_class
|
35
|
+
|
36
|
+
##
|
37
|
+
# Enables the use of `jsonb_nested_set` for hash updates
|
38
|
+
#
|
39
|
+
# if passed a class, or a list of classes, those classes will be added todo
|
40
|
+
# the enabled classes. if no argument is given, and the enabled class list is
|
41
|
+
# empty, `ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Jsonb` is added
|
42
|
+
# to the list of enabled classes
|
30
43
|
def self.enable_jsonb_set(klass = nil, *classes)
|
31
|
-
|
44
|
+
if klass.nil?
|
45
|
+
add_hash_type jsonb_oid_class if jsonb_hash_types.empty?
|
46
|
+
else
|
47
|
+
add_hash_type(*Array(klass), *classes)
|
48
|
+
end
|
32
49
|
self.jsonb_set_enabled = true
|
33
50
|
end
|
34
51
|
|
52
|
+
##
|
53
|
+
# Disables the use of `jsonb_nested_set` for hash updates
|
54
|
+
#
|
55
|
+
# if passed a class, or a list of classes, those classes will be removed from
|
56
|
+
# the list of enabled classes
|
35
57
|
def self.disable_jsonb_set(klass = nil, *classes)
|
36
58
|
remove_hash_type(*Array(klass), *classes) unless klass.nil?
|
37
59
|
self.jsonb_set_enabled = false
|
38
60
|
end
|
39
61
|
|
62
|
+
##
|
63
|
+
# Add class(es) to the list of classes that are able to be upated when
|
64
|
+
# `jsonb_set_enabled` is true
|
40
65
|
def self.add_hash_type(*classes)
|
41
|
-
self.
|
66
|
+
self.jsonb_set_added |= classes.flatten
|
42
67
|
end
|
43
68
|
|
69
|
+
##
|
70
|
+
# Remove class(es) from the list of classes that are able to be upated when
|
71
|
+
# `jsonb_set_enabled` is true
|
44
72
|
def self.remove_hash_type(*classes)
|
45
73
|
self.jsonb_set_removed |= classes.flatten
|
46
74
|
end
|
47
75
|
|
48
|
-
|
49
|
-
|
50
|
-
|
76
|
+
##
|
77
|
+
# Returns true if `jsonb_set_enabled` is true and the value is an enabled hash
|
78
|
+
# type
|
79
|
+
def self.merge_hash?(value)
|
80
|
+
!!jsonb_set_enabled && is_hash?(value)
|
51
81
|
end
|
52
82
|
|
53
|
-
|
54
|
-
|
83
|
+
##
|
84
|
+
# Returns true if the given value is a descendant of any of the classes
|
85
|
+
# in `jsonb_hash_types`
|
86
|
+
def self.is_hash?(value)
|
87
|
+
!!value && self.jsonb_hash_types.any? { |type| value.is_a?(type) }
|
55
88
|
end
|
56
89
|
end
|
57
90
|
|
58
|
-
|
59
91
|
# rubocop:disable Metrics/BlockLength
|
60
92
|
ActiveSupport.on_load :active_record do
|
61
93
|
loader.eager_load
|
62
|
-
AssociateJsonb.
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
94
|
+
AssociateJsonb.module_eval do
|
95
|
+
redefine_method = proc {|name, hide = false, &block|
|
96
|
+
method(name).owner.remove_method name
|
97
|
+
if block
|
98
|
+
define_singleton_method(name, &block)
|
99
|
+
private_class_method name if hide
|
68
100
|
end
|
69
|
-
|
101
|
+
}
|
102
|
+
|
103
|
+
redefine_method.call(:jsonb_oid_class, true) do
|
104
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Jsonb
|
70
105
|
end
|
71
106
|
|
72
|
-
|
107
|
+
redefine_method.call(:add_hash_type) do |*classes|
|
108
|
+
self.jsonb_hash_types |= classes.flatten
|
109
|
+
end
|
73
110
|
|
74
|
-
|
111
|
+
redefine_method.call(:remove_hash_type) do |*classes|
|
75
112
|
self.jsonb_hash_types -= classes.flatten
|
76
113
|
end
|
77
|
-
|
78
|
-
self.
|
79
|
-
self.
|
80
|
-
|
114
|
+
|
115
|
+
self.add_hash_type jsonb_set_added.map {|k| (k == :default) ? self.jsonb_oid_class : k}
|
116
|
+
self.remove_hash_type jsonb_set_removed
|
117
|
+
|
118
|
+
redefine_method.call(:jsonb_set_added)
|
119
|
+
redefine_method.call(:jsonb_set_added=)
|
120
|
+
redefine_method.call(:jsonb_set_removed)
|
121
|
+
redefine_method.call(:jsonb_set_removed=)
|
122
|
+
redefine_method = nil
|
81
123
|
end
|
82
124
|
|
83
125
|
|
@@ -145,9 +187,6 @@ ActiveSupport.on_load :active_record do
|
|
145
187
|
AssociateJsonb::Associations::Preloader::Association
|
146
188
|
)
|
147
189
|
|
148
|
-
# ActiveRecord::Associations::Preloader::HasMany.prepend(
|
149
|
-
# AssociateJsonb::Associations::Preloader::HasMany
|
150
|
-
# )
|
151
190
|
%i[
|
152
191
|
AlterTable
|
153
192
|
ConstraintDefinition
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: associate_jsonb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sampson Crowley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -96,14 +96,14 @@ dependencies:
|
|
96
96
|
requirements:
|
97
97
|
- - "~>"
|
98
98
|
- !ruby/object:Gem::Version
|
99
|
-
version: 3.
|
99
|
+
version: '3.9'
|
100
100
|
type: :development
|
101
101
|
prerelease: false
|
102
102
|
version_requirements: !ruby/object:Gem::Requirement
|
103
103
|
requirements:
|
104
104
|
- - "~>"
|
105
105
|
- !ruby/object:Gem::Version
|
106
|
-
version: 3.
|
106
|
+
version: '3.9'
|
107
107
|
description: |
|
108
108
|
This gem extends ActiveRecord to add additional functionality to JSONB
|
109
109
|
|
@@ -190,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
190
190
|
- !ruby/object:Gem::Version
|
191
191
|
version: '0'
|
192
192
|
requirements: []
|
193
|
-
rubygems_version: 3.1.
|
193
|
+
rubygems_version: 3.1.4
|
194
194
|
signing_key:
|
195
195
|
specification_version: 4
|
196
196
|
summary: Store database references in PostgreSQL Jsonb columns
|