accept values pretending to be a Numeric (like ActiveSupport::Duration), fixes #236

This commit is contained in:
Konstantin Haase 2011-04-11 13:04:28 +02:00
parent c163c3ccdf
commit 7e48254f03
3 changed files with 19 additions and 3 deletions

View File

@ -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 File

@ -340,7 +340,7 @@ module Sinatra
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 @@ module Sinatra
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 @@ module Sinatra
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 File

@ -638,6 +638,14 @@ class HelpersTest < Test::Unit::TestCase
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 @@ class HelpersTest < Test::Unit::TestCase
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