sinatra 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sinatra might be problematic. Click here for more details.

data/CHANGELOG CHANGED
@@ -1 +1,2 @@
1
+ v0.1.5 Mutex and before-filters
1
2
  v0.1.0 Released!
data/RakeFile CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'rake/testtask'
2
2
  require 'ftools'
3
- require 'hoe'
4
3
 
5
4
  Version = '0.1.0'
6
5
 
@@ -20,7 +19,7 @@ begin
20
19
  p.email = "blake.mizerany@gmail.com"
21
20
  p.test_pattern = 'test/**/*_test.rb'
22
21
  p.include_rakefile = true
23
- p.rdoc_pattern = ['README', 'LICENSE'] << Dir.glob('lib/**/*.rb') << Dir.glob('vendor/**/*.rb')
22
+ p.rdoc_pattern = ['README', 'LICENSE'] + Dir.glob('lib/**/*.rb') + Dir.glob('vendor/**/*.rb')
24
23
  p.docs_host = "bmizerany@rubyforge.org:/var/www/gforge-projects/sinatra/"
25
24
  end
26
25
 
data/Rakefile CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'rake/testtask'
2
2
  require 'ftools'
3
- require 'hoe'
4
3
 
5
4
  Version = '0.1.0'
6
5
 
@@ -20,7 +19,7 @@ begin
20
19
  p.email = "blake.mizerany@gmail.com"
21
20
  p.test_pattern = 'test/**/*_test.rb'
22
21
  p.include_rakefile = true
23
- p.rdoc_pattern = ['README', 'LICENSE'] << Dir.glob('lib/**/*.rb') << Dir.glob('vendor/**/*.rb')
22
+ p.rdoc_pattern = ['README', 'LICENSE'] + Dir.glob('lib/**/*.rb') + Dir.glob('vendor/**/*.rb')
24
23
  p.docs_host = "bmizerany@rubyforge.org:/var/www/gforge-projects/sinatra/"
25
24
  end
26
25
 
@@ -54,11 +54,34 @@ module Sinatra
54
54
  Sinatra::Event.new(:delete, path, &block)
55
55
  end
56
56
 
57
+ # Run given block after each Event's execution
58
+ # Usage:
59
+ # before_attend do
60
+ # logger.debug "After event attend!"
61
+ # end
62
+ # or
63
+ # before_attend :authorize # authorize is a helper method defined using helpers
64
+ #
65
+ # Stop execution using - throw :halt
66
+ # before_attend do
67
+ # throw :halt, 401 unless has_access?
68
+ # end
69
+ # Throw a Symbol to execute a helper method
70
+ # Throw a String to render it as the content
71
+ # Throw a Fixnum to set the status
72
+ #
73
+ def before_attend(filter_name = nil, &block)
74
+ Sinatra::Event.before_attend(filter_name, &block)
75
+ end
76
+
57
77
  # Run given block after each Event's execution
58
78
  # Example:
59
79
  # after_attend do
60
80
  # logger.debug "After event attend!"
61
81
  # end
82
+ # or
83
+ # after_attend :clean_up # clean_up is a helper method defined using helpers
84
+ #
62
85
  def after_attend(filter_name = nil, &block)
63
86
  Sinatra::Event.after_attend(filter_name, &block)
64
87
  end
@@ -1,3 +1,5 @@
1
+ require 'thread'
2
+
1
3
  module Sinatra
2
4
 
3
5
  module EventManager # :nodoc:
@@ -43,13 +45,30 @@ module Sinatra
43
45
 
44
46
  cattr_accessor :logger
45
47
  cattr_accessor :after_filters
48
+ cattr_accessor :before_filters
49
+
50
+ @@mutex = Mutex.new
46
51
 
52
+ self.before_filters = []
47
53
  self.after_filters = []
48
54
 
49
- def self.after_attend(filter)
50
- after_filters << filter
55
+ def self.before_attend(method_name = nil, &block)
56
+ setup_filter(:before_filters, method_name, &block)
57
+ end
58
+
59
+ def self.after_attend(method_name = nil, &block)
60
+ setup_filter(:after_filters, method_name, &block)
51
61
  end
52
62
 
