hot_reloader 0.4.0 → 0.5.0

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: 653d3d0ea04c00516d19cbccfbc434c065dfbfc6bc0a2da9b4b6a48c21c9a8e4
4
- data.tar.gz: cfa179f1a802f6585ed42ae2a6dc5b3a3b89ce23cdedf33444a7750e6e8311b7
3
+ metadata.gz: c2bf9cabe1a8e3383c6fcdaaa3530f249a5df5742918831bc414aa30ae9b251f
4
+ data.tar.gz: c4e1a3e0ddbc2332b16be27c4758d015e602b3cb9c4ddad70f257144c7004440
5
5
  SHA512:
6
- metadata.gz: 4a8b390f967df6eca1ed620508f2144e96dc89d52019232f108b2813dc22bb4f0af516220699af28347e95e96ec318170976d7cabf66150a9a3de033431b06b2
7
- data.tar.gz: 8408367dc5eadf1fc7278bd9ad40a10ae557c842b53f32df5faeb1726e6f80fbdc7ba73614fc9f74762238a2eceb34042be4fbbb983c6ffdd2cab48c5d6baebf
6
+ metadata.gz: c8d4d1580458d66f0a03fdeb252f57074776d6dd07dd6711f805e537ff87267f8c3b24cc289e44d62657a8308e41e56e6233f0e5e7600516684490412dd0432a
7
+ data.tar.gz: d2708191113c9441b7a8a769463ce5c6bd1967323703440d654de2e84e54625e289b2ffc920d52506b9045d16bdb7ca6171fe8f3f99aa79c9b614309b5f58975
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # HotReloader [![Build Status](https://travis-ci.org/zw963/hot_reloader.svg?branch=master)](https://travis-ci.org/zw963/hot_reloader) [![Gem Version](https://badge.fury.io/rb/hot_reloader.svg)](http://badge.fury.io/rb/hot_reloader)
1
+ # HotReloader [![Build Status](https://travis-ci.com/zw963/hot_reloader.svg?branch=master)](https://travis-ci.com/zw963/hot_reloader) [![Gem Version](https://badge.fury.io/rb/hot_reloader.svg)](http://badge.fury.io/rb/hot_reloader)
2
2
 
