sinatra-reloader 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +27 -0
- data/lib/sinatra/reloader.rb +24 -14
- data/spec/sinatra/reloader_spec.rb +30 -3
- data/spec/spec_helper.rb +1 -1
- metadata +26 -13
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
copyright (c) 2010 Konstantin Haase. All rights reserved.
|
2
|
+
|
3
|
+
Developed by: Konstantin Haase
|
4
|
+
http://github.com/rkh/big_band
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to
|
8
|
+
deal with the Software without restriction, including without limitation the
|
9
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
1. Redistributions of source code must retain the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimers.
|
14
|
+
2. Redistributions in binary form must reproduce the above copyright
|
15
|
+
notice, this list of conditions and the following disclaimers in the
|
16
|
+
documentation and/or other materials provided with the distribution.
|
17
|
+
3. Neither the name of Konstantin Haase, nor the names of other contributors
|
18
|
+
may be used to endorse or promote products derived from this Software without
|
19
|
+
specific prior written permission.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24
|
+
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
27
|
+
WITH THE SOFTWARE.
|
data/lib/sinatra/reloader.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "monkey"
|
1
|
+
require "monkey-lib"
|
2
2
|
require "sinatra/base"
|
3
3
|
require "sinatra/sugar"
|
4
4
|
require "sinatra/advanced_routes"
|
@@ -7,19 +7,16 @@ module Sinatra
|
|
7
7
|
Base.ignore_caller
|
8
8
|
|
9
9
|
module Reloader
|
10
|
-
|
11
10
|
class FileWatcher < Array
|
12
|
-
|
13
|
-
attr_reader :file, :mtime
|
14
|
-
|
11
|
+
attr_reader :file, :mtime, :inline_templates, :app
|
15
12
|
extend Enumerable
|
16
13
|
@map ||= {}
|
17
14
|
|
18
15
|
def self.register(route)
|
19
|
-
new(route.file) << route if route.file?
|
16
|
+
new(route.file, route.app) << route if route.file?
|
20
17
|
end
|
21
18
|
|
22
|
-
def self.new(file)
|
19
|
+
def self.new(file, app)
|
23
20
|
file = file.expand_path
|
24
21
|
begin
|
25
22
|
file = file.realpath
|
@@ -36,8 +33,8 @@ module Sinatra
|
|
36
33
|
@map.values.each(&block)
|
37
34
|
end
|
38
35
|
|
39
|
-
def initialize(file)
|
40
|
-
@reload, @file = true, file
|
36
|
+
def initialize(file, app)
|
37
|
+
@reload, @file, @app = true, file, app
|
41
38
|
@mtime = File.exist?(file) ? File.mtime(file) : Time.at(0)
|
42
39
|
super()
|
43
40
|
end
|
@@ -58,16 +55,24 @@ module Sinatra
|
|
58
55
|
reload! if reload?
|
59
56
|
end
|
60
57
|
|
58
|
+
def inline_templates?
|
59
|
+
!!inline_templates
|
60
|
+
end
|
61
|
+
|
62
|
+
def inline_templates!
|
63
|
+
@inline_templates = true
|
64
|
+
end
|
65
|
+
|
61
66
|
def reload!
|
62
67
|
each { |route| route.deactivate }
|
63
68
|
$LOADED_FEATURES.delete file
|
64
69
|
clear
|
65
70
|
if File.exist? file
|
71
|
+
app.set :inline_templates, file if inline_templates?
|
66
72
|
@mtime = File.mtime(file)
|
67
73
|
require file
|
68
74
|
end
|
69
75
|
end
|
70
|
-
|
71
76
|
end
|
72
77
|
|
73
78
|
module ClassMethods
|
@@ -77,20 +82,27 @@ module Sinatra
|
|
77
82
|
end
|
78
83
|
files.flatten.each do |file|
|
79
84
|
# Rubinius and JRuby ignore block passed to glob.
|
80
|
-
Dir.glob(file).each { |f| FileWatcher[f].dont_reload! dont }
|
81
|
-
FileWatcher[file].dont_reload! dont
|
85
|
+
Dir.glob(file).each { |f| FileWatcher[f, self].dont_reload! dont }
|
86
|
+
FileWatcher[file, self].dont_reload! dont
|
82
87
|
end
|
83
88
|
end
|
84
89
|
|
85
90
|
def also_reload(*files)
|
86
91
|
dont_reload(files, false)
|
87
92
|
end
|
93
|
+
|
94
|
+
def inline_templates=(file = nil)
|
95
|
+
file = (file.nil? || file == true) ? caller_files.first : (file || $0)
|
96
|
+
FileWatcher[file, self].inline_templates!
|
97
|
+
super
|
98
|
+
end
|
88
99
|
end
|
89
100
|
|
90
101
|
def self.registered(klass)
|
91
102
|
klass.register AdvancedRoutes
|
92
103
|
klass.extend ClassMethods
|
93
104
|
klass.each_route { |route| advanced_route_added(route) }
|
105
|
+
klass.enable :reload_templates
|
94
106
|
klass.before { Reloader.reload_routes }
|
95
107
|
end
|
96
108
|
|
@@ -106,9 +118,7 @@ module Sinatra
|
|
106
118
|
return Thread.exclusive { reload_routes(false) } if thread_safe and thread_safe?
|
107
119
|
FileWatcher.each { |file| file.reload }
|
108
120
|
end
|
109
|
-
|
110
121
|
end
|
111
122
|
|
112
123
|
register Reloader
|
113
|
-
|
114
124
|
end
|
@@ -1,15 +1,20 @@
|
|
1
1
|
require File.expand_path("../../spec_helper", __FILE__)
|
2
2
|
|
3
3
|
describe Sinatra::Reloader do
|
4
|
+
it_should_behave_like 'sinatra'
|
4
5
|
|
5
|
-
def app_file(file, content
|
6
|
+
def app_file(file, content)
|
6
7
|
file = File.expand_path(file, @temp_dir)
|
7
8
|
old_mtime = File.exist?(file) ? File.mtime(file) : Time.at(0)
|
8
9
|
new_mtime = old_mtime
|
9
10
|
# this is eventually faster than a hard coded sleep 1, also it adjusts to the systems
|
10
11
|
# mtime granularity
|
11
12
|
until new_mtime > old_mtime
|
12
|
-
File.open(file, "w")
|
13
|
+
File.open(file, "w") do |f|
|
14
|
+
f << "class ::ExampleApp < Sinatra::Base; #{content}; end\n"
|
15
|
+
yield f if block_given?
|
16
|
+
end
|
17
|
+
File.utime File.atime(file), old_mtime + 10, file unless ENV['MTIME']
|
13
18
|
new_mtime = File.mtime file
|
14
19
|
# ok, let's not generate too much io
|
15
20
|
sleep 0.1 if new_mtime == old_mtime
|
@@ -59,4 +64,26 @@ describe Sinatra::Reloader do
|
|
59
64
|
browse_route(:get, '/baz').body.should == 'foo'
|
60
65
|
end
|
61
66
|
|
62
|
-
|
67
|
+
it "reloads inline templates" do
|
68
|
+
file = app_file("example_app4.rb", "get('/foo') { haml :foo }") { |f| f.puts "__END__", "@@foo", "foo" }
|
69
|
+
app.set :inline_templates, file
|
70
|
+
browse_route(:get, '/foo').body.strip.should == "foo"
|
71
|
+
app_file("example_app4.rb", "get('/foo') { haml :foo }") { |f| f.puts "__END__", "@@foo", "bar" }
|
72
|
+
browse_route(:get, '/foo').body.strip.should == "bar"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should not readd middleware" do
|
76
|
+
3.times do
|
77
|
+
app_file "example_app5.rb", <<-RUBY
|
78
|
+
use Rack::Config do |env|
|
79
|
+
env['some.counter'] ||= 0
|
80
|
+
env['some.counter'] += 1
|
81
|
+
$counter = env['some.counter']
|
82
|
+
end
|
83
|
+
RUBY
|
84
|
+
browse_route :get, '/foo'
|
85
|
+
end
|
86
|
+
$counter.should == 1
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-reloader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 11
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
8
|
+
- 5
|
9
|
+
- 0
|
10
|
+
version: 0.5.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Konstantin Haase
|
@@ -14,58 +15,65 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-07-09 00:00:00 +02:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: sinatra-advanced-routes
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ~>
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 11
|
27
30
|
segments:
|
28
31
|
- 0
|
29
|
-
-
|
32
|
+
- 5
|
30
33
|
- 0
|
31
|
-
version: 0.
|
34
|
+
version: 0.5.0
|
32
35
|
type: :runtime
|
33
36
|
version_requirements: *id001
|
34
37
|
- !ruby/object:Gem::Dependency
|
35
38
|
name: sinatra-test-helper
|
36
39
|
prerelease: false
|
37
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
38
42
|
requirements:
|
39
43
|
- - ~>
|
40
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 11
|
41
46
|
segments:
|
42
47
|
- 0
|
43
|
-
-
|
48
|
+
- 5
|
44
49
|
- 0
|
45
|
-
version: 0.
|
50
|
+
version: 0.5.0
|
46
51
|
type: :development
|
47
52
|
version_requirements: *id002
|
48
53
|
- !ruby/object:Gem::Dependency
|
49
54
|
name: sinatra
|
50
55
|
prerelease: false
|
51
56
|
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
52
58
|
requirements:
|
53
|
-
- -
|
59
|
+
- - ~>
|
54
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 15
|
55
62
|
segments:
|
63
|
+
- 1
|
56
64
|
- 0
|
57
|
-
|
58
|
-
- 4
|
59
|
-
version: 0.9.4
|
65
|
+
version: "1.0"
|
60
66
|
type: :runtime
|
61
67
|
version_requirements: *id003
|
62
68
|
- !ruby/object:Gem::Dependency
|
63
69
|
name: rspec
|
64
70
|
prerelease: false
|
65
71
|
requirement: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
66
73
|
requirements:
|
67
74
|
- - ">="
|
68
75
|
- !ruby/object:Gem::Version
|
76
|
+
hash: 27
|
69
77
|
segments:
|
70
78
|
- 1
|
71
79
|
- 3
|
@@ -86,6 +94,7 @@ files:
|
|
86
94
|
- spec/sinatra/reloader_spec.rb
|
87
95
|
- spec/spec_helper.rb
|
88
96
|
- README.md
|
97
|
+
- LICENSE
|
89
98
|
has_rdoc: yard
|
90
99
|
homepage: http://github.com/rkh/sinatra-reloader
|
91
100
|
licenses: []
|
@@ -96,23 +105,27 @@ rdoc_options: []
|
|
96
105
|
require_paths:
|
97
106
|
- lib
|
98
107
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
99
109
|
requirements:
|
100
110
|
- - ">="
|
101
111
|
- !ruby/object:Gem::Version
|
112
|
+
hash: 3
|
102
113
|
segments:
|
103
114
|
- 0
|
104
115
|
version: "0"
|
105
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
106
118
|
requirements:
|
107
119
|
- - ">="
|
108
120
|
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
109
122
|
segments:
|
110
123
|
- 0
|
111
124
|
version: "0"
|
112
125
|
requirements: []
|
113
126
|
|
114
127
|
rubyforge_project:
|
115
|
-
rubygems_version: 1.3.
|
128
|
+
rubygems_version: 1.3.7
|
116
129
|
signing_key:
|
117
130
|
specification_version: 3
|
118
131
|
summary: Smart and fast code reloader for Sinatra (part of BigBand).
|