Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SettingObject: Refactored complex #method_missing

  • Loading branch information...
commit 2bd272b1cc59e5e4dd8f382ec14943fa8b88c5f9 1 parent b428d01
@ledermann authored
Showing with 21 additions and 14 deletions.
  1. +21 −14 lib/app/models/setting_object.rb
View
35 lib/app/models/setting_object.rb
@@ -6,7 +6,8 @@ class SettingObject < ActiveRecord::Base
validates_presence_of :var, :value, :target_type
validate do
- unless target_class.default_settings[var.to_sym]
+ unless _target_class
+.default_settings[var.to_sym]
errors.add(:var, "#{var} is not defined!")
end
end
@@ -25,19 +26,9 @@ def method_missing(method_name, *args, &block)
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
- end
- end
+ _set_value($1, args.first)
elsif method_name.to_s =~ REGEX_GETTER && args.size == 0
- # Getter
- self.value[$1] || target_class.default_settings[var.to_sym][$1]
+ _get_value($1)
else
super
end
@@ -45,7 +36,23 @@ def method_missing(method_name, *args, &block)
end
private
- def target_class
+ def _get_value(name)
+ value[name] || _target_class.default_settings[var.to_sym][name]
+ end
+
+ def _set_value(name, v)
+ if value[name] != v
+ value_will_change!
+
+ if v.nil?
+ value.delete(name)
+ else
+ value[name] = v
+ end
+ end
+ end
+
+ def _target_class
target_type.constantize
end
Please sign in to comment.
Something went wrong with that request. Please try again.