63
+ def self.setup_filter(filter_set_name, method_name, &block)
64
+ raise "Must specify method or block" if method_name.nil? and !block_given?
65
+ send(filter_set_name) << if block_given?
66
+ block
67
+ else
68
+ method_name
69
+ end
70
+ end
71
+
53
72
  after_attend :log_event
54
73
 
55
74
  attr_reader :path, :verb
@@ -65,13 +84,27 @@ module Sinatra
65
84
  def attend(request)
66
85
  request.params.merge!(@route.params)
67
86
  context = EventContext.new(request)
68
- begin
69
- result = context.instance_eval(&@block) if @block
70
- context.body context.body || result || ''
71
- rescue => e
72
- context.error e
87
+ run_safely do
88
+ caught = catch(:halt) do
89
+ call_filters(before_filters, context)
90
+ end
91
+ body = case caught
92
+ when :filter_chain_completed
93
+ begin
94
+ context.instance_eval(&@block) if @block
95
+ rescue => e
96
+ context.error e
97
+ end
98
+ when Symbol
99
+ context.send(caught)
100
+ when String
101
+ caught
102
+ when Fixnum
103
+ context.status caught
104
+ end
105
+ context.body context.body || body || ''
106
+ call_filters(after_filters, context)
73
107
  end
74
- run_through_after_filters(context)
75
108
  context
76
109
  end
77
110
  alias :call :attend
@@ -82,8 +115,28 @@ module Sinatra
82
115
 
83
116
  private
84
117
 
85
- def run_through_after_filters(context)
86
- after_filters.each { |filter| context.send(filter) }
118
+ def run_safely
119
+ if Options.use_mutex?
120
+ @@mutex.synchronize do
121
+ yield
122
+ end
123
+ else
124
+ yield
125
+ end
126
+ end
127
+
128
+ # Adapted from Merb
129
+ # calls a filter chain according to rules.
130
+ def call_filters(filter_set, context)
131
+ filter_set.each do |filter|
132
+ case filter
133
+ when Symbol, String
134
+ context.send(filter)
135
+ when Proc
136
+ context.instance_eval(&filter)
137
+ end
138
+ end
139
+ :filter_chain_completed
87
140
  end
88
141
 
89
142
  end
@@ -28,6 +28,7 @@ module Sinatra
28
28
  f.puts
29
29
  f.puts body
30
30
  end
31
+ nil
31
32
  end
32
33
  alias :mate :show!
33
34
 
@@ -7,6 +7,9 @@ module Sinatra
7
7
  attr_with_default :port, 4567
8
8
  attr_with_default :environment, :development
9
9
  attr_with_default :console, nil
10
+ attr_with_default :use_mutex, false
11
+
12
+ alias :use_mutex? :use_mutex
10
13
 
11
14
  def parse!(args)
12
15
  return if @environment == :test
@@ -24,6 +27,9 @@ module Sinatra
24
27
  puts opts
25
28
  exit!
26
29
  end
30
+ opts.on '-X', '--mutex', 'Use mutex lock when attending events' do
31
+ @use_mutex = true
32
+ end
27
33
  end.parse!(ARGV)
28
34
  end
29
35
 
@@ -19,6 +19,9 @@ module Sinatra
19
19
  end
20
20
  end
21
21
  self.class.running = true
22
+ rescue Errno::EADDRINUSE => e
23
+ puts "== Someone is already performing on port #{Options.port}!"
24
+ logger.exception e
22
25
  rescue => e
23
26
  logger.exception e
24
27
  ensure
@@ -1,11 +1,11 @@
1
1
 
2
- # Gem::Specification for Sinatra-0.1.0
2
+ # Gem::Specification for Sinatra-0.1.5
3
3
  # Originally generated by Echoe
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = %q{sinatra}
7
- s.version = "0.1.0"
8
- s.date = %q{2007-10-04}
7
+ s.version = "0.1.5"
8
+ s.date = %q{2007-10-07}
9
9
  s.summary = %q{Sinatra is a classy web-framework dressed in a DSL}
10
10
  s.email = %q{blake.mizerany@gmail.com}
11
11
  s.homepage = %q{http://sinatra.rubyforge.org/}
@@ -24,7 +24,6 @@ end
24
24
  #
25
25
  # require 'rake/testtask'
26
26
  # require 'ftools'
27
- # require 'hoe'
28
27
  #
29
28
  # Version = '0.1.0'
30
29
  #
@@ -44,7 +43,7 @@ end
44
43
  # p.email = "blake.mizerany@gmail.com"
45
44
  # p.test_pattern = 'test/**/*_test.rb'
46
45
  # p.include_rakefile = true
47
- # p.rdoc_pattern = ['README', 'LICENSE'] << Dir.glob('lib/**/*.rb') << Dir.glob('vendor/**/*.rb')
46
+ # p.rdoc_pattern = ['README', 'LICENSE'] + Dir.glob('lib/**/*.rb') + Dir.glob('vendor/**/*.rb')
48
47
  # p.docs_host = "bmizerany@rubyforge.org:/var/www/gforge-projects/sinatra/"
49
48
  # end
50
49
  #
@@ -85,13 +85,17 @@ end</pre>
85
85
  <pre><a href="http://repo.or.cz/w/sinatra.git?a=tree;f=examples;h=fd7513e49762738ad945adbb2e15bb31528b4207;hb=HEAD">here</a></pre>
86
86
  </p>
87
87
  <p>
88
- <strong>Contribute!</strong><br />
89
- <pre><a href="http://repo.or.cz/w/sinatra.git">using git</a></pre>
88
+ <strong>Docs!</strong><br />
89
+ <pre><a href="http://sinatra.rubyforge.org/doc/">here</a></pre>
90
90
  </p>
91
91
  <p>
92
- <strong>Docs!</strong><br />
92
+ <strong>Can you <a href="http://digg.com/programming/Sinatra_Classy_web_development_dressed_in_a_DSL">digg</a> it cats?</strong><br />
93
93
  <pre><a href="http://sinatra.rubyforge.org/doc/">here</a></pre>
94
94
  </p>
95
+ <p>
96
+ <strong>Contribute!</strong><br />
97
+ <pre><a href="http://repo.or.cz/w/sinatra.git">using git</a></pre>
98
+ </p>
95
99
 
96
100
  </div>
97
101
  </div>
@@ -85,13 +85,17 @@ end</pre>
85
85
  <pre><a href="http://repo.or.cz/w/sinatra.git?a=tree;f=examples;h=fd7513e49762738ad945adbb2e15bb31528b4207;hb=HEAD">here</a></pre>
86
86
  </p>
87
87
  <p>
88
- <strong>Contribute!</strong><br />
89
- <pre><a href="http://repo.or.cz/w/sinatra.git">using git</a></pre>
88
+ <strong>Docs!</strong><br />
89
+ <pre><a href="http://sinatra.rubyforge.org/doc/">here</a></pre>
90
90
  </p>
91
91
  <p>
92
- <strong>Docs!</strong><br />
92
+ <strong>Can you <a href="http://digg.com/programming/Sinatra_Classy_web_development_dressed_in_a_DSL">digg</a> it cats?</strong><br />
93
93
  <pre><a href="http://sinatra.rubyforge.org/doc/">here</a></pre>
94
94
  </p>
95
+ <p>
96
+ <strong>Contribute!</strong><br />
97
+ <pre><a href="http://repo.or.cz/w/sinatra.git">using git</a></pre>
98
+ </p>
95
99
 
96
100
  </div>
97
101
  </div>
@@ -33,5 +33,14 @@ describe "Event" do
33
33
  Sinatra::EventManager.expects(:not_found)
34
34
  Sinatra::EventManager.determine_event(:get, '/asdfsasd')
35
35
  end
36
+
37
+ it "should not execute event if halted" do
38
+ Sinatra::Event.before_filters << lambda { throw :halt, 'whoa!' }
39
+ event = Sinatra::Event.new(:get, '/') do
40
+ foo
41
+ end
42
+ event.expects(:foo).never
43
+ get_it('/').should.equal 'whoa!'
44
+ end
36
45
 
37
46
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
2
+ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: sinatra
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2007-10-04 00:00:00 -07:00
6
+ version: 0.1.5
7
+ date: 2007-10-07 00:00:00 -07:00
8
8
  summary: Sinatra is a classy web-framework dressed in a DSL
9
9
  require_paths:
10
10
  - lib