fancybox2 0.0.6 → 0.0.11

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: 88d9c51fb40eb09f50db57b7dd601e78f36dc27df736d3b801c331cf9b6ba62c
4
- data.tar.gz: 7cb67a8d2d7894591dca8815f9aee65a809498405ad65fd2fc05e05d3af192f1
3
+ metadata.gz: dc475914503d3c657627de5c2521c7eaa8e68e428c88f37ecda63632715f2333
4
+ data.tar.gz: e3bafe05ad157a0387aa481dcf891aaa0ba6d2d516c0ccfd59aa393d489893a2
5
5
  SHA512:
6
- metadata.gz: 253c8254774761d441e39ef541038a86e6f4860db9fe5d7584565e7aa1641110247231a59cad0ae2304e5f9fe462a5f09fa2c62f8fb9ebe6aa0cc2a38834cf9f
7
- data.tar.gz: ce444bf2a0f6eb8ce1148a17013a0419a50029c193b0a7f89423f768120fcddcd0f25acf2356154d4f75b9c277e265a1499a99fec3588124c1f3f086f3f34d60
6
+ metadata.gz: 7405825f913d995cf332bc33c954e9eb432ba3109ec1e69518e4550f6113ebfbdc4c5b9788202169f9bd70a6ecbbb94b14214bad3e85c1422a59b180a8fb58c2
7
+ data.tar.gz: c50bd9c0898cebba2cdb841cac4a195e02ebc7290519041112c9f89c605c7e9cf7cd6046c6f652033debe0e2e140b9f01ea58fb9e7355511d7d097735873282e
@@ -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' }
@@ -0,0 +1,24 @@
1
+ module Fancybox2
2
+ module Migrations
3
+ class Base
4
+
5
+ class << self
6
+ def self.descendants
7
+ ObjectSpace.each_object(Class).select { |klass| klass < self }
8
+ end
9
+ end
10
+
11
+ attr_reader :name, :version
12
+
13
+ def initialize(name)
14
+ @name = name
15
+
16
+ @version = Runner.extract_and_validate_version_from name
17
+ end
18
+
19
+ def call(up_or_down = :up)
20
+ send up_or_down
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Fancybox2
2
+ module Migrations
3
+ module Exceptions
4
+
5
+ class FileNameError < StandardError
6
+ def initialize(message = nil)
7
+ message = message || 'One of the provided migrations file has an invalid name'
8
+ super(message)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,113 @@
1
+ module Fancybox2
2
+ module Migrations
3
+ class Runner
4
+ include Exceptions
5
+
6
+ VERSION_REGEXP = /^(\d+)/.freeze
7
+
8
+ class << self
9
+
10
+ def extract_and_validate_version_from(migration_name)
11
+ version = migration_name.to_s.scan(VERSION_REGEXP).flatten.first
12
+ unless version
13
+ raise ArgumentError, 'migration name must start with a positive integer number e.g: 1_do_something.rb'
14
+ end
15
+
16
+ version.to_i
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
34
+ end
35
+
36
+ attr_reader :files_path, :current_version, :migrations, :logger
37
+
38
+ def initialize(files_path, logger: nil)
39
+ @files_path = files_path
40
+ @logger = logger || ::Logger.new(STDOUT)
41
+
42
+ load_migrations
43
+ end
44
+
45
+ # @param from a valid migration name or version
46
+ # @param to a valid migration name or version
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
52
+ # Extract and validate versions
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)
56
+ # Select migrations to run
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
67
+ to_run.each do |migration|
68
+ logger.info "Running migration #{migration.name}"
69
+ migration.send direction
70
+ end
71
+
72
+ to_run.last
73
+ end
74
+
75
+ def load_migrations
76
+ # Load files from files_path and create classes
77
+ @migrations = Dir[File.join(File.expand_path(files_path), '**', '*.rb')].map do |file_path|
78
+ migration_name = File.basename(file_path)
79
+ klass = Class.new(Base)
80
+ klass.class_eval(File.read(file_path), file_path)
81
+ klass.freeze
82
+ klass.new migration_name
83
+ end.sort_by { |migration| migration.version }
84
+ end
85
+
86
+ # Select migrations to run depending given a starting and an ending one
87
+ def migrations_to_run(from, to)
88
+ selected = []
89
+ direction = from <= to ? :up : :down
90
+ @migrations.each do |m|
91
+ # downgrading - Break if we already arrived to "from" migration (e.g from=4, to=2 => 1, >2<, 3, *4*, 5)
92
+ break if (from > to) && (m.version > from)
93
+ # upgrading - Break if we already arrived to "to" migration (e.g from=2, to=4 => 1, *2*, 3, >4<, 5)
94
+ break if (from < to) && (m.version > to)
95
+ # downgrading - Skip until we arrive to "to" migration (e.g from=4, to=2 => 1, >2<, 3, *4*, 5)
96
+ next if (from > to) && (m.version < to)
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)
101
+
102
+ if m.version <= from
103
+ selected.prepend m
104
+ else
105
+ selected.append m
106
+ end
107
+ end
108
+
109
+ [selected, direction]
110
+ end
111
+ end
112
+ end
113
+ end
@@ -87,13 +87,10 @@ module Fancybox2
87
87
  @on_configs = block
88
88
  return
89
89
  end
90
- begin
91
- cfg = packet.payload
92
- if cfg && cfg.is_a?(Hash) && cfg['configs']
93
- self.configs.merge! cfg['configs']
94
- end
95
- rescue JSON::ParserError
96
- logger.debug 'on_configs: failed parsing packet as JSON'
90
+
91
+ cfg = packet.payload
92
+ if cfg && cfg.is_a?(Hash) && cfg['configs']
93
+ self.configs.merge! cfg['configs']
97
94
  end
98
95
 
99
96
  @on_configs.call(packet) if @on_configs
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fancybox2
4
- VERSION = '0.0.6'
4
+ VERSION = '0.0.11'
5
5
  end
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.6
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Verlato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-01 00:00:00.000000000 Z
11
+ date: 2021-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -66,6 +66,9 @@ files:
66
66
  - lib/fancybox2/logger/json_formatter.rb
67
67
  - lib/fancybox2/logger/mqtt_log_device.rb
68
68
  - lib/fancybox2/logger/multi.rb
69
+ - lib/fancybox2/migrations/base.rb
70
+ - lib/fancybox2/migrations/exceptions.rb
71
+ - lib/fancybox2/migrations/runner.rb
69
72
  - lib/fancybox2/module/base.rb
70
73
  - lib/fancybox2/module/config.rb
71
74
  - lib/fancybox2/module/exceptions.rb