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.
@@ -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
- logger.info ">> #{request.env['REQUEST_METHOD']} #{request.env['PATH_INFO']}"
40
- logger.info " action: #{action.to_s.upcase}"
41
- logger.info " params: #{request.params.inspect}"
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
- @prefix ||= options[:prefix] || model.plural
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([:index, :show, :new, :create, :edit, :update, :destroy]),
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(record, maker)
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(record, maker)
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 ||= DEFAULTS.dup
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
@@ -1,6 +1,7 @@
1
1
  $LOAD_PATH << File.join(File.dirname(__FILE__))
2
2
 
3
3
  require 'rubygems'
4
+ require 'benchmark'
4
5
  require 'sinatra/base'
5
6
  require 'extlib'
6
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakajima-sinatras-hat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima