Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SettingObject: Improved getter handling

  • Loading branch information...
commit e894e4e8fc3b46b2c9a92f64a73095ce6b4c9e52 1 parent 3bce9e4
@ledermann authored
Showing with 11 additions and 3 deletions.
  1. +6 −3 lib/app/models/setting_object.rb
  2. +5 −0 spec/setting_object_spec.rb
View
9 lib/app/models/setting_object.rb
@@ -13,7 +13,8 @@ class SettingObject < ActiveRecord::Base
serialize :value, Hash
- REGEX_SETTER = /([a-z]\w+)=$/i
+ REGEX_SETTER = /\A([a-z]\w+)=\Z/i
+ REGEX_GETTER = /\A([a-z]\w+)\Z/i
def respond_to?(method_name, include_priv=false)
super || method_name.to_s =~ REGEX_SETTER
@@ -31,9 +32,11 @@ def method_missing(method_name, *args, &block)
self.value[$1] = args.first
end
end
- else
+ elsif method_name.to_s =~ REGEX_GETTER
# Getter
- self.value[method_name.to_s] || target_class.default_settings[var.to_sym][method_name.to_s]
+ self.value[$1] || target_class.default_settings[var.to_sym][$1]
+ else
+ super
end
end
View
5 spec/setting_object_spec.rb
@@ -12,6 +12,11 @@
new_setting_object.should respond_to(:bar=)
end
+ it "should not respond to some getters" do
+ expect { new_setting_object.foo! }.to raise_error(NoMethodError)
+ expect { new_setting_object.foo? }.to raise_error(NoMethodError)
+ end
+
it "should return nil for unknown attribute" do
new_setting_object.foo.should eq(nil)
new_setting_object.bar.should eq(nil)
Please sign in to comment.
Something went wrong with that request. Please try again.