Skip to content
This repository
Browse code

SettingObject: Don't respond if block or args given

  • Loading branch information...
commit 3f9a7cac9751ef638e263abfda4e3b8be6e350b8 1 parent adb712b
Georg Ledermann authored March 05, 2013
30  lib/app/models/setting_object.rb
@@ -21,22 +21,26 @@ def respond_to?(method_name, include_priv=false)
21 21
     end
22 22
 
23 23
     def method_missing(method_name, *args, &block)
24  
-      if method_name.to_s =~ REGEX_SETTER
25  
-        # Setter
26  
-        if self.value[$1] != args.first
27  
-          self.value_will_change!
  24
+      if block_given?
  25
+        super
  26
+      else
  27
+        if method_name.to_s =~ REGEX_SETTER && args.size == 1
  28
+          # Setter
  29
+          if self.value[$1] != args.first
  30
+            self.value_will_change!
28 31
           
29  
-          if args.first.nil?
30  
-            self.value.delete($1)
31  
-          else
32  
-            self.value[$1] = args.first
  32
+            if args.first.nil?
  33
+              self.value.delete($1)
  34
+            else
  35
+              self.value[$1] = args.first
  36
+            end
33 37
           end
  38
+        elsif method_name.to_s =~ REGEX_GETTER && args.size == 0
  39
+          # Getter
  40
+          self.value[$1] || target_class.default_settings[var.to_sym][$1]
  41
+        else
  42
+          super
34 43
         end
35  
-      elsif method_name.to_s =~ REGEX_GETTER
36  
-        # Getter
37  
-        self.value[$1] || target_class.default_settings[var.to_sym][$1]
38  
-      else
39  
-        super
40 44
       end
41 45
     end
42 46
 
12  spec/setting_object_spec.rb
@@ -16,6 +16,18 @@
16 16
         expect { new_setting_object.foo! }.to raise_error(NoMethodError)
17 17
         expect { new_setting_object.foo? }.to raise_error(NoMethodError)
18 18
       end
  19
+      
  20
+      it "should not respond if a block is given" do
  21
+        expect { 
  22
+          new_setting_object.foo do
  23
+          end
  24
+        }.to raise_error(NoMethodError)
  25
+      end
  26
+      
  27
+      it "should not respond if params are given" do
  28
+        expect { new_setting_object.foo(42) }.to raise_error(NoMethodError)
  29
+        expect { new_setting_object.foo(42,43) }.to raise_error(NoMethodError)
  30
+      end
19 31
 
20 32
       it "should return nil for unknown attribute" do
21 33
         new_setting_object.foo.should eq(nil)

0 notes on commit 3f9a7ca

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