garland 0.13 → 1.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/lib/garland_rails.rb +28 -28
- data/lib/generators/garland_rails/install_generator.rb +35 -0
- data/lib/generators/garland_rails/templates/install_migration.rb +14 -0
- data/lib/generators/garland_rails/templates/uninstall_migration.rb +5 -0
- data/lib/generators/garland_rails/templates/v0_to_v1_migration.rb +6 -0
- data/lib/generators/garland_rails/uninstall_generator.rb +35 -0
- data/lib/generators/garland_rails/upgrade_generator.rb +35 -0
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6acc1a7f250514377aac16a344919064b6aa5a4e
|
4
|
+
data.tar.gz: a628e61d15c72b9959ee18e0eef57dbe0ffb5255
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5924e559189b60bf41146286c428ad23ca9826a40c46ac44dc5502c1b0a3a99c345454413d1f8c79720ea9cd8bfc74b05a37135fb17db700e4eb76d8aa92d684
|
7
|
+
data.tar.gz: c94774db93f0b55a547f432ea062d476b2af5c660778b65820693a8c6cec1892e1bbbc70ef3296a396d81ec2a32e9220cafefc668e39bcde1abf963a87f6d273
|
data/lib/garland_rails.rb
CHANGED
@@ -19,7 +19,7 @@ module GarlandRails
|
|
19
19
|
|
20
20
|
belongs_to_type = self.name
|
21
21
|
scope = -> { where(belongs_to_type: belongs_to_type) }
|
22
|
-
options = options.merge(
|
22
|
+
options = options.merge(foreign_key: "belongs_to_id")
|
23
23
|
end
|
24
24
|
|
25
25
|
super(name, scope, options, &extension)
|
@@ -37,8 +37,8 @@ module GarlandRails
|
|
37
37
|
validates_uniqueness_of :type, scope: [
|
38
38
|
:belongs_to_id,
|
39
39
|
:belongs_to_type,
|
40
|
-
:
|
41
|
-
], conditions: -> { where("
|
40
|
+
:next_id,
|
41
|
+
], conditions: -> { where("next_id IS NULL OR previous_id IS NULL") }
|
42
42
|
|
43
43
|
# everything seems to work without `polymorphic: true`,
|
44
44
|
# but we will set it just for accordance to docs
|
@@ -47,7 +47,7 @@ module GarlandRails
|
|
47
47
|
options = scope
|
48
48
|
scope = nil
|
49
49
|
end
|
50
|
-
options = options.merge(
|
50
|
+
options = options.merge(foreign_key: "belongs_to_id")
|
51
51
|
|
52
52
|
super(name, scope, options, &extension)
|
53
53
|
end
|
@@ -62,8 +62,8 @@ module GarlandRails
|
|
62
62
|
belongs_to = args[:belongs_to]
|
63
63
|
if belongs_to
|
64
64
|
belongs_to_params = self._split_belongs_to(belongs_to)
|
65
|
-
belongs_to_id =
|
66
|
-
|
65
|
+
belongs_to_id, belongs_to_type =
|
66
|
+
belongs_to_params.values_at(:belongs_to_id, :belongs_to_type)
|
67
67
|
end
|
68
68
|
else
|
69
69
|
hash = args
|
@@ -94,17 +94,17 @@ module GarlandRails
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def self.tail(belongs_to = nil)
|
97
|
-
self.thread(belongs_to).find_by(
|
97
|
+
self.thread(belongs_to).find_by(previous_id: nil)
|
98
98
|
end
|
99
99
|
|
100
100
|
def self.head(belongs_to = nil)
|
101
|
-
self.thread(belongs_to).find_by(
|
101
|
+
self.thread(belongs_to).find_by(next_id: nil)
|
102
102
|
end
|
103
103
|
|
104
104
|
def self.last_diff(belongs_to = nil)
|
105
105
|
head = self.head(belongs_to)
|
106
106
|
|
107
|
-
self.find_by(id: head.
|
107
|
+
self.find_by(id: head.previous_id)
|
108
108
|
end
|
109
109
|
|
110
110
|
def self.table_type(record)
|
@@ -118,14 +118,14 @@ module GarlandRails
|
|
118
118
|
def self.init(hash, belongs_to = nil)
|
119
119
|
common_props = self._split_belongs_to(belongs_to)
|
120
120
|
|
121
|
-
tail_props = common_props.merge(
|
121
|
+
tail_props = common_props.merge(entity: {}.to_s, entity_type: SNAPSHOT)
|
122
122
|
brand_new_tail = self.new(tail_props)
|
123
123
|
|
124
124
|
diff = HashDiffSym.diff({}, hash)
|
125
|
-
first_diff_props = common_props.merge(
|
125
|
+
first_diff_props = common_props.merge(entity: diff.to_s, entity_type: DIFF)
|
126
126
|
first_diff = self.new(first_diff_props)
|
127
127
|
|
128
|
-
head_props = common_props.merge(
|
128
|
+
head_props = common_props.merge(entity: hash.to_s, entity_type: SNAPSHOT)
|
129
129
|
brand_new_head = self.new(head_props)
|
130
130
|
|
131
131
|
self.transaction do
|
@@ -144,10 +144,10 @@ module GarlandRails
|
|
144
144
|
# here and below validations may be skipped as long as we check for continuity later
|
145
145
|
brand_new_head.save(validate: false)
|
146
146
|
first_diff.save(validate: false)
|
147
|
-
brand_new_tail.update_attribute(:
|
148
|
-
first_diff.update_attribute(:
|
149
|
-
first_diff.update_attribute(:
|
150
|
-
brand_new_head.update_attribute(:
|
147
|
+
brand_new_tail.update_attribute(:next_id, first_diff.id)
|
148
|
+
first_diff.update_attribute(:previous_id, brand_new_tail.id)
|
149
|
+
first_diff.update_attribute(:next_id, brand_new_head.id)
|
150
|
+
brand_new_head.update_attribute(:previous_id, first_diff.id)
|
151
151
|
|
152
152
|
unless self.continuous?(belongs_to)
|
153
153
|
Rails.logger.error("Initialized garland is not continuous")
|
@@ -162,18 +162,18 @@ module GarlandRails
|
|
162
162
|
|
163
163
|
def self.insert_diff(hash, belongs_to = nil)
|
164
164
|
head = self.head(belongs_to)
|
165
|
-
last_diff = self.find_by(id: head.
|
165
|
+
last_diff = self.find_by(id: head.previous_id)
|
166
166
|
common_props = self._split_belongs_to(belongs_to)
|
167
167
|
|
168
168
|
diff = HashDiffSym.diff(eval(head.entity), hash)
|
169
169
|
return unless diff.any?
|
170
170
|
|
171
|
-
new_diff_props = common_props.merge(
|
171
|
+
new_diff_props = common_props.merge(
|
172
172
|
entity: diff.to_s,
|
173
173
|
entity_type: DIFF,
|
174
|
-
|
175
|
-
|
176
|
-
|
174
|
+
previous_id: last_diff.id,
|
175
|
+
next_id: head.id,
|
176
|
+
)
|
177
177
|
new_diff = self.new(new_diff_props)
|
178
178
|
|
179
179
|
self.transaction do
|
@@ -187,17 +187,17 @@ module GarlandRails
|
|
187
187
|
return nil
|
188
188
|
end
|
189
189
|
|
190
|
-
last_diff.
|
190
|
+
last_diff.next_id = new_diff.id
|
191
191
|
unless last_diff.save
|
192
|
-
Rails.logger.error("Unable to save last_diff with '
|
192
|
+
Rails.logger.error("Unable to save last_diff with 'next_id' = '#{new_diff.id}'")
|
193
193
|
ActiveRecord::Base.connection.exec_rollback_to_savepoint("savepoint_before_insert_diff")
|
194
194
|
ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
|
195
195
|
return nil
|
196
196
|
end
|
197
197
|
|
198
|
-
head.
|
198
|
+
head.previous_id = new_diff.id
|
199
199
|
unless head.save
|
200
|
-
Rails.logger.error("Unable to save head with '
|
200
|
+
Rails.logger.error("Unable to save head with 'previous_id' = '#{new_diff.id}'")
|
201
201
|
ActiveRecord::Base.connection.exec_rollback_to_savepoint("savepoint_before_insert_diff")
|
202
202
|
ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
|
203
203
|
return nil
|
@@ -205,7 +205,7 @@ module GarlandRails
|
|
205
205
|
|
206
206
|
head.entity = hash.to_s
|
207
207
|
unless head.save
|
208
|
-
Rails.logger.error("Unable to save head with 'entity' = '#{hash
|
208
|
+
Rails.logger.error("Unable to save head with 'entity' = '#{hash}'")
|
209
209
|
ActiveRecord::Base.connection.exec_rollback_to_savepoint("savepoint_before_insert_diff")
|
210
210
|
ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
|
211
211
|
return nil
|
@@ -223,9 +223,9 @@ module GarlandRails
|
|
223
223
|
current_bulb = tail
|
224
224
|
current_hash = eval(tail.entity)
|
225
225
|
items_counted = 1
|
226
|
-
while current_bulb.
|
226
|
+
while current_bulb.next_id do
|
227
227
|
items_counted += 1
|
228
|
-
current_bulb = self.find_by(id: current_bulb.
|
228
|
+
current_bulb = self.find_by(id: current_bulb.next_id)
|
229
229
|
if current_bulb.entity_type == DIFF
|
230
230
|
current_hash = HashDiffSym.patch!(current_hash, eval(current_bulb.entity))
|
231
231
|
else
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Based on
|
2
|
+
# https://github.com/huacnlee/rails-settings-cached/blob/master/lib/generators/settings/install_generator.rb
|
3
|
+
# by Jason "huacnlee" Lee.
|
4
|
+
require "rails/generators"
|
5
|
+
require "rails/generators/migration"
|
6
|
+
|
7
|
+
module GarlandRails
|
8
|
+
# TODO: DRY
|
9
|
+
class InstallGenerator < Rails::Generators::Base
|
10
|
+
desc 'Generate Garland files.'
|
11
|
+
include Rails::Generators::Migration
|
12
|
+
source_root File.expand_path("../templates", __FILE__)
|
13
|
+
|
14
|
+
@@migrations = false
|
15
|
+
|
16
|
+
def self.next_migration_number(dirname) #:nodoc:
|
17
|
+
if ActiveRecord::Base.timestamped_migrations
|
18
|
+
if @@migrations
|
19
|
+
(current_migration_number(dirname) + 1)
|
20
|
+
else
|
21
|
+
@@migrations = true
|
22
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
23
|
+
end
|
24
|
+
else
|
25
|
+
format '%.3d', current_migration_number(dirname) + 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_migration_file
|
30
|
+
source = "install_migration.rb"
|
31
|
+
destination = "db/migrate/install_garland.rb"
|
32
|
+
migration_template source, destination
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class InstallGarland < ActiveRecord::Migration[5.0]
|
2
|
+
def change
|
3
|
+
create_table :garlands do |t|
|
4
|
+
t.text :entity, null: false
|
5
|
+
t.boolean :entity_type, null: false
|
6
|
+
t.integer :previous
|
7
|
+
t.integer :next
|
8
|
+
t.integer :belongs_to_id
|
9
|
+
t.string :belongs_to_type
|
10
|
+
t.string :type
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Based on
|
2
|
+
# https://github.com/huacnlee/rails-settings-cached/blob/master/lib/generators/settings/install_generator.rb
|
3
|
+
# by Jason "huacnlee" Lee.
|
4
|
+
require "rails/generators"
|
5
|
+
require "rails/generators/migration"
|
6
|
+
|
7
|
+
module GarlandRails
|
8
|
+
# TODO: DRY
|
9
|
+
class UninstallGenerator < Rails::Generators::Base
|
10
|
+
desc 'Generate Garland files.'
|
11
|
+
include Rails::Generators::Migration
|
12
|
+
source_root File.expand_path("../templates", __FILE__)
|
13
|
+
|
14
|
+
@@migrations = false
|
15
|
+
|
16
|
+
def self.next_migration_number(dirname) #:nodoc:
|
17
|
+
if ActiveRecord::Base.timestamped_migrations
|
18
|
+
if @@migrations
|
19
|
+
(current_migration_number(dirname) + 1)
|
20
|
+
else
|
21
|
+
@@migrations = true
|
22
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
23
|
+
end
|
24
|
+
else
|
25
|
+
format '%.3d', current_migration_number(dirname) + 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_migration_file
|
30
|
+
source = "uninstall_migration.rb"
|
31
|
+
destination = "db/migrate/uninstall_garland.rb"
|
32
|
+
migration_template source, destination
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Based on
|
2
|
+
# https://github.com/huacnlee/rails-settings-cached/blob/master/lib/generators/settings/install_generator.rb
|
3
|
+
# by Jason "huacnlee" Lee.
|
4
|
+
require "rails/generators"
|
5
|
+
require "rails/generators/migration"
|
6
|
+
|
7
|
+
module GarlandRails
|
8
|
+
# TODO: DRY
|
9
|
+
class UpgradeGenerator < Rails::Generators::NamedBase
|
10
|
+
desc 'Upgrades Garland db.'
|
11
|
+
include Rails::Generators::Migration
|
12
|
+
source_root File.expand_path("../templates", __FILE__)
|
13
|
+
|
14
|
+
@@migrations = false
|
15
|
+
|
16
|
+
def self.next_migration_number(dirname) #:nodoc:
|
17
|
+
if ActiveRecord::Base.timestamped_migrations
|
18
|
+
if @@migrations
|
19
|
+
(current_migration_number(dirname) + 1)
|
20
|
+
else
|
21
|
+
@@migrations = true
|
22
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
23
|
+
end
|
24
|
+
else
|
25
|
+
format '%.3d', current_migration_number(dirname) + 1
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_migration_file
|
30
|
+
source = "#{file_name}_migration.rb"
|
31
|
+
destination = "db/migrate/upgrade_garland_#{file_name}.rb"
|
32
|
+
migration_template source, destination
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: garland
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0
|
4
|
+
version: '1.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Morozov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashdiff_sym
|
@@ -33,6 +33,12 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- lib/garland.rb
|
35
35
|
- lib/garland_rails.rb
|
36
|
+
- lib/generators/garland_rails/install_generator.rb
|
37
|
+
- lib/generators/garland_rails/templates/install_migration.rb
|
38
|
+
- lib/generators/garland_rails/templates/uninstall_migration.rb
|
39
|
+
- lib/generators/garland_rails/templates/v0_to_v1_migration.rb
|
40
|
+
- lib/generators/garland_rails/uninstall_generator.rb
|
41
|
+
- lib/generators/garland_rails/upgrade_generator.rb
|
36
42
|
homepage: https://github.com/kengho/garland
|
37
43
|
licenses:
|
38
44
|
- MIT
|
@@ -56,5 +62,5 @@ rubyforge_project:
|
|
56
62
|
rubygems_version: 2.5.2
|
57
63
|
signing_key:
|
58
64
|
specification_version: 4
|
59
|
-
summary: HashDiff
|
65
|
+
summary: HashDiff Rails storage
|
60
66
|
test_files: []
|