fancybox2 0.0.4 → 0.0.9

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: 305d4f2b4006be7dde72fdfd9374f792338429defd2a9269e0ad8d3cfb1634ce
4
- data.tar.gz: ebedeaabc2cb173c67907f08c8c3b8f7fa0b276c1d516fb56910b7fd1102bf6d
3
+ metadata.gz: 6821e581df48497ebc9870338ab258f9f4c8f4f4301067edef7936f2957452b0
4
+ data.tar.gz: 5949d3ee2689bf3dd8c8e4e6256061a8d5c20cafb017cde96856df04d0df217d
5
5
  SHA512:
6
- metadata.gz: e313af43974ac8ba6e2c0330381ac532f0264f6edcc0ddfa8695bebe726c1d4c35f1ddad561c9d8c07e024b4392e85e46dd9ecc10847c4dd43cce46260d4698c
7
- data.tar.gz: 141e2e29514e61b282893ca7e2a2ac64308addd364d9bff408d9b6828b7bbd5f9c222b8d206af6b61243f952e710f844c1ac55d546e2712e9de2b9dd6e1c8b22
6
+ metadata.gz: c98c23a20e13629af2723b789d044e03180e8e07a407f3c842ccd3d314e9b07d8c937a16a5f868c6c49435f999b7bed7956a464f1711092a0f738ba8ab6125d4
7
+ data.tar.gz: 14425e0627a2a78f830c7d0dbbd0465d6e009bc308024fa4779ed5c1ea928353574e12c839bb01f9b41e1b8b84eb8e36dddaf25e608f30cac9d63c075af34561
@@ -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,94 @@
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
+ end
19
+
20
+ attr_reader :files_path, :current_version, :migrations, :logger
21
+
22
+ def initialize(files_path, logger: nil)
23
+ @files_path = files_path
24
+ @logger = logger || ::Logger.new(STDOUT)
25
+
26
+ load_migrations
27
+ end
28
+
29
+ # @param from a valid migration name or version
30
+ # @param to a valid migration name or version
31
+ # @return last run migration
32
+ def run(from: nil, to: nil, last_migrated: nil)
33
+ if from && last_migrated
34
+ logger.warn "#{self.class}#run - Both 'from' and 'last_migrated' params given. Only 'last_migrated' considered"
35
+ end
36
+ # Extract and validate versions
37
+ from = self.class.extract_and_validate_version_from(from || last_migrated || 0)
38
+ # This works independently from the direction if migrations' folder contains only migrations of last installed version
39
+ to = self.class.extract_and_validate_version_from (to || @migrations.last.version)
40
+ puts "from: #{from}, to: #{to}, last_migrated: #{last_migrated}"
41
+ # Select migrations to run
42
+ to_run, direction = migrations_to_run from, to
43
+ # If last_migrated has been specified and direction is :up, remove first migration
44
+ if last_migrated && direction == :up
45
+ to_run.shift
46
+ end
47
+ to_run.each do |migration|
48
+ logger.info "Running migration #{migration.name}"
49
+ migration.send direction
50
+ end
51
+
52
+ to_run.last
53
+ end
54
+
55
+ def load_migrations
56
+ # Load files from files_path and create classes
57
+ @migrations = Dir[File.join(File.expand_path(files_path), '**', '*.rb')].map do |file_path|
58
+ migration_name = File.basename(file_path)
59
+ klass = Class.new(Base)
60
+ klass.class_eval(File.read(file_path), file_path)
61
+ klass.freeze
62
+ klass.new migration_name
63
+ end.sort_by { |migration| migration.version }
64
+ end
65
+
66
+ # Select migrations to run depending given a starting and an ending one
67
+ def migrations_to_run(from, to)
68
+ selected = []
69
+ direction = from <= to ? :up : :down
70
+ @migrations.each do |m|
71
+ puts m.version
72
+ # Edge case, no migrations to run
73
+ break if from == to
74
+ # downgrading - Break if we already arrived to "from" migration (e.g from=4, to=2 => 1, >2<, 3, *4*, 5)
75
+ break if (from > to) && (m.version > from)
76
+ # upgrading - Break if we already arrived to "to" migration (e.g from=2, to=4 => 1, *2*, 3, >4<, 5)
77
+ break if (from < to) && (m.version > to)
78
+ # downgrading - Skip until we arrive to "to" migration (e.g from=4, to=2 => 1, >2<, 3, *4*, 5)
79
+ next if (from > to) && (m.version < to)
80
+ # upgrading - Skip until we arrive to "from" migration (e.g from=2, to=4 => 1, *2*, 3, >4<, 5)
81
+ next if (from < to) && (m.version < from)
82
+
83
+ if m.version <= from
84
+ selected.prepend m
85
+ else
86
+ selected.append m
87
+ end
88
+ end
89
+
90
+ [selected, direction]
91
+ end
92
+ end
93
+ end
94
+ end
@@ -30,6 +30,7 @@ module Fancybox2
30
30
  @logger = options.fetch :logger, create_default_logger
