diff --git a/CHANGES b/CHANGES index 61404921..06833872 100644 --- a/CHANGES +++ b/CHANGES @@ -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` diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 08e528b3..2a335fb3 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -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 diff --git a/test/helpers_test.rb b/test/helpers_test.rb index f20bf2ce..d8b791ad 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -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