simplificator-has_setting 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/has_setting/ar_extensions.rb +19 -11
- data/test/foo.rb +1 -0
- data/test/unit/has_setting_test.rb +19 -1
- metadata +1 -1
@@ -1,11 +1,17 @@
|
|
1
1
|
module HasSetting
|
2
2
|
module InstanceMethods
|
3
3
|
def write_setting(name, value)
|
4
|
-
setting
|
4
|
+
# find an existing setting or build a new one
|
5
|
+
setting = self.settings.find_by_name(name)
|
6
|
+
setting = self.settings.build(:name => name) if setting.blank?
|
5
7
|
setting.value = value
|
6
|
-
|
7
|
-
#
|
8
|
-
|
8
|
+
# save only if parent has been saved. else we expect a parent.save call
|
9
|
+
# which will cascade to the children since they were created with build()
|
10
|
+
unless self.new_record?
|
11
|
+
setting.save
|
12
|
+
# reload collection so the read_setting() finds the settings with 'detect()'
|
13
|
+
self.settings.reload
|
14
|
+
end
|
9
15
|
end
|
10
16
|
|
11
17
|
def read_setting(name)
|
@@ -31,26 +37,28 @@ module HasSetting
|
|
31
37
|
#
|
32
38
|
# Setup of the getter/setter
|
33
39
|
def has_setting(name, options = {})
|
34
|
-
options[:type] ||= :string
|
35
40
|
name = name.to_s
|
36
41
|
raise ArgumentError.new("Setting name must not be blank") if name.blank?
|
42
|
+
# default settings
|
43
|
+
options[:type] ||= :string # treat as string
|
44
|
+
options[:default] ||= nil # no default value
|
37
45
|
has_setting_options[name] = options
|
38
46
|
|
39
47
|
define_method("#{name}=".intern) do |value|
|
40
48
|
value = value.nil? ? nil : value.to_s
|
41
|
-
write_setting(
|
49
|
+
write_setting(name, value)
|
42
50
|
end
|
43
51
|
|
44
52
|
# getter
|
45
53
|
define_method(name) do |*args|
|
46
|
-
setting = read_setting(
|
47
|
-
return nil if setting.nil?
|
48
|
-
|
54
|
+
setting = read_setting(name)
|
49
55
|
options = args.first || has_setting_options[name]
|
56
|
+
return options[:default] if setting.nil?
|
57
|
+
|
50
58
|
case options[:type]
|
51
59
|
when :string : setting.value
|
52
|
-
when :int : setting.value.
|
53
|
-
when :float : setting.value.
|
60
|
+
when :int : setting.value.nil? ? nil : setting.value.to_i
|
61
|
+
when :float : setting.value.nil? ? nil : setting.value.to_f
|
54
62
|
else raise ArgumentError.new("Unsupported type: #{options[:type]}")
|
55
63
|
end
|
56
64
|
end
|
data/test/foo.rb
CHANGED
@@ -55,7 +55,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
55
55
|
|
56
56
|
def test_has_nil_setting
|
57
57
|
@foo.setting_1 = nil
|
58
|
-
assert(@foo.read_setting('
|
58
|
+
assert(@foo.read_setting('setting_1'))
|
59
59
|
assert(!@foo.setting_1)
|
60
60
|
end
|
61
61
|
|
@@ -77,4 +77,22 @@ class HasSettingTest < Test::Unit::TestCase
|
|
77
77
|
assert_equal(12, @bar.setting_2)
|
78
78
|
end
|
79
79
|
|
80
|
+
def test_default_values()
|
81
|
+
assert_equal('def', @foo.with_default)
|
82
|
+
assert_equal('override def', @foo.with_default(:default => 'override def'))
|
83
|
+
@foo.with_default = 'not def'
|
84
|
+
assert_equal('not def', @foo.with_default)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_write_settings_without_saved_parent
|
88
|
+
my_foo = Foo.new
|
89
|
+
count_before = HasSetting::Setting.count
|
90
|
+
my_foo.with_default = 'radabumm'
|
91
|
+
assert_equal(count_before, HasSetting::Setting.count)
|
92
|
+
assert_equal('radabumm', my_foo.with_default)
|
93
|
+
my_foo.save!
|
94
|
+
assert_equal(count_before + 1, HasSetting::Setting.count)
|
95
|
+
assert_equal('radabumm', my_foo.with_default)
|
96
|
+
end
|
97
|
+
|
80
98
|
end
|