fancybox2 0.0.6 → 0.0.7

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: 88d9c51fb40eb09f50db57b7dd601e78f36dc27df736d3b801c331cf9b6ba62c
4
- data.tar.gz: 7cb67a8d2d7894591dca8815f9aee65a809498405ad65fd2fc05e05d3af192f1
3
+ metadata.gz: 771569623e40be10df4f7903ee6bde6b9523421e559f1ed05418932ba34da21e
4
+ data.tar.gz: 15bfb48338dd6785ecabdd63dd0b714eefee51d7a18d2add44b0a43cb7d27acb
5
5
  SHA512:
6
- metadata.gz: 253c8254774761d441e39ef541038a86e6f4860db9fe5d7584565e7aa1641110247231a59cad0ae2304e5f9fe462a5f09fa2c62f8fb9ebe6aa0cc2a38834cf9f
7
- data.tar.gz: ce444bf2a0f6eb8ce1148a17013a0419a50029c193b0a7f89423f768120fcddcd0f25acf2356154d4f75b9c277e265a1499a99fec3588124c1f3f086f3f34d60
6
+ metadata.gz: 44ecd19a2239f7687496040045110b2274898c57f33721b63321877e1fffc7e2f2419abc56be3072126bd89e7b353068a5c15a0d10dddf5a85289746adcdb0c0
7
+ data.tar.gz: eb42ba62b805b3e43fe51784d900021b81d779569365d5a617d07639635211013f23fd3c3538c81ac04a1eb7d2719d924fe06d29287728569c1332159287a709
@@ -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,78 @@
1
+ module Fancybox2
2
+ module Migrations
3
+ class Runner
4
+ include Exceptions
5
+
6
+ VERSION_REGEXP = /^(\d{14})/.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 version must be a 14 digits integer'
14
+ end
15
+
16
+ version.to_i
17
+ end
18
+ end
19
+
20
+ attr_reader :files_path, :current_version, :migrations
21
+
22
+ def initialize(files_path)
23
+ @files_path = files_path
24
+
25
+ load_migrations
26
+ end
27
+
28
+ # @param from a valid migration name or version
29
+ # @param to a valid migration name or version
30
+ def run(from: nil, to: nil)
31
+ # Extract and validate versions
32
+ from = self.class.extract_and_validate_version_from from
33
+ to = self.class.extract_and_validate_version_from to
34
+ # Select migrations to run
35
+ to_run, direction = migrations_to_run from, to
36
+ to_run.each do |migration|
37
+ migration.send direction
38
+ end
39
+ end
40
+
41
+ def load_migrations
42
+ # Load files from files_path and create classes
43
+ @migrations = Dir[File.join(File.expand_path(files_path), '**', '*.rb')].sort.map do |f_path|
44
+ migration_name = File.basename(f_path)
45
+ klass = Class.new(Base)
46
+ klass.class_eval(File.read(f_path), f_path)
47
+ klass.freeze
48
+ klass.new migration_name
49
+ end
50
+ end
51
+
52
+ # Select migrations to run depending given a starting and an ending one
53
+ def migrations_to_run(from, to)
54
+ selected = []
55
+ direction = from <= to ? :up : :down
56
+ @migrations.each do |m|
57
+ break if (from == to)
58
+ # Break if we already arrived to "from" migration
59
+ break if (from > to) && (m.version > from)
60
+ # Break if we already arrived to "to" migration
61
+ break if (from < to) && (m.version > to)
62
+ # Skip until we arrive to "to" migration
63
+ next if (from > to) && (m.version < to)
64
+ # Skip until we arrive to "from" migration
65
+ next if (from < to) && (m.version < from)
66
+
67
+ if m.version <= from
68
+ selected.prepend m
69
+ else
70
+ selected.append m
71
+ end
72
+ end
73
+
74
+ [selected, direction]
75
+ end
76
+ end
77
+ end
78
+ 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.7'
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.7
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-01-25 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