has_setting 1.0.1 → 1.0.4
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.
- checksums.yaml +5 -5
- data/README.md +4 -4
- data/has_setting.gemspec +1 -1
- data/lib/has_setting/ar_extensions.rb +15 -6
- data/lib/has_setting/version.rb +1 -1
- data/test/foo.rb +2 -1
- data/test/test_helper.rb +1 -0
- data/test/unit/formatters_test.rb +2 -1
- data/test/unit/has_setting_test.rb +7 -2
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d0f0a651598e3e614da58374c217080580ef91c8a9cd7b29f0a738a917af66cc
|
4
|
+
data.tar.gz: aa4433f3838bc991d7926be8ed61179d09d79e7552a21d5a909ec2e8495de761
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7867c928a56b1fbe8f6806bb48029ec382c8055768b1554caac1c351c02ada220549db7c8d310103d0222e7d9454e32fe4504e21577cc26ed1d197e4e36755d
|
7
|
+
data.tar.gz: 89e78c2c5217f37cb381a22c9f11cf0de7f4497d44183b1d01c798bb105b19a27356dc599fb4e5e4c2fe34b8cc503eef6ab2d4f7d4a21b903834478480250b45
|
data/README.md
CHANGED
@@ -41,9 +41,7 @@ This will create the following methods for you on the owner class:
|
|
41
41
|
* *:strings* Uses the StringsFormatter to convert from/to string[]
|
42
42
|
* *:booleans* Uses the BooleansFormatter to convert from/to boolean[]
|
43
43
|
* *:strict_booleans* Uses the BooleansFormatter to convert from/to boolean[]
|
44
|
-
* *:default* 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.
|
45
|
-
|
46
|
-
|
44
|
+
* *:default* 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. You can provide `Proc` as a default value. In this case it will be executed withing the object's context and the result will be returned as a default value.
|
47
45
|
|
48
46
|
# How it works
|
49
47
|
|
@@ -105,7 +103,9 @@ has_setting should stay as simple as possible... still some ideas are around:
|
|
105
103
|
* Add validation options
|
106
104
|
|
107
105
|
# History
|
108
|
-
|
106
|
+
|
107
|
+
* 1.0.4
|
108
|
+
* Added possibility to provide `Proc` as default value
|
109
109
|
* 0.5:
|
110
110
|
* Added basic locale awareness. If you update from a previous version, you need to add a locale column to the settings table.
|
111
111
|
* 0.4.3:
|
data/has_setting.gemspec
CHANGED
@@ -47,7 +47,15 @@ module HasSetting
|
|
47
47
|
define_method(name) do |*args|
|
48
48
|
setting = read_setting(name)
|
49
49
|
options = args.first || has_setting_option(name)
|
50
|
-
|
50
|
+
if setting.nil?
|
51
|
+
result =
|
52
|
+
if options[:default].is_a?(Proc)
|
53
|
+
instance_exec(&options[:default])
|
54
|
+
else
|
55
|
+
options[:default]
|
56
|
+
end
|
57
|
+
return result
|
58
|
+
end
|
51
59
|
formatter = Formatters.for_type(options[:type] || type)
|
52
60
|
formatter.to_type(setting.value)
|
53
61
|
end
|
@@ -88,12 +96,13 @@ module HasSetting
|
|
88
96
|
klass = self.class
|
89
97
|
option = klass.has_setting_options ? klass.has_setting_options[name] : nil
|
90
98
|
while option.nil? and
|
91
|
-
klass.
|
92
|
-
|
93
|
-
klass
|
94
|
-
|
99
|
+
klass.superclass != ActiveRecord::Base
|
100
|
+
klass = klass.superclass
|
101
|
+
if klass.respond_to?(:has_setting_options) and klass.has_setting_options
|
102
|
+
option = klass.has_setting_options[name]
|
103
|
+
end
|
95
104
|
end
|
96
105
|
option
|
97
106
|
end
|
98
107
|
|
99
|
-
end
|
108
|
+
end
|
data/lib/has_setting/version.rb
CHANGED
data/test/foo.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative '../test_helper'
|
2
2
|
|
3
3
|
class HasSettingTest < Test::Unit::TestCase
|
4
4
|
def setup
|
@@ -87,7 +87,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
87
87
|
assert_equal(12, @bar.setting_2)
|
88
88
|
end
|
89
89
|
|
90
|
-
def test_default_values
|
90
|
+
def test_default_values
|
91
91
|
assert_equal('def', @foo.with_default)
|
92
92
|
assert_equal('override def', @foo.with_default(:default => 'override def'))
|
93
93
|
@foo.with_default = 'not def'
|
@@ -120,6 +120,7 @@ class HasSettingTest < Test::Unit::TestCase
|
|
120
120
|
@foo = Foo.find @foo.id
|
121
121
|
assert_equal true, @foo.setting_3
|
122
122
|
end
|
123
|
+
|
123
124
|
def test_boolean_setting_with_default
|
124
125
|
assert_equal false, @foo.setting_4
|
125
126
|
@foo.setting_4 = true
|
@@ -138,6 +139,10 @@ class HasSettingTest < Test::Unit::TestCase
|
|
138
139
|
assert_equal false, @foo.setting_4
|
139
140
|
end
|
140
141
|
|
142
|
+
def test_proc_value_as_default_value
|
143
|
+
assert_equal @foo.setting_5, true
|
144
|
+
end
|
145
|
+
|
141
146
|
def test_locale_awareness
|
142
147
|
I18n.locale = :de
|
143
148
|
@bar.setting_1 = 'setting de'
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_setting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simplificator GmbH
|
8
8
|
- Nico Ritsche
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-04-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Stores settings as key/value pairs in a settings table and provides accessors
|
15
15
|
for them on the owning object
|
@@ -41,7 +41,7 @@ files:
|
|
41
41
|
homepage: http://github.com/ncri/has_setting
|
42
42
|
licenses: []
|
43
43
|
metadata: {}
|
44
|
-
post_install_message:
|
44
|
+
post_install_message:
|
45
45
|
rdoc_options:
|
46
46
|
- "--charset=UTF-8"
|
47
47
|
require_paths:
|
@@ -57,9 +57,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
57
|
- !ruby/object:Gem::Version
|
58
58
|
version: '0'
|
59
59
|
requirements: []
|
60
|
-
|
61
|
-
|
62
|
-
signing_key:
|
60
|
+
rubygems_version: 3.1.6
|
61
|
+
signing_key:
|
63
62
|
specification_version: 4
|
64
63
|
summary: Simple setting extension to AR
|
65
64
|
test_files:
|