detect whether a second argument was passed to #set correctly
This commit is contained in:
parent
afe9ddf7f3
commit
1632f24b7d
|
@ -944,14 +944,15 @@ module Sinatra
|
||||||
|
|
||||||
# Sets an option to the given value. If the value is a proc,
|
# Sets an option to the given value. If the value is a proc,
|
||||||
# the proc will be called every time the option is accessed.
|
# the proc will be called every time the option is accessed.
|
||||||
def set(option, value=self, &block)
|
def set(option, value = (not_set = true), &block)
|
||||||
raise ArgumentError if block && value != self
|
raise ArgumentError if block and !not_set
|
||||||
value = block if block
|
value = block if block
|
||||||
if value.kind_of?(Proc)
|
if value.kind_of?(Proc)
|
||||||
metadef(option, &value)
|
metadef(option, &value)
|
||||||
metadef("#{option}?") { !!__send__(option) }
|
metadef("#{option}?") { !!__send__(option) }
|
||||||
metadef("#{option}=") { |val| metadef(option, &Proc.new{val}) }
|
metadef("#{option}=") { |val| metadef(option, &Proc.new{val}) }
|
||||||
elsif value == self && option.respond_to?(:each)
|
elsif not_set
|
||||||
|
raise ArgumentError unless option.respond_to?(:each)
|
||||||
option.each { |k,v| set(k, v) }
|
option.each { |k,v| set(k, v) }
|
||||||
elsif respond_to?("#{option}=")
|
elsif respond_to?("#{option}=")
|
||||||
__send__ "#{option}=", value
|
__send__ "#{option}=", value
|
||||||
|
|
|
@ -34,6 +34,24 @@ class SettingsTest < Test::Unit::TestCase
|
||||||
assert !@base.respond_to?(:fiz)
|
assert !@base.respond_to?(:fiz)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'raises an error without value and block' do
|
||||||
|
assert_raise(ArgumentError) { @base.set(:fiz) }
|
||||||
|
assert !@base.respond_to?(:fiz)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'allows setting a value to the app class' do
|
||||||
|
@base.set :base, @base
|
||||||
|
assert @base.respond_to?(:base)
|
||||||
|
assert_equal @base, @base.base
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises an error with the app class as value and a block' do
|
||||||
|
assert_raise ArgumentError do
|
||||||
|
@base.set(:fiz, @base) { 'baz' }
|
||||||
|
end
|
||||||
|
assert !@base.respond_to?(:fiz)
|
||||||
|
end
|
||||||
|
|
||||||
it "sets multiple settings with a Hash" do
|
it "sets multiple settings with a Hash" do
|
||||||
@base.set :foo => 1234,
|
@base.set :foo => 1234,
|
||||||
:bar => 'Hello World',
|
:bar => 'Hello World',
|
||||||
|
|
Loading…
Reference in New Issue