nakajima-sinatras-hat 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sinatras-hat/maker.rb +22 -7
- data/lib/sinatras-hat/resource.rb +2 -2
- data/lib/sinatras-hat/responder.rb +34 -36
- data/lib/sinatras-hat.rb +1 -0
- metadata +1 -1
data/lib/sinatras-hat/maker.rb
CHANGED
@@ -36,11 +36,9 @@ module Sinatra
|
|
36
36
|
request.error(404) unless only.include?(action)
|
37
37
|
protect!(request) if protect.include?(action)
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
instance_exec(request, &self.class.actions[action][:fn])
|
39
|
+
log_with_benchmark(request, action) do
|
40
|
+
instance_exec(request, &self.class.actions[action][:fn])
|
41
|
+
end
|
44
42
|
end
|
45
43
|
|
46
44
|
def after(action)
|
@@ -92,7 +90,7 @@ module Sinatra
|
|
92
90
|
end
|
93
91
|
|
94
92
|
def prefix
|
95
|
-
|
93
|
+
options[:prefix] ||= model.plural
|
96
94
|
end
|
97
95
|
|
98
96
|
def parents
|
@@ -105,7 +103,7 @@ module Sinatra
|
|
105
103
|
|
106
104
|
def options
|
107
105
|
@options ||= {
|
108
|
-
:only => Set.new(
|
106
|
+
:only => Set.new(Maker.actions.keys),
|
109
107
|
:parent => nil,
|
110
108
|
:finder => proc { |model, params| model.all },
|
111
109
|
:record => proc { |model, params| model.find_by_id(params[:id]) },
|
@@ -139,6 +137,23 @@ module Sinatra
|
|
139
137
|
|
140
138
|
private
|
141
139
|
|
140
|
+
def log_with_benchmark(request, action)
|
141
|
+
msg = [ ]
|
142
|
+
msg << "#{request.env['REQUEST_METHOD']} #{request.env['PATH_INFO']}"
|
143
|
+
msg << "Params: #{request.params.inspect}"
|
144
|
+
msg << "Action: #{action.to_s.upcase}"
|
145
|
+
|
146
|
+
logger.info ">> " + msg.join(' | ')
|
147
|
+
|
148
|
+
result = nil
|
149
|
+
|
150
|
+
t = Benchmark.realtime { result = yield }
|
151
|
+
|
152
|
+
logger.info " Request finished in #{t} sec."
|
153
|
+
|
154
|
+
result
|
155
|
+
end
|
156
|
+
|
142
157
|
def resource
|
143
158
|
@resource ||= Resource.new(self)
|
144
159
|
end
|
@@ -11,7 +11,7 @@ module Sinatra
|
|
11
11
|
suffix = suffix.dup
|
12
12
|
|
13
13
|
path = resources.inject("") do |memo, maker|
|
14
|
-
memo += fragment(
|
14
|
+
memo += fragment(maker, record)
|
15
15
|
end
|
16
16
|
|
17
17
|
suffix.gsub!('/:id', "/#{record.id}") if record
|
@@ -21,7 +21,7 @@ module Sinatra
|
|
21
21
|
|
22
22
|
private
|
23
23
|
|
24
|
-
def fragment(
|
24
|
+
def fragment(maker, record)
|
25
25
|
@maker.eql?(maker) ?
|
26
26
|
"/#{maker.prefix}" :
|
27
27
|
"/#{maker.prefix}/" + interpolate(maker, record)
|
@@ -6,41 +6,6 @@ module Sinatra
|
|
6
6
|
class Responder
|
7
7
|
delegate :model, :to => :maker
|
8
8
|
|
9
|
-
DEFAULTS = {
|
10
|
-
:show => {
|
11
|
-
:success => proc { |data| render(:show) },
|
12
|
-
:failure => proc { |data| redirect('/') }
|
13
|
-
},
|
14
|
-
|
15
|
-
:index => {
|
16
|
-
:success => proc { |data| render(:index) },
|
17
|
-
:failure => proc { |data| redirect('/') }
|
18
|
-
},
|
19
|
-
|
20
|
-
:create => {
|
21
|
-
:success => proc { |data| redirect(data) },
|
22
|
-
:failure => proc { |data| render(:new) }
|
23
|
-
},
|
24
|
-
|
25
|
-
:new => {
|
26
|
-
:success => proc { |data| render(:new) },
|
27
|
-
:failure => proc { |data| redirect('/') }
|
28
|
-
},
|
29
|
-
|
30
|
-
:edit => {
|
31
|
-
:success => proc { |data| render(:edit) }
|
32
|
-
},
|
33
|
-
|
34
|
-
:destroy => {
|
35
|
-
:success => proc { |data| redirect(resource_path('/')) }
|
36
|
-
},
|
37
|
-
|
38
|
-
:update => {
|
39
|
-
:success => proc { |data| redirect(data) },
|
40
|
-
:failure => proc { |data| render(:edit) }
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
9
|
attr_reader :maker
|
45
10
|
|
46
11
|
def initialize(maker)
|
@@ -48,7 +13,40 @@ module Sinatra
|
|
48
13
|
end
|
49
14
|
|
50
15
|
def defaults
|
51
|
-
@defaults ||=
|
16
|
+
@defaults ||= {
|
17
|
+
:show => {
|
18
|
+
:success => proc { |data| render(:show) },
|
19
|
+
:failure => proc { |data| redirect('/') }
|
20
|
+
},
|
21
|
+
|
22
|
+
:index => {
|
23
|
+
:success => proc { |data| render(:index) },
|
24
|
+
:failure => proc { |data| redirect('/') }
|
25
|
+
},
|
26
|
+
|
27
|
+
:create => {
|
28
|
+
:success => proc { |data| redirect(data) },
|
29
|
+
:failure => proc { |data| render(:new) }
|
30
|
+
},
|
31
|
+
|
32
|
+
:new => {
|
33
|
+
:success => proc { |data| render(:new) },
|
34
|
+
:failure => proc { |data| redirect('/') }
|
35
|
+
},
|
36
|
+
|
37
|
+
:edit => {
|
38
|
+
:success => proc { |data| render(:edit) }
|
39
|
+
},
|
40
|
+
|
41
|
+
:destroy => {
|
42
|
+
:success => proc { |data| redirect(resource_path('/')) }
|
43
|
+
},
|
44
|
+
|
45
|
+
:update => {
|
46
|
+
:success => proc { |data| redirect(data) },
|
47
|
+
:failure => proc { |data| render(:edit) }
|
48
|
+
}
|
49
|
+
}
|
52
50
|
end
|
53
51
|
|
54
52
|
def success(name, request, data)
|
data/lib/sinatras-hat.rb
CHANGED