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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2b7dd1f57f85031a2c3078a402651786ecc07900
4
- data.tar.gz: d712740b678c37bc6f724cd5fc2374f5e19c0110
2
+ SHA256:
3
+ metadata.gz: d0f0a651598e3e614da58374c217080580ef91c8a9cd7b29f0a738a917af66cc
4
+ data.tar.gz: aa4433f3838bc991d7926be8ed61179d09d79e7552a21d5a909ec2e8495de761
5
5
  SHA512:
6
- metadata.gz: 7cfe4aeb31062a4ab5ced166ebaaf6a1e53d0a26c7e98ed263ee023dc8cb52b2a0c7f4ff99d9e52692144bc465a37c3237b5fffa8668db44640a9620adb31e8a
7
- data.tar.gz: 5ebe5930b8eb9db4fc2ac4c0661446a75e9fcda63c8f7a6fb12b76390299d7fc0135ef06d65b9988f149aeaf902c08834f061012c8543b947359a243690947ef
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
@@ -26,4 +26,4 @@ Gem::Specification.new do |gem|
26
26
  "test/unit/has_setting_test.rb"
27
27
  ]
28
28
 
29
- end
29
+ end
@@ -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
- return options[:default] if setting.nil?
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.base_class != ActiveRecord::Base and
92
- klass.base_class.respond_to?(:has_setting_options)
93
- klass = klass.base_class
94
- option = klass.has_setting_options[name]
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
@@ -1,3 +1,3 @@
1
1
  module HasSetting
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.4"
3
3
  end
data/test/foo.rb CHANGED
@@ -5,4 +5,5 @@ class Foo < ActiveRecord::Base
5
5
 
6
6
  has_setting(:setting_3, :type => :boolean)
7
7
  has_setting(:setting_4, :type => :boolean, :default => false)
8
- end
8
+ has_setting(:setting_5, :type => :boolean, :default => -> { is_a?(Foo) })
9
+ end
data/test/test_helper.rb CHANGED
@@ -4,6 +4,7 @@ require 'test/unit'
4
4
 
5
5
  require File.dirname(__FILE__) + '/../lib/has_setting'
6
6
 
7
+ I18n.available_locales = %w(en de de-CH)
7
8
 
8
9
  ActiveRecord::Base.establish_connection(
9
10
  :adapter => 'sqlite3',
@@ -1,4 +1,5 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require_relative '../test_helper'
2
+
2
3
  include HasSetting
3
4
  class FormattersTest < Test::Unit::TestCase
4
5
  def test_for_type
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
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.1
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: 2017-05-15 00:00:00.000000000 Z
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
- rubyforge_project:
61
- rubygems_version: 2.5.1
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: