sinarey 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sinarey/router.rb +50 -11
- data/lib/sinarey/version.rb +1 -1
- data/lib/sinatra/sinarey_reloader.rb +254 -0
- data/sinarey.gemspec +2 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bee69a3761ccb406bdee2cedbd1cb81d851ceaa
|
4
|
+
data.tar.gz: f373640196f44cda768dad4b88d29ae9a02a5a81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17f7ab80712fa2253221f0e5c864ec9ca3fcb75105497db34d75ee21fece68adb8105d15c834e22e17b12b99a3d1717e165bc2dfba509a15cddfb6f2d1d4ab8f
|
7
|
+
data.tar.gz: e8547832624b5abcc05935cf6a8b9ba32588b5397ab063f16a6fe01cfeff1fe008ceb87440f088a99e66b5a13eae8fce76c0de2c0f34215db31278f391b3f044
|
data/lib/sinarey/router.rb
CHANGED
@@ -58,23 +58,62 @@ module Sinarey
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
61
|
+
case ENV["RACK_ENV"]
|
62
|
+
when 'development'
|
63
|
+
|
64
|
+
#development need support sinarey reloader.so here use dev logic.
|
65
|
+
def apps_route(verb, path, env)
|
66
|
+
|
67
|
+
#auto reload modified code
|
68
|
+
@apps.each do |index,app|
|
69
|
+
app.auto_reload if app.respond_to?(:auto_reload)
|
70
|
+
end
|
71
|
+
|
72
|
+
#rebuild route table
|
73
|
+
@turbo_routes = {}
|
74
|
+
@routes = {}
|
75
|
+
build_routing_table
|
76
|
+
|
77
|
+
if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
|
78
|
+
turbo_route.tap do |block_id,app_id|
|
79
|
+
env['sinarey.router'] = {type: :turbo, block_id: block_id}
|
80
|
+
status, headers, response = @apps[app_id].call(env)
|
81
|
+
return status, headers, response
|
82
|
+
end
|
83
|
+
elsif routes = @routes[verb]
|
84
|
+
routes.each do |pattern, keys, conditions, block_id, app_id|
|
85
|
+
if match = pattern.match(path)
|
86
|
+
env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
|
87
|
+
status, headers, response = @apps[app_id].call(env)
|
88
|
+
return status, headers, response
|
89
|
+
end
|
90
|
+
end
|
67
91
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
92
|
+
nil
|
93
|
+
end
|
94
|
+
|
95
|
+
else
|
96
|
+
|
97
|
+
def apps_route(verb, path, env)
|
98
|
+
if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
|
99
|
+
turbo_route.tap do |block_id,app_id|
|
100
|
+
env['sinarey.router'] = {type: :turbo, block_id: block_id}
|
72
101
|
status, headers, response = @apps[app_id].call(env)
|
73
102
|
return status, headers, response
|
74
103
|
end
|
104
|
+
elsif routes = @routes[verb]
|
105
|
+
routes.each do |pattern, keys, conditions, block_id, app_id|
|
106
|
+
if match = pattern.match(path)
|
107
|
+
env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
|
108
|
+
status, headers, response = @apps[app_id].call(env)
|
109
|
+
return status, headers, response
|
110
|
+
end
|
111
|
+
end
|
75
112
|
end
|
113
|
+
nil
|
76
114
|
end
|
77
|
-
|
115
|
+
|
78
116
|
end
|
117
|
+
|
79
118
|
end
|
80
119
|
end
|
data/lib/sinarey/version.rb
CHANGED
@@ -0,0 +1,254 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
|
5
|
+
module SinareyReloader
|
6
|
+
|
7
|
+
class Watcher
|
8
|
+
|
9
|
+
class Element < Struct.new(:type, :representation)
|
10
|
+
end
|
11
|
+
|
12
|
+
class List
|
13
|
+
@app_list_map = Hash.new { |hash, key| hash[key] = new }
|
14
|
+
|
15
|
+
def self.for(app)
|
16
|
+
@app_list_map[app]
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@path_watcher_map = Hash.new do |hash, key|
|
21
|
+
hash[key] = Watcher.new(key)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def watch(path, element)
|
26
|
+
watcher_for(path).elements << element
|
27
|
+
end
|
28
|
+
|
29
|
+
def ignore(path)
|
30
|
+
watcher_for(path).ignore
|
31
|
+
end
|
32
|
+
|
33
|
+
def watcher_for(path)
|
34
|
+
@path_watcher_map[File.expand_path(path)]
|
35
|
+
end
|
36
|
+
alias watch_file watcher_for
|
37
|
+
|
38
|
+
def watchers
|
39
|
+
@path_watcher_map.values
|
40
|
+
end
|
41
|
+
|
42
|
+
def updated
|
43
|
+
watchers.find_all(&:updated?)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_reader :path, :elements, :mtime
|
48
|
+
|
49
|
+
def initialize(path)
|
50
|
+
@path, @elements = path, []
|
51
|
+
update
|
52
|
+
end
|
53
|
+
|
54
|
+
def updated?
|
55
|
+
!ignore? && !removed? && mtime != File.mtime(path)
|
56
|
+
end
|
57
|
+
|
58
|
+
def update
|
59
|
+
@mtime = File.mtime(path)
|
60
|
+
end
|
61
|
+
|
62
|
+
def inline_templates?
|
63
|
+
elements.any? { |element| element.type == :inline_templates }
|
64
|
+
end
|
65
|
+
|
66
|
+
def ignore
|
67
|
+
@ignore = true
|
68
|
+
end
|
69
|
+
|
70
|
+
def ignore?
|
71
|
+
!!@ignore
|
72
|
+
end
|
73
|
+
|
74
|
+
def removed?
|
75
|
+
!File.exist?(path)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.registered(klass)
|
80
|
+
@reloader_loaded_in ||= {}
|
81
|
+
return if @reloader_loaded_in[klass]
|
82
|
+
|
83
|
+
@reloader_loaded_in[klass] = true
|
84
|
+
|
85
|
+
klass.extend BaseMethods
|
86
|
+
klass.extend ExtensionMethods
|
87
|
+
klass.set(:reload_templates) { true }
|
88
|
+
|
89
|
+
def klass.auto_reload
|
90
|
+
SinareyReloader.perform(settings)
|
91
|
+
end
|
92
|
+
|
93
|
+
klass.before do
|
94
|
+
SinareyReloader.perform(klass)
|
95
|
+
end
|
96
|
+
klass.set(:inline_templates, klass.app_file) if klass == Sinatra::Application
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.perform(klass)
|
100
|
+
updated_sum = 0
|
101
|
+
Watcher::List.for(klass).updated.each do |watcher|
|
102
|
+
updated_sum += 1
|
103
|
+
klass.set(:inline_templates, watcher.path) if watcher.inline_templates?
|
104
|
+
watcher.elements.each { |element|
|
105
|
+
klass.deactivate(element)
|
106
|
+
}
|
107
|
+
$LOADED_FEATURES.delete(watcher.path)
|
108
|
+
require watcher.path
|
109
|
+
watcher.update
|
110
|
+
end
|
111
|
+
return updated_sum
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.thread_safe?
|
115
|
+
Thread and Thread.list.size > 1 and Thread.respond_to?(:exclusive)
|
116
|
+
end
|
117
|
+
|
118
|
+
module BaseMethods
|
119
|
+
def run!(*args)
|
120
|
+
if settings.reloader?
|
121
|
+
super unless running?
|
122
|
+
else
|
123
|
+
super
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def compile!(verb, path, block, options = {})
|
128
|
+
source_location = block.respond_to?(:source_location) ?
|
129
|
+
block.source_location.first : caller_files[1]
|
130
|
+
signature = super
|
131
|
+
watch_element(
|
132
|
+
source_location, :route, { :verb => verb, :signature => signature }
|
133
|
+
)
|
134
|
+
signature
|
135
|
+
end
|
136
|
+
|
137
|
+
def turbo_compile!(verb, path, block, options = {})
|
138
|
+
source_location = block.respond_to?(:source_location) ?
|
139
|
+
block.source_location.first : caller_files[1]
|
140
|
+
signature = super
|
141
|
+
watch_element(
|
142
|
+
source_location, :turbo_route, { :verb => verb, :path => path, :block_id => signature }
|
143
|
+
)
|
144
|
+
signature
|
145
|
+
end
|
146
|
+
|
147
|
+
def inline_templates=(file=nil)
|
148
|
+
file = (file.nil? || file == true) ?
|
149
|
+
(caller_files[1] || File.expand_path($0)) : file
|
150
|
+
watch_element(file, :inline_templates)
|
151
|
+
super
|
152
|
+
end
|
153
|
+
|
154
|
+
def use(middleware, *args, &block)
|
155
|
+
path = caller_files[1] || File.expand_path($0)
|
156
|
+
watch_element(path, :middleware, [middleware, args, block])
|
157
|
+
super
|
158
|
+
end
|
159
|
+
|
160
|
+
def add_filter(type, path = nil, options = {}, &block)
|
161
|
+
source_location = block.respond_to?(:source_location) ?
|
162
|
+
block.source_location.first : caller_files[1]
|
163
|
+
result = super
|
164
|
+
watch_element(source_location, :"#{type}_filter", filters[type].last)
|
165
|
+
result
|
166
|
+
end
|
167
|
+
|
168
|
+
def error(*codes, &block)
|
169
|
+
path = caller_files[1] || File.expand_path($0)
|
170
|
+
result = super
|
171
|
+
codes.each do |c|
|
172
|
+
watch_element(path, :error, :code => c, :handler => @errors[c])
|
173
|
+
end
|
174
|
+
result
|
175
|
+
end
|
176
|
+
|
177
|
+
def register(*extensions, &block)
|
178
|
+
start_registering_extension
|
179
|
+
result = super
|
180
|
+
stop_registering_extension
|
181
|
+
result
|
182
|
+
end
|
183
|
+
|
184
|
+
def inherited(subclass)
|
185
|
+
result = super
|
186
|
+
subclass.register Sinatra::SinareyReloader
|
187
|
+
result
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
module ExtensionMethods
|
192
|
+
def deactivate(element)
|
193
|
+
case element.type
|
194
|
+
when :route then
|
195
|
+
verb = element.representation[:verb]
|
196
|
+
signature = element.representation[:signature]
|
197
|
+
block_id = signature.last
|
198
|
+
(blocks ||= {}).delete(block_id)
|
199
|
+
(routes[verb] ||= []).delete(signature)
|
200
|
+
when :turbo_route then
|
201
|
+
verb = element.representation[:verb]
|
202
|
+
path = element.representation[:path]
|
203
|
+
block_id = element.representation[:block_id]
|
204
|
+
(blocks ||= {}).delete(block_id)
|
205
|
+
(turbo_routes[verb] ||= {}).delete(path)
|
206
|
+
when :middleware then
|
207
|
+
@middleware.delete(element.representation)
|
208
|
+
when :before_filter then
|
209
|
+
filters[:before].delete(element.representation)
|
210
|
+
when :after_filter then
|
211
|
+
filters[:after].delete(element.representation)
|
212
|
+
when :error then
|
213
|
+
code = element.representation[:code]
|
214
|
+
handler = element.representation[:handler]
|
215
|
+
@errors.delete(code) if @errors[code] == handler
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def also_reload(*glob)
|
220
|
+
Dir[*glob].each { |path| Watcher::List.for(self).watch_file(path) }
|
221
|
+
end
|
222
|
+
|
223
|
+
def dont_reload(*glob)
|
224
|
+
Dir[*glob].each { |path| Watcher::List.for(self).ignore(path) }
|
225
|
+
end
|
226
|
+
|
227
|
+
private
|
228
|
+
|
229
|
+
attr_reader :register_path
|
230
|
+
|
231
|
+
def start_registering_extension
|
232
|
+
@register_path = caller_files[2]
|
233
|
+
end
|
234
|
+
|
235
|
+
def stop_registering_extension
|
236
|
+
@register_path = nil
|
237
|
+
end
|
238
|
+
|
239
|
+
def registering_extension?
|
240
|
+
!register_path.nil?
|
241
|
+
end
|
242
|
+
|
243
|
+
def watch_element(path, type, representation=nil)
|
244
|
+
list = Watcher::List.for(self)
|
245
|
+
element = Watcher::Element.new(type, representation)
|
246
|
+
list.watch(path, element)
|
247
|
+
list.watch(register_path, element) if registering_extension?
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
register SinareyReloader
|
253
|
+
Delegator.delegate :also_reload, :dont_reload
|
254
|
+
end
|
data/sinarey.gemspec
CHANGED
@@ -10,7 +10,8 @@ Gem::Specification.new 'sinarey',Sinarey::VERSION do |spec|
|
|
10
10
|
spec.homepage = "https://github.com/maymay25/sinarey"
|
11
11
|
spec.license = "MIT"
|
12
12
|
|
13
|
-
spec.files = ['lib/
|
13
|
+
spec.files = ['lib/sinatra/sinarey_reloader.rb',
|
14
|
+
'lib/sinarey/version.rb',
|
14
15
|
'lib/sinarey/base.rb',
|
15
16
|
'lib/sinarey/router.rb',
|
16
17
|
'lib/sinarey.rb',
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinarey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeffrey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -31,6 +31,7 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- lib/sinatra/sinarey_reloader.rb
|
34
35
|
- lib/sinarey/version.rb
|
35
36
|
- lib/sinarey/base.rb
|
36
37
|
- lib/sinarey/router.rb
|