lib/tunable/model.rb in tunable-0.0.2 vs lib/tunable/model.rb in tunable-0.0.3

- old
+ new

@@ -84,18 +84,11 @@ main_settings_list.push(field) set_default_setting(:main, field, default) unless default.nil? define_method field do - if instance_variable_defined?("@setting_main_#{field}") - # the instance var is already normalized to 1/0 when called by the setter - Tunable.getter_value(instance_variable_get("@setting_main_#{field}")) - else - current = main_settings[field.to_sym] - default_value = default.is_a?(Proc) ? default.call(self) : default - current.nil? ? default_value : current - end + get_value_for(field) end define_method "#{field}?" do main_setting_on?(field.to_sym) end @@ -109,14 +102,15 @@ if strict && !default.nil? && !Tunable.matching_type(raw_value, default) raise "Invalid value: #{raw_value}. Expected #{default.class}, got #{raw_value.class}" end value = Tunable.normalize_value(raw_value) - current = Tunable.normalize_value(send("#{field}")) + current = Tunable.normalize_value(get_value_for(field, false)) # don't fallback to default # debug "Setting #{field} to #{value} (#{value.class}), current: #{current} (#{current.class})" if value === current + # puts 'Value is same as current' send('changed_attributes').delete(field) # in case we had set if before return end instance_variable_set("@setting_main_#{field}", value) @@ -197,9 +191,25 @@ end (deleted_settings[context.to_sym] ||= []) << key.to_sym end private + + def get_value_for(field, use_default = true) + if instance_variable_defined?("@setting_main_#{field}") + # the instance var is already normalized to 1/0 when called by the setter + Tunable.getter_value(instance_variable_get("@setting_main_#{field}")) + else + current = main_settings[field.to_sym] + return current if current.present? or !use_default + + if default = self.class.default_settings(:main)[field.to_sym] + return default.is_a?(Proc) ? default.call(self) : default + end + + nil + end + end def modified_settings @modified_settings ||= {} end