umbrellio-sequel-plugins 0.10.0.101 → 0.11.0.143

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: 7fa321f3720c745f853bf852f1fbfcb430b35391ff67d48284f051ff804c3e20
4
- data.tar.gz: 0d1d70e9f8f05173b63b62db97dfa5f4e562527d7b9e87affdbd8f2151987757
3
+ metadata.gz: 4c7f90176d9dbae1bc7d18e1ad91f2755963a5c2c3dbe10fd5f8d3f96200c161
4
+ data.tar.gz: '057828ad853e3823e6e4a0da8981d531c1b880984fd74b3b3a6672a8db6e8141'
5
5
  SHA512:
6
- metadata.gz: b40811232e8e9b6a711b134ae831cb05c04bc85553587bffa7bd65f0d77105ff12d0e739fa84e6048a02ca254e0782dd8bf200e488b7c9437d89c170f199862e
7
- data.tar.gz: b8060ae4b412df5d1a459c19f5187aa27acddf8cc753465f1dd38844d8301e68e5a703a00de9530818f7d529757f4dc89aa592165542f542b3495676283c45ed
6
+ metadata.gz: 44bc22d1c7a37d0c7241c025eaa372ace531a52ca84dab3c8a191c0e4070ef0242b09e717a1a87accd6e8cc751511855a7b9bb6dd8ff26ead70ddd150002ca3c
7
+ data.tar.gz: 75691f50b458f276c5416b27d79b1ea2da4debbce8f3b4e18a58ef3e9ae0acaf3a5c525dd77f6d3c8eb4b96b78e71e8bb3761ce0f130fb6a8c1ac46a0bf9f9e0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- umbrellio-sequel-plugins (0.10.0)
4
+ umbrellio-sequel-plugins (0.11.0)
5
5
  sequel
6
6
  symbiont-ruby
7
7
 
data/README.md CHANGED
@@ -380,7 +380,7 @@ Example:
380
380
 
