sinarey 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,120 +1,120 @@
1
- module Sinarey
2
- class Router
3
- def initialize(*args, &block)
4
- @notfound_app = lambda { |env| [404, {}, ['404']] }
5
- @apps = {}
6
- @turbo_routes = {}
7
- @routes = {}
8
- instance_eval(&block) if block
9
- build_routing_table
10
- end
11
-
12
- def call(env)
13
- route = env["PATH_INFO"]
14
- route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
15
-
16
- if response = apps_route(env["REQUEST_METHOD"], route, env)
17
- response
18
- else
19
- @notfound_app.call(env)
20
- end
21
- end
22
-
23
- def mount(app)
24
- app_id = @apps.size + 1
25
- @apps[app_id] = app
26
- end
27
-
28
- def notfound(app)
29
- @notfound_app = app
30
- end
31
-
32
- private
33
-
34
- def build_routing_table
35
- @apps.each do |app_id, app|
36
- regedit_turbo_routes(app_id, app)
37
- regedit_basic_routes(app_id, app)
38
- end
39
- end
40
-
41
- def regedit_turbo_routes(app_id, app)
42
- return unless app.respond_to?(:turbo_routes)
43
- app.turbo_routes.each do |verb, routes|
44
- routes.each do |path, route|
45
- route.tap do |block_id|
46
- tmp = @turbo_routes[verb] ||= {}
47
- tmp[path] = [block_id, app_id] unless tmp[path]
48
- end
49
- end
50
- end
51
- end
52
-
53
- def regedit_basic_routes(app_id, app)
54
- return unless app.respond_to?(:routes)
55
- app.routes.each do |verb, routes|
56
- routes.each do |pattern, keys, conditions, block_id|
57
- (@routes[verb] ||= []) << [pattern, keys, conditions, block_id, app_id]
58
- end
59
- end
60
- end
61
-
62
- case ENV["RACK_ENV"]
63
- when 'development'
64
-
65
- #development need support sinarey reloader.so here use dev logic.
66
- def apps_route(verb, path, env)
67
-
68
- #auto reload modified code
69
- @apps.each do |index,app|
70
- app.auto_reload if app.respond_to?(:auto_reload)
71
- end
72
-
73
- #rebuild route table
74
- @turbo_routes = {}
75
- @routes = {}
76
- build_routing_table
77
-
78
- if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
79
- turbo_route.tap do |block_id,app_id|
80
- env['sinarey.router'] = {type: :turbo, block_id: block_id}
81
- status, headers, response = @apps[app_id].call(env)
82
- return status, headers, response
83
- end
84
- elsif routes = @routes[verb]
85
- routes.each do |pattern, keys, conditions, block_id, app_id|
86
- if match = pattern.match(path)
87
- env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
88
- status, headers, response = @apps[app_id].call(env)
89
- return status, headers, response
90
- end
91
- end
92
- end
93
- nil
94
- end
95
-
96
- else
97
-
98
- def apps_route(verb, path, env)
99
- if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
100
- turbo_route.tap do |block_id,app_id|
101
- env['sinarey.router'] = {type: :turbo, block_id: block_id}
102
- status, headers, response = @apps[app_id].call(env)
103
- return status, headers, response
104
- end
105
- elsif routes = @routes[verb]
106
- routes.each do |pattern, keys, conditions, block_id, app_id|
107
- if match = pattern.match(path)
108
- env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
109
- status, headers, response = @apps[app_id].call(env)
110
- return status, headers, response
111
- end
112
- end
113
- end
114
- nil
115
- end
116
-
117
- end
118
-
119
- end
120
- end
1
+ module Sinarey
2
+ class Router
3
+ def initialize(*args, &block)
4
+ @notfound_app = lambda { |env| [404, {}, ['404']] }
5
+ @apps = {}
6
+ @turbo_routes = {}
7
+ @routes = {}
8
+ instance_eval(&block) if block
9
+ build_routing_table
10
+ end
11
+
12
+ def call(env)
13
+ route = env["PATH_INFO"]
14
+ route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
15
+
16
+ if response = apps_route(env["REQUEST_METHOD"], route, env)
17
+ response
18
+ else
19
+ @notfound_app.call(env)
20
+ end
21
+ end
22
+
23
+ def mount(app)
24
+ app_id = @apps.size + 1
25
+ @apps[app_id] = app
26
+ end
27
+
28
+ def notfound(app)
29
+ @notfound_app = app
30
+ end
31
+
32
+ private
33
+
34
+ def build_routing_table
35
+ @apps.each do |app_id, app|
36
+ regedit_turbo_routes(app_id, app)
37
+ regedit_basic_routes(app_id, app)
38
+ end
39
+ end
40
+
41
+ def regedit_turbo_routes(app_id, app)
42
+ return unless app.respond_to?(:turbo_routes)
43
+ app.turbo_routes.each do |verb, routes|
44
+ routes.each do |path, route|
45
+ route.tap do |block_id|
46
+ tmp = @turbo_routes[verb] ||= {}
47
+ tmp[path] = [block_id, app_id] unless tmp[path]
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ def regedit_basic_routes(app_id, app)
54
+ return unless app.respond_to?(:routes)
55
+ app.routes.each do |verb, routes|
56
+ routes.each do |pattern, keys, conditions, block_id|
57
+ (@routes[verb] ||= []) << [pattern, keys, conditions, block_id, app_id]
58
+ end
59
+ end
60
+ end
61
+
62
+ case ENV["RACK_ENV"]
63
+ when 'development'
64
+
65
+ #development need support sinarey reloader.so here use dev logic.
66
+ def apps_route(verb, path, env)
67
+
68
+ #auto reload modified code
69
+ @apps.each do |index,app|
70
+ app.auto_reload if app.respond_to?(:auto_reload)
71
+ end
72
+
73
+ #rebuild route table
74
+ @turbo_routes = {}
75
+ @routes = {}
76
+ build_routing_table
77
+
78
+ if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
79
+ turbo_route.tap do |block_id,app_id|
80
+ env['sinarey.router'] = {type: :turbo, block_id: block_id}
81
+ status, headers, response = @apps[app_id].call(env)
82
+ return status, headers, response
83
+ end
84
+ elsif routes = @routes[verb]
85
+ routes.each do |pattern, keys, conditions, block_id, app_id|
86
+ if match = pattern.match(path)
87
+ env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
88
+ status, headers, response = @apps[app_id].call(env)
89
+ return status, headers, response
90
+ end
91
+ end
92
+ end
93
+ nil
94
+ end
95
+
96
+ else
97
+
98
+ def apps_route(verb, path, env)
99
+ if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
100
+ turbo_route.tap do |block_id,app_id|
101
+ env['sinarey.router'] = {type: :turbo, block_id: block_id}
102
+ status, headers, response = @apps[app_id].call(env)
103
+ return status, headers, response
104
+ end
105
+ elsif routes = @routes[verb]
106
+ routes.each do |pattern, keys, conditions, block_id, app_id|
107
+ if match = pattern.match(path)
108
+ env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
109
+ status, headers, response = @apps[app_id].call(env)
110
+ return status, headers, response
111
+ end
112
+ end
113
+ end
114
+ nil
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+ end
@@ -1,3 +1,3 @@
1
- module Sinarey
2
- VERSION = '1.0.5'
3
- end
1
+ module Sinarey
2
+ VERSION = '1.0.6'
3
+ end
@@ -1,254 +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
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