pakyow-data 1.0.0.rc1 → 1.0.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/CHANGELOG.md +5 -0
- data/lib/pakyow/application/behavior/data/lookup.rb +77 -0
- data/lib/pakyow/application/behavior/data/serialization.rb +43 -0
- data/lib/pakyow/{data/behavior/config.rb → application/config/data.rb} +3 -3
- data/lib/pakyow/application/helpers/data.rb +13 -0
- data/lib/pakyow/{environment → behavior}/data/auto_migrate.rb +1 -1
- data/lib/pakyow/{environment → behavior}/data/connections.rb +1 -1
- data/lib/pakyow/{environment → behavior}/data/memory_db.rb +1 -1
- data/lib/pakyow/config/data.rb +52 -0
- data/lib/pakyow/data/adapters/{abstract.rb → base.rb} +1 -1
- data/lib/pakyow/data/adapters/sql/commands.rb +3 -3
- data/lib/pakyow/data/adapters/sql/differ.rb +1 -0
- data/lib/pakyow/data/adapters/sql/migrator/adapter_methods.rb +1 -0
- data/lib/pakyow/data/adapters/sql/migrator.rb +1 -0
- data/lib/pakyow/data/adapters/sql/migrators/automator.rb +1 -0
- data/lib/pakyow/data/adapters/sql/migrators/finalizer.rb +1 -0
- data/lib/pakyow/data/adapters/sql/runner.rb +1 -0
- data/lib/pakyow/data/adapters/sql.rb +3 -2
- data/lib/pakyow/data/container.rb +2 -1
- data/lib/pakyow/data/framework.rb +9 -9
- data/lib/pakyow/data/migrator.rb +2 -0
- data/lib/pakyow/data/object.rb +1 -0
- data/lib/pakyow/data/proxy.rb +25 -5
- data/lib/pakyow/data/result.rb +3 -0
- data/lib/pakyow/data/sources/{abstract.rb → base.rb} +2 -1
- data/lib/pakyow/data/sources/ephemeral.rb +2 -2
- data/lib/pakyow/data/sources/relational/association.rb +4 -0
- data/lib/pakyow/data/sources/relational/associations/belongs_to.rb +1 -0
- data/lib/pakyow/data/sources/relational/associations/has_many.rb +1 -0
- data/lib/pakyow/data/sources/relational/associations/has_one.rb +1 -0
- data/lib/pakyow/data/sources/relational/associations/through.rb +1 -0
- data/lib/pakyow/data/sources/relational/command.rb +15 -14
- data/lib/pakyow/data/sources/relational.rb +10 -10
- data/lib/pakyow/data/subscribers/adapters/redis.rb +11 -4
- data/lib/pakyow/data/subscribers.rb +50 -20
- data/lib/pakyow/data.rb +10 -10
- data/lib/pakyow/tasks/info/sources.rake +64 -0
- data/lib/pakyow/validations/data/unique.rb +35 -0
- metadata +47 -33
- data/lib/pakyow/data/behavior/lookup.rb +0 -75
- data/lib/pakyow/data/behavior/serialization.rb +0 -40
- data/lib/pakyow/data/helpers.rb +0 -11
- data/lib/pakyow/environment/data/config.rb +0 -54
- data/lib/pakyow/validations/unique.rb +0 -26
- /data/lib/pakyow/{data/tasks → tasks/db}/bootstrap.rake +0 -0
- /data/lib/pakyow/{data/tasks → tasks/db}/create.rake +0 -0
- /data/lib/pakyow/{data/tasks → tasks/db}/drop.rake +0 -0
- /data/lib/pakyow/{data/tasks → tasks/db}/finalize.rake +0 -0
- /data/lib/pakyow/{data/tasks → tasks/db}/migrate.rake +0 -0
- /data/lib/pakyow/{data/tasks → tasks/db}/reset.rake +0 -0
|
@@ -12,8 +12,8 @@ module Pakyow
|
|
|
12
12
|
using Support::DeepDup
|
|
13
13
|
using Support::Refinements::Array::Ensurable
|
|
14
14
|
|
|
15
|
-
def initialize(name, block:, source:, provides_dataset:,
|
|
16
|
-
@name, @block, @source, @provides_dataset, @
|
|
15
|
+
def initialize(name, block:, source:, provides_dataset:, creates:, updates:, deletes:)
|
|
16
|
+
@name, @block, @source, @provides_dataset, @creates, @updates, @deletes = name, block, source, provides_dataset, creates, updates, deletes
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def call(values = {})
|
|
@@ -24,7 +24,7 @@ module Pakyow
|
|
|
24
24
|
#
|
|
25
25
|
@source.class.attributes.each do |attribute_name, attribute|
|
|
26
26
|
if attribute.meta[:required]
|
|
27
|
-
if @
|
|
27
|
+
if @creates && !values.include?(attribute_name)
|
|
28
28
|
raise NotNullViolation.new_with_message(attribute: attribute_name)
|
|
29
29
|
end
|
|
30
30
|
|
|
@@ -66,7 +66,7 @@ module Pakyow
|
|
|
66
66
|
# Update timestamp fields.
|
|
67
67
|
#
|
|
68
68
|
if timestamp_fields = @source.class.timestamp_fields
|
|
69
|
-
if @
|
|
69
|
+
if @creates
|
|
70
70
|
timestamp_fields.values.each do |timestamp_field|
|
|
71
71
|
final_values[timestamp_field] = Time.now
|
|
72
72
|
end
|
|
@@ -77,7 +77,7 @@ module Pakyow
|
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
if @
|
|
80
|
+
if @creates
|
|
81
81
|
# Set default values.
|
|
82
82
|
#
|
|
83
83
|
@source.class.attributes.each do |attribute_name, attribute|
|
|
@@ -102,7 +102,7 @@ module Pakyow
|
|
|
102
102
|
case association_value
|
|
103
103
|
when Proxy
|
|
104
104
|
if association_value.source.class == association.associated_source
|
|
105
|
-
if association.result_type == :one && (association_value.count > 1 || (@
|
|
105
|
+
if association.result_type == :one && (association_value.count > 1 || (@updates && @source.count > 1))
|
|
106
106
|
raise ConstraintViolation.new_with_message(
|
|
107
107
|
:associate_multiple,
|
|
108
108
|
association: association.name
|
|
@@ -118,7 +118,7 @@ module Pakyow
|
|
|
118
118
|
when Object
|
|
119
119
|
if association.result_type == :one
|
|
120
120
|
if association_value.originating_source
|
|
121
|
-
if association_value.originating_source == association.associated_source
|
|
121
|
+
if association_value.originating_source.__object_name.name == association.associated_source.__object_name.name
|
|
122
122
|
if association.associated_source.instance.send(:"by_#{association.associated_source.primary_key_field}", association_value[association.associated_source.primary_key_field]).count == 0
|
|
123
123
|
raise ConstraintViolation.new_with_message(
|
|
124
124
|
:associate_missing,
|
|
@@ -244,20 +244,20 @@ module Pakyow
|
|
|
244
244
|
end
|
|
245
245
|
end
|
|
246
246
|
|
|
247
|
-
original_dataset = if @
|
|
247
|
+
original_dataset = if @updates
|
|
248
248
|
# Hold on to the original values so we can update them locally.
|
|
249
249
|
@source.dup.to_a
|
|
250
250
|
else
|
|
251
251
|
nil
|
|
252
252
|
end
|
|
253
253
|
|
|
254
|
-
unless @provides_dataset || @
|
|
254
|
+
unless @provides_dataset || @updates
|
|
255
255
|
# Cache the result prior to running the command.
|
|
256
256
|
@source.to_a
|
|
257
257
|
end
|
|
258
258
|
|
|
259
259
|
@source.transaction do
|
|
260
|
-
if @
|
|
260
|
+
if @deletes
|
|
261
261
|
@source.class.associations.values.flatten.select(&:dependents?).each do |association|
|
|
262
262
|
dependent_values = @source.class.container.connection.adapter.restrict_to_attribute(
|
|
263
263
|
@source.class.primary_key_field, @source
|
|
@@ -326,12 +326,13 @@ module Pakyow
|
|
|
326
326
|
end
|
|
327
327
|
end
|
|
328
328
|
|
|
329
|
-
if @
|
|
329
|
+
if @creates || @updates
|
|
330
330
|
# Ensure that has_one associations only have one associated object.
|
|
331
331
|
#
|
|
332
332
|
@source.class.associations[:belongs_to].flat_map { |belongs_to_association|
|
|
333
333
|
belongs_to_association.associated_source.associations[:has_one].select { |has_one_association|
|
|
334
|
-
has_one_association.
|
|
334
|
+
has_one_association.associated_source == @source.class &&
|
|
335
|
+
has_one_association.associated_query_field == belongs_to_association.query_field
|
|
335
336
|
}
|
|
336
337
|
}.each do |association|
|
|
337
338
|
value = final_values.dig(
|
|
@@ -354,7 +355,7 @@ module Pakyow
|
|
|
354
355
|
|
|
355
356
|
command_result = @source.instance_exec(final_values, &@block)
|
|
356
357
|
|
|
357
|
-
final_result = if @
|
|
358
|
+
final_result = if @updates
|
|
358
359
|
# For updates, we fetch the values prior to performing the update and
|
|
359
360
|
# return a source containing locally updated values. This lets us see
|
|
360
361
|
# the original values but prevents us from fetching twice.
|
|
@@ -382,7 +383,7 @@ module Pakyow
|
|
|
382
383
|
@source
|
|
383
384
|
end
|
|
384
385
|
|
|
385
|
-
if @
|
|
386
|
+
if @creates || @updates
|
|
386
387
|
# Update records associated with the data we just changed.
|
|
387
388
|
#
|
|
388
389
|
future_associated_changes.each do |association, association_value|
|
|
@@ -4,7 +4,7 @@ require "pakyow/support/makeable"
|
|
|
4
4
|
require "pakyow/support/class_state"
|
|
5
5
|
require "pakyow/support/inflector"
|
|
6
6
|
|
|
7
|
-
require "pakyow/data/sources/
|
|
7
|
+
require "pakyow/data/sources/base"
|
|
8
8
|
|
|
9
9
|
module Pakyow
|
|
10
10
|
module Data
|
|
@@ -54,7 +54,7 @@ module Pakyow
|
|
|
54
54
|
# data.posts.by_id(1).first
|
|
55
55
|
# => #<Pakyow::Data::Object @values={:id => 1, :title => "foo", :created_at => "2018-11-30 10:55:05 -0800", :updated_at => "2018-11-30 10:55:05 -0800"}>
|
|
56
56
|
#
|
|
57
|
-
class Relational < Sources::
|
|
57
|
+
class Relational < Sources::Base
|
|
58
58
|
require "pakyow/data/sources/relational/associations/belongs_to"
|
|
59
59
|
require "pakyow/data/sources/relational/associations/has_many"
|
|
60
60
|
require "pakyow/data/sources/relational/associations/has_one"
|
|
@@ -188,9 +188,9 @@ module Pakyow
|
|
|
188
188
|
block: command[:block],
|
|
189
189
|
source: self,
|
|
190
190
|
provides_dataset: command[:provides_dataset],
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
191
|
+
creates: command[:creates],
|
|
192
|
+
updates: command[:updates],
|
|
193
|
+
deletes: command[:deletes]
|
|
194
194
|
)
|
|
195
195
|
else
|
|
196
196
|
raise(
|
|
@@ -418,20 +418,20 @@ module Pakyow
|
|
|
418
418
|
class_state :timestamp_fields
|
|
419
419
|
class_state :primary_key_field
|
|
420
420
|
class_state :attributes, default: {}
|
|
421
|
-
class_state :qualifications, default: {},
|
|
421
|
+
class_state :qualifications, default: {}, reader: false
|
|
422
422
|
class_state :associations, default: { belongs_to: [], has_many: [], has_one: [] }
|
|
423
423
|
class_state :commands, default: {}
|
|
424
424
|
|
|
425
425
|
class << self
|
|
426
426
|
attr_reader :name, :adapter, :connection
|
|
427
427
|
|
|
428
|
-
def command(command_name, provides_dataset: true,
|
|
428
|
+
def command(command_name, provides_dataset: true, creates: false, updates: false, deletes: false, &block)
|
|
429
429
|
@commands[command_name] = {
|
|
430
430
|
block: block,
|
|
431
431
|
provides_dataset: provides_dataset,
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
432
|
+
creates: creates,
|
|
433
|
+
updates: updates,
|
|
434
|
+
deletes: deletes
|
|
435
435
|
}
|
|
436
436
|
end
|
|
437
437
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "digest/sha1"
|
|
4
|
+
require "zlib"
|
|
4
5
|
|
|
5
6
|
require "redis"
|
|
6
7
|
require "concurrent/timer_task"
|
|
@@ -20,7 +21,7 @@ module Pakyow
|
|
|
20
21
|
class Redis
|
|
21
22
|
class << self
|
|
22
23
|
def stringify_subscription(subscription)
|
|
23
|
-
Marshal.dump(subscription)
|
|
24
|
+
Zlib::Deflate.deflate(Marshal.dump(subscription))
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
def generate_subscription_id(subscription_string)
|
|
@@ -137,9 +138,15 @@ module Pakyow
|
|
|
137
138
|
def cleanup
|
|
138
139
|
@redis.with do |redis|
|
|
139
140
|
redis.scan_each(match: key_subscription_ids_by_source("*")) do |key|
|
|
140
|
-
Pakyow.logger.
|
|
141
|
+
Pakyow.logger.internal {
|
|
142
|
+
"[Pakyow::Data::Subscribers::Adapters::Redis] Cleaning up expired subscriptions for #{key}"
|
|
143
|
+
}
|
|
144
|
+
|
|
141
145
|
removed_count = redis.zremrangebyscore(key, 0, Time.now.to_i)
|
|
142
|
-
|
|
146
|
+
|
|
147
|
+
Pakyow.logger.internal {
|
|
148
|
+
"[Pakyow::Data::Subscribers::Adapters::Redis] Removed #{removed_count} members for #{key}"
|
|
149
|
+
}
|
|
143
150
|
end
|
|
144
151
|
end
|
|
145
152
|
end
|
|
@@ -154,7 +161,7 @@ module Pakyow
|
|
|
154
161
|
key_subscription_id(subscription_id)
|
|
155
162
|
}).zip(subscription_ids).map { |subscription_string, subscription_id|
|
|
156
163
|
begin
|
|
157
|
-
Marshal.restore(subscription_string).tap do |subscription|
|
|
164
|
+
Marshal.restore(Zlib::Inflate.inflate(subscription_string)).tap do |subscription|
|
|
158
165
|
subscription[:id] = subscription_id
|
|
159
166
|
end
|
|
160
167
|
rescue TypeError
|
|
@@ -44,31 +44,55 @@ module Pakyow
|
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
def register_subscriptions(subscriptions, subscriber: nil, &block)
|
|
47
|
-
|
|
48
|
-
subscriptions.
|
|
49
|
-
|
|
50
|
-
end
|
|
47
|
+
subscriptions.each do |subscription|
|
|
48
|
+
subscription[:version] = @app.config.data.subscriptions.version
|
|
49
|
+
end
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
51
|
+
@adapter.register_subscriptions(subscriptions, subscriber: subscriber).tap do |ids|
|
|
52
|
+
yield ids if block_given?
|
|
53
|
+
end
|
|
56
54
|
end
|
|
57
55
|
|
|
58
56
|
def did_mutate(source_name, changed_values = nil, result_source = nil)
|
|
59
|
-
@executor
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
@executor.post(source_name, changed_values, result_source, Pakyow.logger.target) do |source_name, changed_values, result_source, logger|
|
|
58
|
+
logger.internal {
|
|
59
|
+
"[Pakyow::Data::Subscribers] did mutate #{source_name}"
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
subscriptions = @adapter.subscriptions_for_source(source_name)
|
|
63
|
+
|
|
64
|
+
logger.internal {
|
|
65
|
+
"[Pakyow::Data::Subscribers] fetched #{subscriptions.count} subscriptions"
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
subscriptions.uniq { |subscription|
|
|
69
|
+
subscription.dig(:payload, :id) || subscription
|
|
70
|
+
}.select { |subscription|
|
|
71
|
+
process?(subscription, changed_values, result_source)
|
|
72
|
+
}.each do |subscription|
|
|
73
|
+
if subscription[:version] == @app.config.data.subscriptions.version
|
|
74
|
+
begin
|
|
75
|
+
logger.internal {
|
|
76
|
+
"[Pakyow::Data::Subscribers] processing subscription #{subscription[:id]}"
|
|
77
|
+
}
|
|
78
|
+
|
|
65
79
|
process(subscription, result_source)
|
|
80
|
+
|
|
81
|
+
logger.internal {
|
|
82
|
+
"[Pakyow::Data::Subscribers] finished processing subscription #{subscription[:id]}"
|
|
83
|
+
}
|
|
84
|
+
rescue => error
|
|
85
|
+
logger.error {
|
|
86
|
+
"[Pakyow::Data::Subscribers] did_mutate failed: #{error}"
|
|
87
|
+
}
|
|
66
88
|
end
|
|
67
89
|
end
|
|
68
|
-
rescue => error
|
|
69
|
-
Pakyow.logger.error "[Pakyow::Data::Subscribers] did_mutate failed: #{error}"
|
|
70
90
|
end
|
|
71
|
-
|
|
91
|
+
|
|
92
|
+
logger.internal {
|
|
93
|
+
"[Pakyow::Data::Subscribers] finished mutate for #{source_name}"
|
|
94
|
+
}
|
|
95
|
+
end
|
|
72
96
|
end
|
|
73
97
|
|
|
74
98
|
def unsubscribe(subscriber)
|
|
@@ -131,16 +155,22 @@ module Pakyow
|
|
|
131
155
|
end
|
|
132
156
|
end
|
|
133
157
|
|
|
134
|
-
QUALIFIABLE_TYPES = [Hash, Support::IndifferentHash]
|
|
158
|
+
QUALIFIABLE_TYPES = [Hash, Support::IndifferentHash].freeze
|
|
135
159
|
def qualified?(qualifications, changed_values, changed_results, original_results)
|
|
136
160
|
qualifications.all? do |key, value|
|
|
137
|
-
(
|
|
161
|
+
(
|
|
162
|
+
QUALIFIABLE_TYPES.include?(changed_values.class) && (
|
|
163
|
+
(
|
|
164
|
+
value.is_a?(Array) && value.include?(changed_values.to_h[key])
|
|
165
|
+
) || changed_values.to_h[key] == value
|
|
166
|
+
)
|
|
167
|
+
) || qualified_result?(key, value, changed_results, original_results)
|
|
138
168
|
end
|
|
139
169
|
end
|
|
140
170
|
|
|
141
171
|
def qualified_result?(key, value, changed_results, original_results)
|
|
142
172
|
original_results.concat(changed_results).any? do |result|
|
|
143
|
-
result[key] == value
|
|
173
|
+
(value.is_a?(Array) && value.include?(result[key])) || result[key] == value
|
|
144
174
|
end
|
|
145
175
|
end
|
|
146
176
|
end
|
data/lib/pakyow/data.rb
CHANGED
|
@@ -10,18 +10,18 @@ Pakyow::Data::Connection.register_adapter :sql
|
|
|
10
10
|
require "pakyow/data/errors"
|
|
11
11
|
require "pakyow/data/framework"
|
|
12
12
|
|
|
13
|
-
require "pakyow/
|
|
14
|
-
require "pakyow/
|
|
15
|
-
require "pakyow/
|
|
16
|
-
require "pakyow/
|
|
13
|
+
require "pakyow/behavior/data/auto_migrate"
|
|
14
|
+
require "pakyow/behavior/data/connections"
|
|
15
|
+
require "pakyow/behavior/data/memory_db"
|
|
16
|
+
require "pakyow/config/data"
|
|
17
17
|
|
|
18
|
-
require "pakyow/validations/unique"
|
|
18
|
+
require "pakyow/validations/data/unique"
|
|
19
19
|
|
|
20
20
|
module Pakyow
|
|
21
|
-
config.tasks.paths << File.expand_path("../
|
|
21
|
+
config.tasks.paths << File.expand_path("../tasks", __FILE__)
|
|
22
22
|
|
|
23
|
-
include
|
|
24
|
-
include
|
|
25
|
-
include
|
|
26
|
-
include
|
|
23
|
+
include Behavior::Data::AutoMigrate
|
|
24
|
+
include Behavior::Data::Connections
|
|
25
|
+
include Behavior::Data::MemoryDB
|
|
26
|
+
include Config::Data
|
|
27
27
|
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pakyow/support/cli/style"
|
|
4
|
+
require "pakyow/support/dependencies"
|
|
5
|
+
|
|
6
|
+
desc "Show defined sources for an app"
|
|
7
|
+
namespace :info do
|
|
8
|
+
task :sources, [:app] do |_, args|
|
|
9
|
+
sources = args[:app].state(:source).sort_by { |source|
|
|
10
|
+
source.plural_name
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
source_names = sources.map { |source|
|
|
14
|
+
source.plural_name
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
source_locations = sources.map { |source|
|
|
18
|
+
source_location = source.__source_location.to_a.join(":")
|
|
19
|
+
|
|
20
|
+
source_location = if source_location.empty?
|
|
21
|
+
"unknown"
|
|
22
|
+
else
|
|
23
|
+
Pakyow::Support::Dependencies.library_name(source_location) ||
|
|
24
|
+
Pakyow::Support::Dependencies.strip_path_prefix(source_location)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
source_location = if source_location.start_with?("pakyow-")
|
|
28
|
+
Pakyow::Support::CLI.style.blue(
|
|
29
|
+
"pakyow/#{source_location.split("pakyow-", 2)[1]}"
|
|
30
|
+
)
|
|
31
|
+
elsif source_location == "unknown"
|
|
32
|
+
Pakyow::Support::CLI.style.italic(source_location)
|
|
33
|
+
else
|
|
34
|
+
source_location
|
|
35
|
+
end
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
sources.each_with_index do |source, i|
|
|
39
|
+
puts Pakyow::Support::CLI.style.bold(source_names[i].inspect) + " #{source_locations[i]}"
|
|
40
|
+
|
|
41
|
+
source.associations.each do |association_type, associations|
|
|
42
|
+
associations.each do |association|
|
|
43
|
+
puts " #{association_type} #{association.name.inspect}"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
if source.attributes.any? && source.associations.values.flatten.any?
|
|
48
|
+
puts
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
if source.attributes.any?
|
|
52
|
+
attributes = source.attributes.sort_by { |attribute_name, _|
|
|
53
|
+
attribute_name
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
attributes.each do |attribute_name, attribute_type|
|
|
57
|
+
puts " attribute #{attribute_name.inspect}, #{attribute_type.meta[:mapping].inspect}"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
puts
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pakyow/validator"
|
|
4
|
+
|
|
5
|
+
module Pakyow
|
|
6
|
+
module Validations
|
|
7
|
+
module Data
|
|
8
|
+
# Validates that the value is unique within its data source.
|
|
9
|
+
#
|
|
10
|
+
module Unique
|
|
11
|
+
def self.message(**)
|
|
12
|
+
"must be unique"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.valid?(value, source:, **options)
|
|
16
|
+
query = options[:context].app.data.public_send(source).public_send(:"by_#{options[:key]}", value)
|
|
17
|
+
|
|
18
|
+
if updating = options[:updating]
|
|
19
|
+
if updating.is_a?(Pakyow::Data::Result)
|
|
20
|
+
query.count == 0 || query.any? { |result|
|
|
21
|
+
result[updating.__proxy.source.class.primary_key_field] == updating[updating.__proxy.source.class.primary_key_field]
|
|
22
|
+
}
|
|
23
|
+
else
|
|
24
|
+
raise ArgumentError, "Expected `#{updating.class}' to be a `Pakyow::Data::Result'"
|
|
25
|
+
end
|
|
26
|
+
else
|
|
27
|
+
query.count == 0
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
Validator.register_validation(Unique, :unique)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pakyow-data
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.0
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bryan Powell
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2019-07-
|
|
11
|
+
date: 2019-07-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: pakyow-core
|
|
@@ -16,28 +16,28 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - '='
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 1.0.0
|
|
19
|
+
version: 1.0.0
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - '='
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 1.0.0
|
|
26
|
+
version: 1.0.0
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: pakyow-support
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - '='
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 1.0.0
|
|
33
|
+
version: 1.0.0
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - '='
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 1.0.0
|
|
40
|
+
version: 1.0.0
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: concurrent-ruby
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -72,30 +72,44 @@ dependencies:
|
|
|
72
72
|
requirements:
|
|
73
73
|
- - "~>"
|
|
74
74
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: '1.
|
|
75
|
+
version: '1.1'
|
|
76
|
+
type: :runtime
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '1.1'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: redis
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '4.1'
|
|
76
90
|
type: :runtime
|
|
77
91
|
prerelease: false
|
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
93
|
requirements:
|
|
80
94
|
- - "~>"
|
|
81
95
|
- !ruby/object:Gem::Version
|
|
82
|
-
version: '1
|
|
96
|
+
version: '4.1'
|
|
83
97
|
- !ruby/object:Gem::Dependency
|
|
84
98
|
name: sequel
|
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
|
86
100
|
requirements:
|
|
87
101
|
- - "~>"
|
|
88
102
|
- !ruby/object:Gem::Version
|
|
89
|
-
version: '5.
|
|
103
|
+
version: '5.22'
|
|
90
104
|
type: :runtime
|
|
91
105
|
prerelease: false
|
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
|
93
107
|
requirements:
|
|
94
108
|
- - "~>"
|
|
95
109
|
- !ruby/object:Gem::Version
|
|
96
|
-
version: '5.
|
|
110
|
+
version: '5.22'
|
|
97
111
|
description: Data persistence layer for Pakyow
|
|
98
|
-
email: bryan@
|
|
112
|
+
email: bryan@bryanp.org
|
|
99
113
|
executables: []
|
|
100
114
|
extensions: []
|
|
101
115
|
extra_rdoc_files: []
|
|
@@ -103,8 +117,16 @@ files:
|
|
|
103
117
|
- CHANGELOG.md
|
|
104
118
|
- LICENSE
|
|
105
119
|
- README.md
|
|
120
|
+
- lib/pakyow/application/behavior/data/lookup.rb
|
|
121
|
+
- lib/pakyow/application/behavior/data/serialization.rb
|
|
122
|
+
- lib/pakyow/application/config/data.rb
|
|
123
|
+
- lib/pakyow/application/helpers/data.rb
|
|
124
|
+
- lib/pakyow/behavior/data/auto_migrate.rb
|
|
125
|
+
- lib/pakyow/behavior/data/connections.rb
|
|
126
|
+
- lib/pakyow/behavior/data/memory_db.rb
|
|
127
|
+
- lib/pakyow/config/data.rb
|
|
106
128
|
- lib/pakyow/data.rb
|
|
107
|
-
- lib/pakyow/data/adapters/
|
|
129
|
+
- lib/pakyow/data/adapters/base.rb
|
|
108
130
|
- lib/pakyow/data/adapters/sql.rb
|
|
109
131
|
- lib/pakyow/data/adapters/sql/commands.rb
|
|
110
132
|
- lib/pakyow/data/adapters/sql/dataset_methods.rb
|
|
@@ -116,20 +138,16 @@ files:
|
|
|
116
138
|
- lib/pakyow/data/adapters/sql/runner.rb
|
|
117
139
|
- lib/pakyow/data/adapters/sql/source_extension.rb
|
|
118
140
|
- lib/pakyow/data/adapters/sql/types.rb
|
|
119
|
-
- lib/pakyow/data/behavior/config.rb
|
|
120
|
-
- lib/pakyow/data/behavior/lookup.rb
|
|
121
|
-
- lib/pakyow/data/behavior/serialization.rb
|
|
122
141
|
- lib/pakyow/data/connection.rb
|
|
123
142
|
- lib/pakyow/data/container.rb
|
|
124
143
|
- lib/pakyow/data/errors.rb
|
|
125
144
|
- lib/pakyow/data/framework.rb
|
|
126
|
-
- lib/pakyow/data/helpers.rb
|
|
127
145
|
- lib/pakyow/data/lookup.rb
|
|
128
146
|
- lib/pakyow/data/migrator.rb
|
|
129
147
|
- lib/pakyow/data/object.rb
|
|
130
148
|
- lib/pakyow/data/proxy.rb
|
|
131
149
|
- lib/pakyow/data/result.rb
|
|
132
|
-
- lib/pakyow/data/sources/
|
|
150
|
+
- lib/pakyow/data/sources/base.rb
|
|
133
151
|
- lib/pakyow/data/sources/ephemeral.rb
|
|
134
152
|
- lib/pakyow/data/sources/relational.rb
|
|
135
153
|
- lib/pakyow/data/sources/relational/association.rb
|
|
@@ -147,19 +165,16 @@ files:
|
|
|
147
165
|
- lib/pakyow/data/subscribers/adapters/redis/scripts/expire.lua
|
|
148
166
|
- lib/pakyow/data/subscribers/adapters/redis/scripts/persist.lua
|
|
149
167
|
- lib/pakyow/data/subscribers/adapters/redis/scripts/register.lua
|
|
150
|
-
- lib/pakyow/data/tasks/bootstrap.rake
|
|
151
|
-
- lib/pakyow/data/tasks/create.rake
|
|
152
|
-
- lib/pakyow/data/tasks/drop.rake
|
|
153
|
-
- lib/pakyow/data/tasks/finalize.rake
|
|
154
|
-
- lib/pakyow/data/tasks/migrate.rake
|
|
155
|
-
- lib/pakyow/data/tasks/reset.rake
|
|
156
168
|
- lib/pakyow/data/types.rb
|
|
157
|
-
- lib/pakyow/
|
|
158
|
-
- lib/pakyow/
|
|
159
|
-
- lib/pakyow/
|
|
160
|
-
- lib/pakyow/
|
|
161
|
-
- lib/pakyow/
|
|
162
|
-
|
|
169
|
+
- lib/pakyow/tasks/db/bootstrap.rake
|
|
170
|
+
- lib/pakyow/tasks/db/create.rake
|
|
171
|
+
- lib/pakyow/tasks/db/drop.rake
|
|
172
|
+
- lib/pakyow/tasks/db/finalize.rake
|
|
173
|
+
- lib/pakyow/tasks/db/migrate.rake
|
|
174
|
+
- lib/pakyow/tasks/db/reset.rake
|
|
175
|
+
- lib/pakyow/tasks/info/sources.rake
|
|
176
|
+
- lib/pakyow/validations/data/unique.rb
|
|
177
|
+
homepage: https://pakyow.com
|
|
163
178
|
licenses:
|
|
164
179
|
- LGPL-3.0
|
|
165
180
|
metadata: {}
|
|
@@ -174,12 +189,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
174
189
|
version: 2.5.0
|
|
175
190
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
191
|
requirements:
|
|
177
|
-
- - "
|
|
192
|
+
- - ">="
|
|
178
193
|
- !ruby/object:Gem::Version
|
|
179
|
-
version:
|
|
194
|
+
version: '0'
|
|
180
195
|
requirements: []
|
|
181
|
-
|
|
182
|
-
rubygems_version: 2.7.6
|
|
196
|
+
rubygems_version: 3.0.3
|
|
183
197
|
signing_key:
|
|
184
198
|
specification_version: 4
|
|
185
199
|
summary: Pakyow Data
|