fancybox2 0.0.5 → 0.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2ebfac4305bcbb63e0c539f2bcf665b3d76103dda23ce778af75b9b4dcab683
4
- data.tar.gz: c23595776e88b5d3cb58853dca0e394485c0f556cbfded59bc528af3c8d81154
3
+ metadata.gz: 3656436f12dfd2d4129e12b7db0ffd45a116698030802589a729ee82bc829948
4
+ data.tar.gz: 75f33ab435919e63a9696ef84a731084060bf3cc654a0a80d427b6c96c38f89a
5
5
  SHA512:
6
- metadata.gz: 4333bdd2ed269d3f35066558984024baed4b71f776c5c1b3c8813ee99af8cafeb397203c48fe166d0d3890ca5e90a339ed2e2357a491550aad55815b560f42cc
7
- data.tar.gz: 2df663ca74c34c912d159c2c0b127883d94e3754a236e6b01a3fd0bb2321dfc8ec52365e419fa21598fba476347ad3b1f4d0d237eee34dff8d37c3f369660c9c
6
+ metadata.gz: fdbd2c3d7e2e69882a53d830c49bf7ee8341c5091bbd43ed55fda77776da36326c16c0c5093a5ed62ef8632357fa0c14e8cc951846352ca9f001fec72b88bad8
7
+ data.tar.gz: 01716640347e04c37b72cdf7fc9b8055c2685beb59381f7869f6b08fc86614eb8a747dfd1611e37e1aa6f6f61424f22da5e1d1363a63d255671d73860132078e
@@ -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,14 +87,10 @@ module Fancybox2
87
87
  @on_configs = block
88
88
  return
89
89
  end
90
- begin
91
- # Try to parse
92
- cfg = JSON.parse packet.payload
93
- if cfg && cfg['configs']
94
- self.configs.merge! cfg['configs']
95
- end
96
- rescue JSON::ParserError
97
- 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']
98
94
  end
99
95
 
100
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.5'
4
+ VERSION = '0.0.10'
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.5
4
+ version: 0.0.10
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-02-11 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