setting_accessors 0.0.7 → 0.0.8

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
2
  SHA1:
3
- metadata.gz: 0b94565258ae7fce43922281d6b76f20d300c623
4
- data.tar.gz: 20a552250f28acc7de4709ad9aa0bdd2d9686ab4
3
+ metadata.gz: 69528301e4ae518a751b5e58de3a286a5ae19eb8
4
+ data.tar.gz: 77578f0cf78c80f461ff55053fcc998df702cc5b
5
5
  SHA512:
6
- metadata.gz: 94f79c3276a678e92ea28b2b557b98ab3d926cb04bf14033359cd7a91ad7038db756fd452d542ba53597b883deba0ad545484a396fd98813a2392de06dd3d776
7
- data.tar.gz: eaa0a4c1a96dd730525dd753e6347fb28d06d31bca26dcfebbfb6619dc60af5b77c83dcae2f85d7a22b0542425c9b6cff075df79f28db46bb440ddd40a12b133
6
+ metadata.gz: d8160afa3694ad0da94ab265f8b43b61a37aac05eb543dcca2fd4586cab6efabfb2241d375105c9c3a2a98d03092b0e919b37fcd9f7d6909571d4cf5e19d26c0
7
+ data.tar.gz: 3e56a6f1b6a989f165b7584df75acb4906be2bf9948403ca2d21e0a369945f2d78b32e1baa989e9ef4c4481d62826e043855cadb52b896db32d41b3e69a662a4
@@ -16,6 +16,18 @@ class SettingAccessors::Accessor
16
16
  has_key?(key) ? @temp_settings[key.to_sym] : SettingAccessors.setting_class.get(key, @record)
17
17
  end
18
18
 
19
+ #
20
+ # Tries to fetch a setting value using the provided key and #[].
21
+ # It will only return the +default+ value if there is
22
+ # - no temporary setting with the given key AND
23
+ # - no already persisted setting (see #[])
24
+ #
25
+ def fetch(key, default = nil)
26
+ result = self[key]
27
+ return default if result.nil? && !has_key?(key)
28
+ result
29
+ end
30
+
19
31
  def has_key?(key)
20
32
  @temp_settings.has_key?(key.to_sym)
21
33
  end
@@ -34,7 +46,7 @@ class SettingAccessors::Accessor
34
46
  # specified in the setting config file.
35
47
  #
36
48
  def get_or_default(key)
37
- self[key] || SettingAccessors.setting_class.get_or_default(key, @record)
49
+ fetch key, SettingAccessors.setting_class.get_or_default(key, @record)
38
50
  end
39
51
 
40
52
  #
@@ -42,7 +54,7 @@ class SettingAccessors::Accessor
42
54
  # If none is found, tries to find a global setting with the same name
43
55
  #
44
56
  def get_or_global(key)
45
- self[key] || SettingAccessors.setting_class.get(key)
57
+ fetch key, SettingAccessors.setting_class.get(key)
46
58
  end
47
59
 
48
60
  #
@@ -50,7 +62,7 @@ class SettingAccessors::Accessor
50
62
  # if none is found, it will return the given value instead.
51
63
  #
52
64
  def get_or_value(key, value)
53
- self.has_key?(key) ? self[key] : value
65
+ fetch key, value
54
66
  end
55
67
 
56
68
  def get_with_fallback(key, fallback = nil)
@@ -1,3 +1,3 @@
1
1
  module SettingAccessors
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
@@ -8,6 +8,8 @@ class User < ActiveRecord::Base
8
8
 
9
9
  setting_accessor :a_boolean, :fallback => false
10
10
 
11
+ setting_accessor :class_wise_truthy_boolean, :type => :boolean, :default => true, :fallback => :default
12
+
11
13
  setting_accessor :class_wise_with_value_fallback, :type => :string, :fallback => 'Oiski Poiski!'
12
14
 
13
15
  setting_accessor :class_wise_with_default_fallback, :type => :string, :fallback => :default, :default => 'Kapitanski'
@@ -128,4 +128,16 @@ class SettingTest < ActiveSupport::TestCase
128
128
  assert_equal 'Kapitanski', User.new.class_wise_with_default_fallback
129
129
  end
130
130
  end
131
+
132
+ context 'Boolean settings' do
133
+ should 'respect temporary settings which are set to `false`' do
134
+ user = User.new(:class_wise_truthy_boolean => false)
135
+ assert_equal user.class_wise_truthy_boolean, false
136
+ end
137
+
138
+ should 'respect temporary settings which are set to `true`' do
139
+ user = User.new(:a_boolean => true)
140
+ assert_equal user.a_boolean, true
141
+ end
142
+ end
131
143
  end
@@ -17,6 +17,5 @@ class UserTest < ActiveSupport::TestCase
17
17
  assert_equal @user.as_json[setting_name.to_s], @user.send(setting_name)
18
18
  end
19
19
  end
20
-
21
20
  end
22
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setting_accessors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Exner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler