Permalink
Browse files

SettingObject: Don't respond if block or args given

  • Loading branch information...
1 parent adb712b commit 3f9a7cac9751ef638e263abfda4e3b8be6e350b8 @ledermann committed Mar 5, 2013
Showing with 29 additions and 13 deletions.
  1. +17 −13 lib/app/models/setting_object.rb
  2. +12 −0 spec/setting_object_spec.rb
View
30 lib/app/models/setting_object.rb
@@ -21,22 +21,26 @@ def respond_to?(method_name, include_priv=false)
end
def method_missing(method_name, *args, &block)
- if method_name.to_s =~ REGEX_SETTER
- # Setter
- if self.value[$1] != args.first
- self.value_will_change!
+ if block_given?
+ super
+ else
+ if method_name.to_s =~ REGEX_SETTER && args.size == 1
+ # Setter
+ if self.value[$1] != args.first
+ self.value_will_change!
- if args.first.nil?
- self.value.delete($1)
- else
- self.value[$1] = args.first
+ if args.first.nil?
+ self.value.delete($1)
+ else
+ self.value[$1] = args.first
+ end
end
+ elsif method_name.to_s =~ REGEX_GETTER && args.size == 0
+ # Getter
+ self.value[$1] || target_class.default_settings[var.to_sym][$1]
+ else
+ super
end
- elsif method_name.to_s =~ REGEX_GETTER
- # Getter
- self.value[$1] || target_class.default_settings[var.to_sym][$1]
- else
- super
end
end
View
12 spec/setting_object_spec.rb
@@ -16,6 +16,18 @@
expect { new_setting_object.foo! }.to raise_error(NoMethodError)
expect { new_setting_object.foo? }.to raise_error(NoMethodError)
end
+
+ it "should not respond if a block is given" do
+ expect {
+ new_setting_object.foo do
+ end
+ }.to raise_error(NoMethodError)
+ end
+
+ it "should not respond if params are given" do
+ expect { new_setting_object.foo(42) }.to raise_error(NoMethodError)
+ expect { new_setting_object.foo(42,43) }.to raise_error(NoMethodError)
+ end
it "should return nil for unknown attribute" do
new_setting_object.foo.should eq(nil)

0 comments on commit 3f9a7ca

Please sign in to comment.