Skip to content
This repository
Browse code

detect whether a second argument was passed to #set correctly

  • Loading branch information...
commit 5432f570f7f7024cac59f33429f40c2d65b3ef24 1 parent 696acf2
Konstantin Haase authored
7  lib/sinatra/base.rb
@@ -949,14 +949,15 @@ def middleware
949 949
 
950 950
       # Sets an option to the given value.  If the value is a proc,
951 951
       # the proc will be called every time the option is accessed.
952  
-      def set(option, value=self, &block)
953  
-        raise ArgumentError if block && value != self
  952
+      def set(option, value = (not_set = true), &block)
  953
+        raise ArgumentError if block and !not_set
954 954
         value = block if block
955 955
         if value.kind_of?(Proc)
956 956
           metadef(option, &value)
957 957
           metadef("#{option}?") { !!__send__(option) }
958 958
           metadef("#{option}=") { |val| metadef(option, &Proc.new{val}) }
959  
-        elsif value == self && option.respond_to?(:each)
  959
+        elsif not_set
  960
+          raise ArgumentError unless option.respond_to?(:each)
960 961
           option.each { |k,v| set(k, v) }
961 962
         elsif respond_to?("#{option}=")
962 963
           __send__ "#{option}=", value
18  test/settings_test.rb
@@ -34,6 +34,24 @@ class SettingsTest < Test::Unit::TestCase
34 34
     assert !@base.respond_to?(:fiz)
35 35
   end
36 36
 
  37
+  it 'raises an error without value and block' do
  38
+    assert_raise(ArgumentError) { @base.set(:fiz) }
  39
+    assert !@base.respond_to?(:fiz)
  40
+  end
  41
+
  42
+  it 'allows setting a value to the app class' do
  43
+    @base.set :base, @base
  44
+    assert @base.respond_to?(:base)
  45
+    assert_equal @base, @base.base
  46
+  end
  47
+
  48
+  it 'raises an error with the app class as value and a block' do
  49
+    assert_raise ArgumentError do
  50
+      @base.set(:fiz, @base) { 'baz' }
  51
+    end
  52
+    assert !@base.respond_to?(:fiz)
  53
+  end
  54
+
37 55
   it "sets multiple settings with a Hash" do
38 56
     @base.set :foo => 1234,
39 57
         :bar => 'Hello World',

0 notes on commit 5432f57

Please sign in to comment.
Something went wrong with that request. Please try again.