3
3
  A dead simple ruby code hot reloader wrap around [zeitwerk](https://github.com/fxn/zeitwerk) and [listen](https://github.com/guard/listen).
4
4
 
@@ -20,31 +20,85 @@ Add to your Gemfile
20
20
 
21
21
  Following is a example for use hot_reloader with [Roda](https://github.com/jeremyevans/roda):
22
22
 
23
+ `config.ru` which used to start rack based web server with `command rackup -o 0.0.0.0 -p 9393`
24
+
23
25
  ```rb
24
26
  # config.ru
25
27
 
26
- require 'roda'
27
- require 'hot_reloader'
28
+ require_relative './config/environment'
28
29
 
29
30
  if ENV['RACK_ENV'] == 'production'
30
- HotReloader.eager_load('path1', 'path2')
31
- run App
31
+ run App.freeze.app
32
32
  else
33
- HotReloader.will_listen('path1', 'path2'')
34
33
  run ->(env) { App.call(env) }
35
34
  end
36
35
  ```
37
36
 
37
+ Add loader initialize code into `config/environment.rb`
38
+
39
+
38
40
  ```rb
39
- # app.rb
41
+ # config/environment.rb
40
42
 
41
- class App < Roda
42
- articles = []
43
+ require 'bundler'
44
+ Bundler.require(:default, ENV.fetch('RACK_ENV', "development"))
45
+ require_relative 'application'
46
+
47
+ loader = Zeitwerk::Loader.new
48
+ loader.push_dir("#{__dir__}/../app")
49
+ loader.push_dir("#{__dir__}/../app/models")
50
+ loader.inflector.inflect "ar" => "AR"
51
+
52
+ if ENV['RACK_ENV'] == 'production'
53
+ HotReloader.eager_load(loader)
54
+ else
55
+ HotReloader.will_listen(loader)
56
+ end
57
+ ```
58
+
59
+ Or use more simple form (if you don't need setup Zeitwerk loader youself)
60
+
61
+
62
+ ```rb
63
+ # config/environment.rb
64
+
65
+ require 'bundler'
66
+ Bundler.require(:default, ENV.fetch('RACK_ENV', "development"))
67
+
68
+ paths = ["#{__dir__}/../app", "#{__dir__}/../app/models"]
43
69
 
70
+ if ENV['RACK_ENV'] == 'production'
71
+ HotReloader.eager_load(*paths)
72
+ else
73
+ HotReloader.will_listen(*paths)
74
+ end
75
+
76
+ require_relative 'application'
77
+ ```
78
+
79
+ Write whatever application initialize code which need add into application.rb
80
+
81
+ ```rb
82
+ # config/application.rb
83
+
84
+ DB = Sequel.connect(ENV.fetch("DATABASE_URL"), timeout: 10000)
85
+ ```
86
+
87
+ Add roda code into app/app.rb
88
+
89
+ ```rb
90
+ # app/app.rb
91
+
92
+ class App < Roda
93
+ articles = ['programming ruby', 'programming rust']
44
94
  route do |r|
45
95
  r.post "articles" do
46
- articles << r.params["content"]
47
- "Count: #{articles.count}"
96
+ articles << r.params["content"]
97
+ "Count: #{articles.count}"
98
+ end
99
+
100
+ r.get "articles" do
101
+ articles.join(', ')
48
102
  end
49
103
  end
50
104
  end
@@ -53,7 +107,9 @@ end
53
107
  Directory structure is like this:
54
108
 
55
109
  ```
56
- ├── app.rb
110
+ ├── app/app.rb
111
+ ├── config/environment.rb
112
+ ├── config/application.rb
57
113
  ├── config.ru
58
114
  ├── Gemfile
59
115
  └── Gemfile.lock
data/lib/hot_reloader.rb CHANGED
@@ -9,17 +9,25 @@ class HotReloader
9
9
  # Should be used for development mode only.
10
10
  #
11
11
  # @param [*String, Array<String>] folders Folders which should be monitor, can be multi-args or array.
12
+ # or only one Zeitwerk::Loader object can be provided.
12
13
  # @param [#call] logger logger or any object should response call.
13
14
  # @param [Array<String>] ignore Glob patterns or Pathname object which should be excluded.
14
15
  # @return nil
15
16
  def will_listen(*folders, logger: method(:puts), ignore: [])
16
- loader = Zeitwerk::Loader.new
17
17
  folders = folders.flatten
18
18
 
19
- raise 'you must set the root folders from which you want to load watched files.' if folders&.empty?
20
- raise 'ignore: only accept an array of glob patterns string or Pathname objects.' unless ignore.is_a? Array
19
+ if folders.first.is_a? Zeitwerk::Loader
20
+ loader = folders.first
21
+ folders = loader.root_dirs.keys
22
+ else
23
+ loader = Zeitwerk::Loader.new
24
+
25
+ raise 'you must set the root folders from which you want to load watched files.' if folders&.empty?
26
+ raise 'ignore: only accept an array of glob patterns string or Pathname objects.' unless ignore.is_a? Array
27
+
28
+ folders.each {|folder| loader.push_dir(folder) }
29
+ end
21
30
 
22
- folders.each {|folder| loader.push_dir(folder) }
23
31
  loader.enable_reloading if loader.respond_to? :enable_reloading
24
32
  loader.logger = logger
25
33
 
@@ -33,15 +41,22 @@ class HotReloader
33
41
  # More rule see https://github.com/fxn/zeitwerk
34
42
  #
35
43
  # @param [*String, Array<String>] folders folders which should be autoload, can be multi-args or array.
44
+ # or only one Zeitwerk::Loader object can be provided.
36
45
  # @param [#call] logger logger or any object should response call.
37
46
  # @return nil
38
47
  def eager_load(*folders, logger: method(:puts))
39
- loader = Zeitwerk::Loader.new
40
48
  folders = folders.flatten
41
49
 
42
- raise 'you must set the root folders from which you want to load watched files.' if folders&.empty?
50
+ if folders.first.is_a? Zeitwerk::Loader
51
+ loader = folders.first
52
+ folders = loader.root_dirs.keys
53
+ else
54
+ loader = Zeitwerk::Loader.new
55
+ raise 'you must set the root folders from which you want to load watched files.' if folders&.empty?
56
+
57
+ folders.each {|folder| loader.push_dir(folder) }
58
+ end
43
59
 
44
- folders.each {|folder| loader.push_dir(folder) }
45
60
  loader.logger = logger
46
61
 
47
62
  loader.setup
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class HotReloader
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hot_reloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Billy.Zheng(zw963)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-23 00:00:00.000000000 Z
11
+ date: 2021-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk