simplificator-has_setting 0.4.2 → 0.4.3
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/README +7 -1
- data/VERSION.yml +1 -1
- data/has_setting.gemspec +2 -2
- data/lib/has_setting.rb +3 -1
- data/lib/has_setting/formatters.rb +21 -1
- data/test/unit/formatters_test.rb +43 -3
- data/test/unit/has_setting_test.rb +28 -15
- metadata +3 -4
data/README
CHANGED
@@ -3,6 +3,10 @@ has_setting is a simple extension that enables ActiveRecord models to
|
|
3
3
|
store settings in a separate settings table as key/value pairs where the key and value are stored as Strings.
|
4
4
|
|
5
5
|
==History
|
6
|
+
* 0.4.3:
|
7
|
+
* Changed behaviour of :boolean formatters: Now they treat '0', 0, false, nil and '' as false, everything else as true
|
8
|
+
This is not the same behaviour as ruby (treating only nil and false as false) but should help with input from web forms (i.e. checkboxes)
|
9
|
+
If you want strict ruby boolean behaviour, then use :strict_boolean as :type
|
6
10
|
* 0.4.2:
|
7
11
|
* bug fixes for boolean types default values
|
8
12
|
* 0.3.10:
|
@@ -45,12 +49,14 @@ This will create the following methods for you on the owner class:
|
|
45
49
|
<em>:type</em> allows you to convert the value:
|
46
50
|
* <em>:string</em> (default) Uses the StringFormatter to convert from/to String (actually this formatter just leaves the value as it is)
|
47
51
|
* <em>:int</em> Uses the IntFormatter to convert from/to int values.
|
48
|
-
* <em>:boolean</em> Uses the BooleanFormatter to convert from/to boolean values.
|
52
|
+
* <em>:boolean</em> Uses the BooleanFormatter to convert from/to boolean values; treating 0, '0', false, '' and nil as false.
|
53
|
+
* <em>:strict_boolean</em> Uses the StrictBooleanFormatter to convert from/to boolean values; treating false, and nil as false.
|
49
54
|
* <em>:float</em> Uses the FloatFormatter to convert from/to float values.
|
50
55
|
* <em>:ints</em> Uses the IntsFormatter to convert from/to int[]
|
51
56
|
* <em>:floats</em> Uses the FloatsFormatter to convert from/to float[]
|
52
57
|
* <em>:strings</em> Uses the StringsFormatter to convert from/to string[]
|
53
58
|
* <em>:booleans</em> Uses the BooleansFormatter to convert from/to boolean[]
|
59
|
+
* <em>:strict_booleans</em> Uses the BooleansFormatter to convert from/to boolean[]
|
54
60
|
<em>:default</em> allows you to specify a default value that will be returned if the setting does not exist (i.e. has never been written). Note that the default value is _ignored_ if the setting exists, no matter what the value of the setting is. The default value is returned as is, no type conversion takes place.
|
55
61
|
|
56
62
|
|
data/VERSION.yml
CHANGED
data/has_setting.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{has_setting}
|
5
|
-
s.version = "0.4.
|
5
|
+
s.version = "0.4.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Simplificator GmbH"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-09-02}
|
10
10
|
s.description = %q{Stores settings as key/value pairs in a settings table and provides accessors for them on the owning object}
|
11
11
|
s.email = %q{info@simplificator.com}
|
12
12
|
s.extra_rdoc_files = [
|
data/lib/has_setting.rb
CHANGED
@@ -15,4 +15,6 @@ HasSetting::Formatters.register_formatter(:floats, HasSetting::Formatters::Float
|
|
15
15
|
HasSetting::Formatters.register_formatter(:int, HasSetting::Formatters::IntFormatter.new)
|
16
16
|
HasSetting::Formatters.register_formatter(:ints, HasSetting::Formatters::IntsFormatter.new)
|
17
17
|
HasSetting::Formatters.register_formatter(:boolean, HasSetting::Formatters::BooleanFormatter.new)
|
18
|
-
HasSetting::Formatters.register_formatter(:booleans, HasSetting::Formatters::BooleansFormatter.new)
|
18
|
+
HasSetting::Formatters.register_formatter(:booleans, HasSetting::Formatters::BooleansFormatter.new)
|
19
|
+
HasSetting::Formatters.register_formatter(:strict_boolean, HasSetting::Formatters::StrictBooleanFormatter.new)
|
20
|
+
HasSetting::Formatters.register_formatter(:strict_booleans, HasSetting::Formatters::StrictBooleansFormatter.new)
|
@@ -43,12 +43,15 @@ module HasSetting
|
|
43
43
|
value.to_s
|
44
44
|
end
|
45
45
|
end
|
46
|
+
# Convert a Boolean to String and Back
|
47
|
+
# nil, '0', false, 0 and '' are considered __false__, everything else is __true__
|
48
|
+
# This is not like ruby where only nil and false are considered __false__
|
46
49
|
class BooleanFormatter < NilSafeFormatter
|
47
50
|
def safe_to_type(value)
|
48
51
|
value == '1'
|
49
52
|
end
|
50
53
|
def safe_to_s(value)
|
51
|
-
value ? '1' : '0'
|
54
|
+
value && value != '0' && value != 0 && value != '' ? '1' : '0'
|
52
55
|
end
|
53
56
|
end
|
54
57
|
class BooleansFormatter < NilSafeFormatter
|
@@ -60,6 +63,23 @@ module HasSetting
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
66
|
+
class StrictBooleanFormatter < NilSafeFormatter
|
67
|
+
def safe_to_type(value)
|
68
|
+
value == '1'
|
69
|
+
end
|
70
|
+
def safe_to_s(value)
|
71
|
+
value ? '1' : '0'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
class StrictBooleansFormatter < NilSafeFormatter
|
75
|
+
def safe_to_type(value)
|
76
|
+
value.split(',').map() {|item| Formatters.for_type(:strict_boolean).to_type(item)}
|
77
|
+
end
|
78
|
+
def safe_to_s(value)
|
79
|
+
Array(value).map() {|item| Formatters.for_type(:strict_boolean).to_s(item)}.join(',')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
63
83
|
class IntsFormatter < NilSafeFormatter
|
64
84
|
def safe_to_type(value)
|
65
85
|
value.split(',').map() {|item| Formatters.for_type(:int).to_type(item)}
|
@@ -2,9 +2,15 @@ require File.dirname(__FILE__) + '/../test_helper'
|
|
2
2
|
include HasSetting
|
3
3
|
class FormattersTest < Test::Unit::TestCase
|
4
4
|
def test_for_type
|
5
|
-
[:string, :float, :floats, :int, :ints, :strings, :boolean, :booleans].each do |symbol|
|
5
|
+
[:string, :float, :floats, :int, :ints, :strings, :boolean, :booleans, :strict_boolean, :strict_booleans].each do |symbol|
|
6
6
|
assert(Formatters.for_type(symbol), "No formatter for #{symbol}")
|
7
|
-
|
7
|
+
if symbol == :strict_boolean
|
8
|
+
assert_equal(Formatters.for_type(symbol).class.to_s, "HasSetting::Formatters::StrictBooleanFormatter")
|
9
|
+
elsif symbol == :strict_booleans
|
10
|
+
assert_equal(Formatters.for_type(symbol).class.to_s, "HasSetting::Formatters::StrictBooleansFormatter")
|
11
|
+
else
|
12
|
+
assert_equal(Formatters.for_type(symbol).class.to_s, "HasSetting::Formatters::#{symbol.to_s.capitalize}Formatter")
|
13
|
+
end
|
8
14
|
end
|
9
15
|
assert_raises(ArgumentError) do
|
10
16
|
Formatters.for_type(:rarararararara_i_do_not_exist)
|
@@ -39,9 +45,12 @@ class FormattersTest < Test::Unit::TestCase
|
|
39
45
|
|
40
46
|
def test_boolean_formatter
|
41
47
|
f = Formatters::BooleanFormatter.new
|
42
|
-
assert_equal('
|
48
|
+
assert_equal('0', f.to_s(''))
|
43
49
|
assert_equal('1', f.to_s(true))
|
44
50
|
assert_equal('0', f.to_s(false))
|
51
|
+
assert_equal('0', f.to_s('0'))
|
52
|
+
assert_equal('0', f.to_s(0))
|
53
|
+
assert_equal('0', f.to_s(''))
|
45
54
|
assert_equal(nil, f.to_s(nil))
|
46
55
|
|
47
56
|
assert_equal(true, f.to_type('1'))
|
@@ -49,6 +58,18 @@ class FormattersTest < Test::Unit::TestCase
|
|
49
58
|
assert_equal(nil, f.to_type(nil))
|
50
59
|
end
|
51
60
|
|
61
|
+
def test_strict_boolean_formatter
|
62
|
+
f = Formatters::StrictBooleanFormatter.new
|
63
|
+
assert_equal('1', f.to_s(''))
|
64
|
+
assert_equal('1', f.to_s(true))
|
65
|
+
assert_equal('0', f.to_s(false))
|
66
|
+
assert_equal('1', f.to_s('0'))
|
67
|
+
assert_equal(nil, f.to_s(nil))
|
68
|
+
|
69
|
+
assert_equal(true, f.to_type('1'))
|
70
|
+
assert_equal(false, f.to_type('0'))
|
71
|
+
assert_equal(nil, f.to_type(nil))
|
72
|
+
end
|
52
73
|
def test_int_formatter()
|
53
74
|
f = Formatters::IntFormatter.new
|
54
75
|
assert_raises(ArgumentError) do
|
@@ -127,6 +148,7 @@ class FormattersTest < Test::Unit::TestCase
|
|
127
148
|
assert_equal('1', f.to_s(true))
|
128
149
|
assert_equal('1', f.to_s([true]))
|
129
150
|
assert_equal('1,0', f.to_s([true,false]))
|
151
|
+
assert_equal('0,0,0,0,', f.to_s(['', 0, false, '0', nil]))
|
130
152
|
|
131
153
|
assert_equal(nil, f.to_type(nil))
|
132
154
|
assert_equal([], f.to_type(''))
|
@@ -137,7 +159,25 @@ class FormattersTest < Test::Unit::TestCase
|
|
137
159
|
# test boolean with values != true|false
|
138
160
|
assert_equal('1', f.to_s('true'))
|
139
161
|
assert_equal('1', f.to_s(555))
|
162
|
+
end
|
163
|
+
|
164
|
+
|
165
|
+
def test_strict_booleans_formatter
|
166
|
+
f = Formatters::StrictBooleansFormatter.new
|
167
|
+
assert_equal(nil, f.to_s(nil))
|
168
|
+
assert_equal('1', f.to_s(true))
|
169
|
+
assert_equal('1', f.to_s([true]))
|
170
|
+
assert_equal('1,0', f.to_s([true,false]))
|
171
|
+
assert_equal('1,1', f.to_s([1,0]))
|
172
|
+
|
173
|
+
assert_equal(nil, f.to_type(nil))
|
174
|
+
assert_equal([], f.to_type(''))
|
175
|
+
assert_equal([true], f.to_type('1'))
|
176
|
+
assert_equal([true, false], f.to_type('1,0'))
|
140
177
|
|
141
178
|
|
179
|
+
# test boolean with values != true|false
|
180
|
+
assert_equal('1', f.to_s('true'))
|
181
|
+
assert_equal('1', f.to_s(555))
|
142
182
|
end
|
143
183
|
end
|
@@ -8,18 +8,18 @@ class HasSettingTest < Test::Unit::TestCase
|
|
8
8
|
end
|
9
9
|
|
10
10
|
|
11
|
-
def
|
11
|
+
def test_setting_has_accessors
|
12
12
|
assert @foo.respond_to?(:setting_1)
|
13
13
|
assert @foo.respond_to?(:setting_1=)
|
14
14
|
end
|
15
15
|
|
16
16
|
|
17
|
-
def
|
17
|
+
def test_has_many
|
18
18
|
assert @foo.respond_to?(:settings)
|
19
19
|
assert @foo.settings.is_a?(Array)
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
22
|
+
def test_settings_are_destroyed
|
23
23
|
count_before = HasSetting::Setting.count
|
24
24
|
@foo.setting_1 = 10
|
25
25
|
@foo.save!
|
@@ -29,7 +29,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
29
29
|
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
32
|
+
def test_write_setting
|
33
33
|
count_before = HasSetting::Setting.count
|
34
34
|
@foo.write_setting('name', 'value1')
|
35
35
|
@foo.save!
|
@@ -45,7 +45,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
45
45
|
assert_equal('value2', setting.value)
|
46
46
|
end
|
47
47
|
|
48
|
-
def
|
48
|
+
def test_setting_accessors
|
49
49
|
count_before = HasSetting::Setting.count
|
50
50
|
assert(!@foo.setting_1)
|
51
51
|
@foo.setting_1 = 'bli'
|
@@ -56,7 +56,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
56
56
|
assert_equal('bla', @foo.setting_1)
|
57
57
|
end
|
58
58
|
|
59
|
-
def
|
59
|
+
def test_different_classes_do_not_share_setting
|
60
60
|
count_before = HasSetting::Setting.count
|
61
61
|
@foo.setting_1 = 'foo'
|
62
62
|
@foo.save!
|
@@ -69,13 +69,13 @@ class HasSettingTest < Test::Unit::TestCase
|
|
69
69
|
end
|
70
70
|
|
71
71
|
|
72
|
-
def
|
72
|
+
def test_has_nil_setting
|
73
73
|
@foo.setting_1 = nil
|
74
74
|
assert(@foo.read_setting('setting_1'))
|
75
75
|
assert(!@foo.setting_1)
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
78
|
+
def test_options_on_getter
|
79
79
|
@foo.setting_1 = '12.3'
|
80
80
|
assert_equal('12.3', @foo.setting_1)
|
81
81
|
assert_equal(12, @foo.setting_1(:type => :int))
|
@@ -86,21 +86,21 @@ class HasSettingTest < Test::Unit::TestCase
|
|
86
86
|
assert_equal('12.3', @foo.setting_2(:type => :string))
|
87
87
|
end
|
88
88
|
|
89
|
-
def
|
89
|
+
def test_different_classes_do_not_share_options()
|
90
90
|
@foo.setting_2 = 12.3
|
91
91
|
assert_equal(12.3, @foo.setting_2)
|
92
92
|
@bar.setting_2 = 12.3
|
93
93
|
assert_equal(12, @bar.setting_2)
|
94
94
|
end
|
95
95
|
|
96
|
-
def
|
96
|
+
def test_default_values()
|
97
97
|
assert_equal('def', @foo.with_default)
|
98
98
|
assert_equal('override def', @foo.with_default(:default => 'override def'))
|
99
99
|
@foo.with_default = 'not def'
|
100
100
|
assert_equal('not def', @foo.with_default)
|
101
101
|
end
|
102
102
|
|
103
|
-
def
|
103
|
+
def test_write_settings_without_saved_parent
|
104
104
|
my_foo = Foo.new
|
105
105
|
count_before = HasSetting::Setting.count
|
106
106
|
my_foo.with_default = 'radabumm'
|
@@ -111,24 +111,37 @@ class HasSettingTest < Test::Unit::TestCase
|
|
111
111
|
assert_equal('radabumm', my_foo.with_default)
|
112
112
|
end
|
113
113
|
|
114
|
-
def
|
114
|
+
def test_not_everyone_has_settings_association
|
115
115
|
assert_equal(true, @foo.respond_to?(:settings))
|
116
116
|
assert_equal(true, @bar.respond_to?(:settings))
|
117
117
|
assert_equal(false, @baz.respond_to?(:settings))
|
118
118
|
end
|
119
119
|
|
120
120
|
|
121
|
-
def
|
121
|
+
def test_boolean_setting_without_default
|
122
122
|
assert_equal nil, @foo.setting_3
|
123
123
|
@foo.setting_3 = true
|
124
124
|
@foo.save!
|
125
|
-
|
125
|
+
|
126
|
+
@foo = Foo.find @foo.id
|
127
|
+
assert_equal true, @foo.setting_3
|
126
128
|
end
|
127
129
|
def test_boolean_setting_with_default
|
128
130
|
assert_equal false, @foo.setting_4
|
129
131
|
@foo.setting_4 = true
|
130
132
|
@foo.save!
|
131
|
-
|
133
|
+
@foo = Foo.find @foo.id
|
134
|
+
assert_equal true, @foo.setting_4
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_boolean_setting_with_default_and_no_saving
|
138
|
+
assert_equal false, @foo.setting_4
|
139
|
+
@foo.setting_4 = true
|
140
|
+
assert_equal true, @foo.setting_4
|
141
|
+
@foo.setting_4 = nil
|
142
|
+
assert_equal nil, @foo.setting_4
|
143
|
+
@foo.setting_4 = false
|
144
|
+
assert_equal false, @foo.setting_4
|
132
145
|
end
|
133
146
|
|
134
147
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplificator-has_setting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simplificator GmbH
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-02 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -40,7 +40,6 @@ files:
|
|
40
40
|
- test/unit/has_setting_test.rb
|
41
41
|
has_rdoc: true
|
42
42
|
homepage: http://github.com/simplificator/has_setting
|
43
|
-
licenses:
|
44
43
|
post_install_message:
|
45
44
|
rdoc_options:
|
46
45
|
- --charset=UTF-8
|
@@ -61,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
60
|
requirements: []
|
62
61
|
|
63
62
|
rubyforge_project:
|
64
|
-
rubygems_version: 1.
|
63
|
+
rubygems_version: 1.2.0
|
65
64
|
signing_key:
|
66
65
|
specification_version: 3
|
67
66
|
summary: simple setting extension to AR
|