has_setting 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 343a6665b03e21db5cd725cd2b546bf2bf8864ba
4
- data.tar.gz: 5cab867d80e1c46c3865aa5b30044fdd9c8bc699
2
+ SHA256:
3
+ metadata.gz: d0f0a651598e3e614da58374c217080580ef91c8a9cd7b29f0a738a917af66cc
4
+ data.tar.gz: aa4433f3838bc991d7926be8ed61179d09d79e7552a21d5a909ec2e8495de761
5
5
  SHA512:
6
- metadata.gz: f55178fd52dfabf260aac58fef99150bc003ec465adf9c20a877c3095b5a219b7ae5e15c87c2c1323b731959c1c456d850bbf457edbf6b731f376b57f657b32b
7
- data.tar.gz: 642cd2d9ee5760ccfba79259e69a9f8c3c69f9ecb66d00e04410fd4d00c0c0e87cbbfd963a4d2530402afc11dfcaa4c33e9b1b7e6e2816b8f4d915a9432149fd
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
@@ -1,3 +1,3 @@
1
1
  module HasSetting
2
- VERSION = "1.0.3"
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.3
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-17 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.6.12
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: