rb_reloader 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +18 -0
- data/Rakefile +1 -0
- data/lib/rb_reloader/version.rb +3 -0
- data/lib/rb_reloader.rb +61 -0
- data/rb_reloader.gemspec +22 -0
- data/spec/rb_reloader/folder1/folder2/folder3/test4.rb +5 -0
- data/spec/rb_reloader/folder1/folder2/test3.rb +0 -0
- data/spec/rb_reloader/folder1/test2.rb +0 -0
- data/spec/rb_reloader/rb_reloader_spec.rb +94 -0
- data/spec/rb_reloader/test1.rb +5 -0
- data/spec/spec_helper.rb +2 -0
- metadata +114 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Ruby reloader
|
2
|
+
=============
|
3
|
+
|
4
|
+
This would be helpful if you want to reload your ruby files.
|
5
|
+
|
6
|
+
How to use:
|
7
|
+
-----------
|
8
|
+
|
9
|
+
RbReloader.register(["#{current_folder}/**/t*.rb"])
|
10
|
+
|
11
|
+
That is all.
|
12
|
+
|
13
|
+
When you want to reload them:
|
14
|
+
-----------------------------
|
15
|
+
|
16
|
+
RbReloader.reload
|
17
|
+
|
18
|
+
it will auto reload the changed files. You can register the reloader somewhere in the router for example.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/rb_reloader.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require "rb_reloader/version"
|
2
|
+
|
3
|
+
module RbReloader
|
4
|
+
class FileWatcher < Array
|
5
|
+
attr_reader :file, :mtime
|
6
|
+
@map ||= {}
|
7
|
+
|
8
|
+
def self.new(file)
|
9
|
+
@map[file] ||= super
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
alias [] new
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.each(&block)
|
17
|
+
@map.values.each(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(file)
|
21
|
+
@reload, @file = true, file
|
22
|
+
@mtime = File.exist?(file) ? File.mtime(file) : Time.at(0)
|
23
|
+
super()
|
24
|
+
end
|
25
|
+
|
26
|
+
def reload?
|
27
|
+
@reload and changed?
|
28
|
+
end
|
29
|
+
|
30
|
+
def changed?
|
31
|
+
!File.exist? file or @mtime != File.mtime(file)
|
32
|
+
end
|
33
|
+
|
34
|
+
def reload
|
35
|
+
reload! if reload?
|
36
|
+
end
|
37
|
+
|
38
|
+
def reload!
|
39
|
+
$LOADED_FEATURES.delete file
|
40
|
+
clear
|
41
|
+
if File.exist? file
|
42
|
+
@mtime = File.mtime(file)
|
43
|
+
require file
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
def register(files = [])
|
50
|
+
files.flatten.each do |file|
|
51
|
+
# Rubinius and JRuby ignore block passed to glob.
|
52
|
+
Dir.glob(file).each { |f| FileWatcher[f] }
|
53
|
+
#FileWatcher[file]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def reload
|
58
|
+
FileWatcher.each {|x| x.reload}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/rb_reloader.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rb_reloader/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rb_reloader"
|
7
|
+
s.version = RbReloader::VERSION
|
8
|
+
s.authors = ["arkxu"]
|
9
|
+
s.email = ["ark.work@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Reload the ruby file timely if it has been changed}
|
12
|
+
s.description = %q{You set the path that needs monitored, and the gem will do the reload for you automatically if the file has been changed}
|
13
|
+
|
14
|
+
s.rubyforge_project = "rb_reloader"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.add_development_dependency "rspec", ">= 1.3.0"
|
21
|
+
s.add_development_dependency "ruby-debug"
|
22
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require File.expand_path("../../spec_helper", __FILE__)
|
2
|
+
|
3
|
+
require File.expand_path("../test1", __FILE__)
|
4
|
+
require File.expand_path("../folder1/folder2/folder3/test4", __FILE__)
|
5
|
+
|
6
|
+
describe RbReloader do
|
7
|
+
it "should reload test1" do
|
8
|
+
|
9
|
+
Test1.m1("t1").should == "t1"
|
10
|
+
|
11
|
+
current_folder = File.expand_path("..", __FILE__)
|
12
|
+
RbReloader.register(["#{current_folder}/**/t*.rb"])
|
13
|
+
|
14
|
+
change_test1
|
15
|
+
|
16
|
+
RbReloader.reload
|
17
|
+
|
18
|
+
# call the m2
|
19
|
+
Test1.m2("t2").should == "t2"
|
20
|
+
|
21
|
+
change_back_test1
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should reload test4" do
|
25
|
+
Test4.m4("t4").should == "t4"
|
26
|
+
|
27
|
+
current_folder = File.expand_path("..", __FILE__)
|
28
|
+
RbReloader.register(["#{current_folder}/**/t*.rb"])
|
29
|
+
|
30
|
+
change_test4
|
31
|
+
|
32
|
+
RbReloader.reload
|
33
|
+
|
34
|
+
# call the m2
|
35
|
+
Test4.m5("t5").should == "t5"
|
36
|
+
|
37
|
+
change_back_test4
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def change_test1
|
43
|
+
# change the test1.rb to add a new method t2
|
44
|
+
File.open(File.expand_path("../test1.rb", __FILE__), "r+") do |file|
|
45
|
+
counter = 0
|
46
|
+
while (line = file.gets)
|
47
|
+
counter = counter + 1
|
48
|
+
if counter == 4
|
49
|
+
file << "def self.m2(p2) \n p2 \n end \n end"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def change_test4
|
56
|
+
# change the test1.rb to add a new method t2
|
57
|
+
File.open(File.expand_path("../folder1/folder2/folder3/test4.rb", __FILE__), "r+") do |file|
|
58
|
+
counter = 0
|
59
|
+
while (line = file.gets)
|
60
|
+
counter = counter + 1
|
61
|
+
if counter == 4
|
62
|
+
file << "def self.m5(p2) \n p2 \n end \n end"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def change_back_test1
|
69
|
+
# change back the test1.rb
|
70
|
+
File.open(File.expand_path("../test1.rb", __FILE__), "w") do |file|
|
71
|
+
file << <<-CONTENT
|
72
|
+
module Test1
|
73
|
+
def self.m1(p1)
|
74
|
+
p1
|
75
|
+
end
|
76
|
+
end
|
77
|
+
CONTENT
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
def change_back_test4
|
83
|
+
# change back the test1.rb
|
84
|
+
File.open(File.expand_path("../folder1/folder2/folder3/test4.rb", __FILE__), "w") do |file|
|
85
|
+
file << <<-CONTENT
|
86
|
+
module Test4
|
87
|
+
def self.m4(p)
|
88
|
+
p
|
89
|
+
end
|
90
|
+
end
|
91
|
+
CONTENT
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rb_reloader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- arkxu
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-03 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 1.3.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: ruby-debug
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: You set the path that needs monitored, and the gem will do the reload for you automatically if the file has been changed
|
52
|
+
email:
|
53
|
+
- ark.work@gmail.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- lib/rb_reloader.rb
|
66
|
+
- lib/rb_reloader/version.rb
|
67
|
+
- rb_reloader.gemspec
|
68
|
+
- spec/rb_reloader/folder1/folder2/folder3/test4.rb
|
69
|
+
- spec/rb_reloader/folder1/folder2/test3.rb
|
70
|
+
- spec/rb_reloader/folder1/test2.rb
|
71
|
+
- spec/rb_reloader/rb_reloader_spec.rb
|
72
|
+
- spec/rb_reloader/test1.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
has_rdoc: true
|
75
|
+
homepage: ""
|
76
|
+
licenses: []
|
77
|
+
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project: rb_reloader
|
104
|
+
rubygems_version: 1.6.2
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Reload the ruby file timely if it has been changed
|
108
|
+
test_files:
|
109
|
+
- spec/rb_reloader/folder1/folder2/folder3/test4.rb
|
110
|
+
- spec/rb_reloader/folder1/folder2/test3.rb
|
111
|
+
- spec/rb_reloader/folder1/test2.rb
|
112
|
+
- spec/rb_reloader/rb_reloader_spec.rb
|
113
|
+
- spec/rb_reloader/test1.rb
|
114
|
+
- spec/spec_helper.rb
|