31
31
  @status = :stopped
32
32
  @alive_task = nil
33
+ @configs = {}
33
34
  end
34
35
 
35
36
  def alive_message_data(&block)
@@ -86,20 +87,12 @@ module Fancybox2
86
87
  @on_configs = block
87
88
  return
88
89
  end
89
- @configs = begin
90
- # Try to parse
91
- JSON.parse packet.payload
92
- rescue JSON::ParserError
93
- logger.debug 'on_configs: failed parsing packet as JSON, retrying with YAML'
94
- begin
95
- # Try to parse YAML
96
- YAML.load packet.payload
97
- rescue StandardError
98
- logger.debug 'on_configs: failed parsing packet as YAML. Falling back to raw payload'
99
- # Fallback to original content
100
- packet.payload
101
- end
102
- end
90
+
91
+ cfg = packet.payload
92
+ if cfg && cfg.is_a?(Hash) && cfg['configs']
93
+ self.configs.merge! cfg['configs']
94
+ end
95
+
103
96
  @on_configs.call(packet) if @on_configs
104
97
  end
105
98
 
@@ -193,8 +186,8 @@ module Fancybox2
193
186
  # Call user code
194
187
  @on_start.call(packet) if @on_start
195
188
 
196
- configs = packet ? packet.payload : {}
197
- interval = configs['aliveTimeout'] || 1000
189
+ cfg = packet ? packet.payload : {}
190
+ interval = cfg['aliveTimeout'] || 1000
198
191
  # Start code execution from scratch
199
192
  logger.debug "Received 'start'"
200
193
  @status = :running
@@ -4,34 +4,34 @@ module Fancybox2
4
4
  extend self
5
5
 
6
6
  def identifier
7
- return @indentifier if @indentifier
7
+ return @identifier if @identifier
8
8
 
9
9
  host_os = RbConfig::CONFIG['host_os']
10
- case host_os
11
- when /aix(.+)$/
12
- 'aix'
13
- when /darwin(.+)$/
14
- 'darwin'
15
- when /linux/
16
- 'linux'
17
- when /freebsd(.+)$/
18
- 'freebsd'
19
- when /openbsd(.+)$/
20
- 'openbsd'
21
- when /netbsd(.*)$/
22
- 'netbsd'
23
- when /dragonfly(.*)$/
24
- 'dragonflybsd'
25
- when /solaris2/
26
- 'solaris2'
27
- when /mswin|mingw32|windows/
28
- # No Windows platform exists that was not based on the Windows_NT kernel,
29
- # so 'windows' refers to all platforms built upon the Windows_NT kernel and
30
- # have access to win32 or win64 subsystems.
31
- 'windows'
32
- else
33
- host_os
34
- end
10
+ @identifier = case host_os
11
+ when /aix(.+)$/
12
+ 'aix'
13
+ when /darwin(.+)$/
14
+ 'darwin'
15
+ when /linux/
16
+ 'linux'
17
+ when /freebsd(.+)$/
18
+ 'freebsd'
19
+ when /openbsd(.+)$/
20
+ 'openbsd'
21
+ when /netbsd(.*)$/
22
+ 'netbsd'
23
+ when /dragonfly(.*)$/
24
+ 'dragonflybsd'
25
+ when /solaris2/
26
+ 'solaris2'
27
+ when /mswin|mingw32|windows/
28
+ # No Windows platform exists that was not based on the Windows_NT kernel,
29
+ # so 'windows' refers to all platforms built upon the Windows_NT kernel and
30
+ # have access to win32 or win64 subsystems.
31
+ 'windows'
32
+ else
33
+ host_os
34
+ end
35
35
  end
36
36
  end
37
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fancybox2
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.9'
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.4
4
+ version: 0.0.9
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-10-26 00:00:00.000000000 Z
11
+ date: 2021-02-10 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