381
381
  ```ruby
382
382
  class User < Sequel::Model
383
- store :data, :first_name
383
+ store :data, :first_name, :last_name
384
384
  end
385
385
 
386
386
  user = User.create(first_name: "John")
@@ -27,10 +27,14 @@ module MigratorExtension
27
27
  if migration.use_transactions.nil?
28
28
  @db.supports_transactional_ddl?
29
29
  else
30
+ # :nocov:
30
31
  migration.use_transactions
32
+ # :nocov:
31
33
  end
32
34
  else
35
+ # :nocov:
33
36
  @use_transactions
37
+ # :nocov:
34
38
  end
35
39
  end
36
40
 
@@ -16,7 +16,7 @@ module Sequel::Plugins::StoreAccessors
16
16
  # user.first_name # => "John"
17
17
  # user.data # => {"first_name": "John"}
18
18
  def store(column, *fields)
19
- include_accessors_module
19
+ include_accessors_module(column)
20
20
 
21
21
  fields.each do |field|
22
22
  define_store_getter(column, field)
@@ -24,12 +24,22 @@ module Sequel::Plugins::StoreAccessors
24
24
  end
25
25
  end
26
26
 
27
+ def call(_)
28
+ super.tap(&:calculate_initial_store)
29
+ end
30
+
27
31
  private
28
32
 
29
- def include_accessors_module
30
- return if defined?(@_store_accessors_module)
31
- @_store_accessors_module = Module.new
32
- include @_store_accessors_module
33
+ def include_accessors_module(column)
34
+ unless defined?(@_store_accessors_module)
35
+ @_store_accessors_module = Module.new
36
+ include @_store_accessors_module
37
+ end
38
+
39
+ prev_columns = @_store_accessors_module.instance_variable_get(:@_store_columns) || []
40
+ new_columns = [*prev_columns, column]
41
+ @_store_accessors_module.instance_variable_set(:@_store_columns, new_columns)
42
+ @_store_accessors_module.define_method(:store_columns) { new_columns }
33
43
  end
34
44
 
35
45
  def define_store_getter(column, field)
@@ -48,4 +58,95 @@ module Sequel::Plugins::StoreAccessors
48
58
  end
49
59
  end
50
60
  end
61
+
62
+ module InstanceMethods
63
+ def after_update
64
+ super
65
+ refresh_initial_store
66
+ end
67
+
68
+ def after_create
69
+ super
70
+ refresh_initial_store
71
+ end
72
+
73
+ def calculate_initial_store
74
+ @store_values_hashes || refresh_initial_store
75
+ end
76
+
77
+ def changed_columns
78
+ changed = super
79
+ return changed unless respond_to?(:store_columns)
80
+ changed = changed.dup if frozen?
81
+ store_columns.each do |col|
82
+ match = patched(col).empty? && deleted(col).empty?
83
+ if changed.include?(col)
84
+ changed.delete(col) if match
85
+ else
86
+ changed << col unless match
87
+ end
88
+ end
89
+ changed
90
+ end
91
+
92
+ private
93
+
94
+ def _update_without_checking(columns)
95
+ return super unless respond_to?(:store_columns)
96
+
97
+ mapped_columns = columns.to_h do |column, v|
98
+ next [column, v] unless store_columns.include?(column)
99
+
100
+ json = Sequel.pg_jsonb_op(
101
+ Sequel.function(:coalesce, Sequel[column], Sequel.pg_jsonb({})),
102
+ )
103
+ updated = deleted(column).inject(json) { |res, k| res.delete_path([k.to_s]) }
104
+ [column, updated.concat(patched(column))]
105
+ end
106
+
107
+ super(mapped_columns)
108
+ end
109
+
110
+ def patched(column)
111
+ initial_fields = initial_store_fields[column] || []
112
+ initial_hashes = store_values_hashes[column] || {}
113
+ current = @values[column] || {}
114
+
115
+ current.dup.delete_if do |k, v|
116
+ initial_fields.include?(k) && initial_hashes[k] == v.hash
117
+ end
118
+ end
119
+
120
+ def deleted(column)
121
+ initial_fields = initial_store_fields[column] || []
122
+ current = @values[column] || {}
123
+
124
+ initial_fields.dup - current.keys
125
+ end
126
+
127
+ def _refresh(dataset)
128
+ super
129
+ refresh_initial_store
130
+ end
131
+
132
+ def _save_refresh
133
+ super
134
+ refresh_initial_store
135
+ end
136
+
137
+ def refresh_initial_store
138
+ return unless respond_to?(:store_columns)
139
+ store_values = @values.slice(*store_columns)
140
+ @initial_store_fields = store_values.transform_values { |v| v.to_h.keys }
141
+ @store_values_hashes = store_values.transform_values { |v| v.to_h.transform_values(&:hash) }
142
+ end
143
+
144
+ def initial_store_fields
145
+ @initial_store_fields || {}
146
+ end
147
+
148
+ def store_values_hashes
149
+ @store_values_hashes || {}
150
+ end
151
+ end
51
152
  end
@@ -4,7 +4,7 @@ lib = File.expand_path("lib", __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- gem_version = "0.10.0"
7
+ gem_version = "0.11.0"
8
8
 
9
9
  if ENV.fetch("PUBLISH_JOB", nil)
10
10
  release_version = "#{gem_version}.#{ENV.fetch("GITHUB_RUN_NUMBER")}"
data/utils/database.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "logger"
4
4
 
5
- DB = Sequel.connect(ENV.fetch("DB_URL", "postgres://localhost/sequel_plugins"))
5
+ DB = Sequel.connect(ENV.fetch("DB_URL", "postgres:///sequel_plugins"))
6
6
  DB.logger = Logger.new("log/db.log")
7
7
 
8
8
  Sequel::Model.db = DB
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: umbrellio-sequel-plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0.101
4
+ version: 0.11.0.143
5
5
  platform: ruby
6
6
  authors:
7
7
  - Team Umbrellio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-29 00:00:00.000000000 Z
11
+ date: 2023-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel