Permalink
Browse files

accept values pretending to be a Numeric (like ActiveSupport::Duratio…

…n), fixes #236
  • Loading branch information...
1 parent c163c3c commit 7e48254f03289adbd854169cac18b0cfcbd046e4 @rkh rkh committed Apr 11, 2011
Showing with 19 additions and 3 deletions.
  1. +3 −0 CHANGES
  2. +3 −3 lib/sinatra/base.rb
  3. +13 −0 test/helpers_test.rb
View
@@ -18,6 +18,9 @@
* Added `request.accept?` and `request.preferred_type` to ease dealing with
`Accept` headers. (Konstantin Haase)
+ * Helpers dealing with time, like `expires`, handle objects that pretend to be
+ numbers, lik `ActiveSupport::Duration`, better. (Konstantin Haase)
+
= 1.2.2 / 2011-04-08
* The `:provides => :js` condition now matches both `application/javascript`
View
@@ -340,7 +340,7 @@ def cache_control(*values)
def expires(amount, *values)
values << {} unless values.last.kind_of?(Hash)
- if Integer === amount
+ if amount.is_a? Integer
time = Time.now + amount
max_age = amount
else
@@ -405,7 +405,7 @@ def back
def time_for(value)
if value.respond_to? :to_time
value.to_time
- elsif Time === value
+ elsif value.is_a? Time
value
elsif value.respond_to? :new_offset
# DateTime#to_time does the same on 1.9
@@ -415,7 +415,7 @@ def time_for(value)
elsif value.respond_to? :mday
# Date#to_time does the same on 1.9
Time.local(value.year, value.mon, value.mday)
- elsif Numeric === value
+ elsif value.is_a? Numeric
Time.at value
else
Time.parse value.to_s
View
@@ -638,6 +638,14 @@ def send_file_app(opts={})
get '/baz' do
expires Time.at(0)
end
+
+ get '/blah' do
+ obj = Object.new
+ def obj.method_missing(*a, &b) 60.send(*a, &b) end
+ def obj.is_a?(thing) 60.is_a?(thing) end
+ expires obj, :public, :no_cache
+ 'Hello World'
+ end
end
end
@@ -660,6 +668,11 @@ def send_file_app(opts={})
get '/baz'
assert_equal 'Thu, 01 Jan 1970 00:00:00 GMT', response['Expires']
end
+
+ it 'accepts values pretending to be a Numeric (like ActiveSupport::Duration)' do
+ get '/blah'
+ assert_equal ['public', 'no-cache', 'max-age=60'], response['Cache-Control'].split(', ')
+ end
end
describe 'last_modified' do

0 comments on commit 7e48254

Please sign in to comment.