fancybox2 0.0.7 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fancybox2/core_ext/hash.rb +42 -0
- data/lib/fancybox2/migrations/runner.rb +53 -18
- data/lib/fancybox2/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2781b164afddd8b93a721940e22a9edbd70f169fb9e20af7ed0ee9aaf250aa77
|
4
|
+
data.tar.gz: 7eb4002e13fddcb0bf42de55e433ddae0c84c9c3eee53778061095d19479e689
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94d8cdcaf8a4371f2f52c00e990b139650998395701f6f54685d94af3820225bf77e009a976e0f7fec1560e5f9f281a9d1acb4f5dcaf43f60bf38e8b5b74c297
|
7
|
+
data.tar.gz: 694edc4b11c0488edb538e7c3faf8f8b760ce1c4d04bb6e35608bad4007664a4d828b2c1212e699e3ec8eaeac2c4307ef8acfbdc029b76913cb7fc274c9ba7b3
|
@@ -2,6 +2,25 @@
|
|
2
2
|
|
3
3
|
# :nocov:
|
4
4
|
class Hash
|
5
|
+
# Returns a new hash with all values converted by the block operation.
|
6
|
+
# This includes the values from the root hash and from all
|
7
|
+
# nested hashes and arrays.
|
8
|
+
#
|
9
|
+
# hash = { person: { name: 'Rob', age: '28' } }
|
10
|
+
#
|
11
|
+
# hash.deep_transform_values{ |value| value.to_s.upcase }
|
12
|
+
# # => {person: {name: "ROB", age: "28"}}
|
13
|
+
def deep_transform_values(&block)
|
14
|
+
_deep_transform_values_in_object(self, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Destructively converts all values by using the block operation.
|
18
|
+
# This includes the values from the root hash and from all
|
19
|
+
# nested hashes and arrays.
|
20
|
+
def deep_transform_values!(&block)
|
21
|
+
_deep_transform_values_in_object!(self, &block)
|
22
|
+
end
|
23
|
+
|
5
24
|
# Returns a new hash with all keys converted using the block operation.
|
6
25
|
#
|
7
26
|
# hash = { name: 'Rob', age: '28' }
|
@@ -169,4 +188,27 @@ class Hash
|
|
169
188
|
object
|
170
189
|
end
|
171
190
|
end
|
191
|
+
|
192
|
+
# support methods for deep transforming nested hashes and arrays
|
193
|
+
def _deep_transform_values_in_object(object, &block)
|
194
|
+
case object
|
195
|
+
when Hash
|
196
|
+
object.transform_values { |value| _deep_transform_values_in_object(value, &block) }
|
197
|
+
when Array
|
198
|
+
object.map { |e| _deep_transform_values_in_object(e, &block) }
|
199
|
+
else
|
200
|
+
yield(object)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def _deep_transform_values_in_object!(object, &block)
|
205
|
+
case object
|
206
|
+
when Hash
|
207
|
+
object.transform_values! { |value| _deep_transform_values_in_object!(value, &block) }
|
208
|
+
when Array
|
209
|
+
object.map! { |e| _deep_transform_values_in_object!(e, &block) }
|
210
|
+
else
|
211
|
+
yield(object)
|
212
|
+
end
|
213
|
+
end
|
172
214
|
end
|
@@ -3,50 +3,84 @@ module Fancybox2
|
|
3
3
|
class Runner
|
4
4
|
include Exceptions
|
5
5
|
|
6
|
-
VERSION_REGEXP = /^(\d
|
6
|
+
VERSION_REGEXP = /^(\d+)/.freeze
|
7
7
|
|
8
8
|
class << self
|
9
9
|
|
10
10
|
def extract_and_validate_version_from(migration_name)
|
11
11
|
version = migration_name.to_s.scan(VERSION_REGEXP).flatten.first
|
12
12
|
unless version
|
13
|
-
raise ArgumentError, 'migration
|
13
|
+
raise ArgumentError, 'migration name must start with a positive integer number e.g: 1_do_something.rb'
|
14
14
|
end
|
15
15
|
|
16
16
|
version.to_i
|
17
17
|
end
|
18
|
+
|
19
|
+
def auto(migrations_folder, last_migrated_file_path: nil, logger: nil)
|
20
|
+
# Try to read file content, rescue with nil
|
21
|
+
content = File.read(last_migrated_file_path) rescue nil
|
22
|
+
# Extract last run migration version
|
23
|
+
last_run_migration_version = content.to_i
|
24
|
+
|
25
|
+
runner = new(migrations_folder, logger: logger)
|
26
|
+
last_migrated = runner.run last_migrated: last_run_migration_version
|
27
|
+
# Update migration status file
|
28
|
+
if last_migrated
|
29
|
+
f = File.open last_migrated_file_path, 'w'
|
30
|
+
f.write last_migrated.version
|
31
|
+
f.close
|
32
|
+
end
|
33
|
+
end
|
18
34
|
end
|
19
35
|
|
20
|
-
attr_reader :files_path, :current_version, :migrations
|
36
|
+
attr_reader :files_path, :current_version, :migrations, :logger
|
21
37
|
|
22
|
-
def initialize(files_path)
|
38
|
+
def initialize(files_path, logger: nil)
|
23
39
|
@files_path = files_path
|
40
|
+
@logger = logger || ::Logger.new(STDOUT)
|
24
41
|
|
25
42
|
load_migrations
|
26
43
|
end
|
27
|
-
|
44
|
+
|
28
45
|
# @param from a valid migration name or version
|
29
46
|
# @param to a valid migration name or version
|
30
|
-
|
47
|
+
# @return last run migration
|
48
|
+
def run(from: nil, to: nil, last_migrated: nil)
|
49
|
+
if from && last_migrated
|
50
|
+
logger.warn "#{self.class}#run - Both 'from' and 'last_migrated' params given. Only 'last_migrated' considered"
|
51
|
+
end
|
31
52
|
# Extract and validate versions
|
32
|
-
from = self.class.extract_and_validate_version_from
|
33
|
-
|
53
|
+
from = self.class.extract_and_validate_version_from(from || last_migrated || 0)
|
54
|
+
# This works independently from the direction if migrations' folder contains only migrations of last installed version
|
55
|
+
to = self.class.extract_and_validate_version_from (to || @migrations.last.version)
|
34
56
|
# Select migrations to run
|
35
57
|
to_run, direction = migrations_to_run from, to
|
58
|
+
# If last_migrated param has been provided, remove some migration from the list depending on direction
|
59
|
+
if last_migrated && to_run.any?
|
60
|
+
if direction == :up && last_migrated == to_run.first.version
|
61
|
+
to_run.shift
|
62
|
+
elsif direction == :down
|
63
|
+
# We surely have at least 2 migrations in the array, otherwise the direction would've been :up
|
64
|
+
to_run.pop
|
65
|
+
end
|
66
|
+
end
|
36
67
|
to_run.each do |migration|
|
68
|
+
logger.info "Running migration #{migration.name}"
|
37
69
|
migration.send direction
|
38
70
|
end
|
71
|
+
|
72
|
+
to_run.last
|
39
73
|
end
|
40
74
|
|
41
75
|
def load_migrations
|
42
76
|
# Load files from files_path and create classes
|
43
|
-
@migrations = Dir[File.join(File.expand_path(files_path), '**', '*.rb')].
|
44
|
-
migration_name = File.basename(
|
77
|
+
@migrations = Dir[File.join(File.expand_path(files_path), '**', '*.rb')].map do |file_path|
|
78
|
+
migration_name = File.basename(file_path)
|
45
79
|
klass = Class.new(Base)
|
46
|
-
klass.class_eval(File.read(
|
80
|
+
klass.class_eval(File.read(file_path), file_path)
|
47
81
|
klass.freeze
|
48
82
|
klass.new migration_name
|
49
|
-
end
|
83
|
+
end.sort_by { |migration| migration.version }
|
50
84
|
end
|
51
85
|
|
52
86
|
# Select migrations to run depending given a starting and an ending one
|
@@ -54,15 +88,16 @@ module Fancybox2
|
|
54
88
|
selected = []
|
55
89
|
direction = from <= to ? :up : :down
|
56
90
|
@migrations.each do |m|
|
57
|
-
|
58
|
-
# Break if we already arrived to "from" migration
|
91
|
+
# downgrading - Break if we already arrived to "from" migration (e.g from=4, to=2 => 1, >2<, 3, *4*, 5)
|
59
92
|
break if (from > to) && (m.version > from)
|
60
|
-
# Break if we already arrived to "to" migration
|
93
|
+
# upgrading - Break if we already arrived to "to" migration (e.g from=2, to=4 => 1, *2*, 3, >4<, 5)
|
61
94
|
break if (from < to) && (m.version > to)
|
62
|
-
# Skip until we arrive to "to" migration
|
95
|
+
# downgrading - Skip until we arrive to "to" migration (e.g from=4, to=2 => 1, >2<, 3, *4*, 5)
|
63
96
|
next if (from > to) && (m.version < to)
|
64
|
-
# Skip until we arrive to "from" migration
|
65
|
-
next if (from
|
97
|
+
# upgrading - Skip until we arrive to "from" migration (e.g from=2, to=4 => 1, *2*, 3, >4<, 5)
|
98
|
+
next if (from <= to) && (m.version < from)
|
99
|
+
# Break if we're already out of range
|
100
|
+
break if (m.version > from && m.version > to)
|
66
101
|
|
67
102
|
if m.version <= from
|
68
103
|
selected.prepend m
|
data/lib/fancybox2/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fancybox2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Verlato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|