settingson 1.3.4 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -1
- data/README.md +47 -19
- data/app/models/concerns/settingson/base.rb +54 -20
- data/lib/settingson/version.rb +1 -1
- data/spec/models/settings_spec.rb +25 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe05f0f361a5568a359fb45b6fa141efcc09b14a
|
4
|
+
data.tar.gz: 01c34a1af2329cc4eaa44f0384a7c508f4f643cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f970836ba3dc03f7f711a9cf21a5c855bf920d6a1b095bdaba08ff9d34a18ad0a2722d3b416426afdcbf2432ff6cd1a89f7f79cd8a7c6ff95875d9acbc50ebc1
|
7
|
+
data.tar.gz: 4d566c47608e3f53eb34ca609e023d4f870191322deb298bb47d41986ad96b0e169defa470d4505d39e221968f0d47a88da7abda924906f169fc141ffd618ee6
|
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -15,29 +15,56 @@ rake db:migrate
|
|
15
15
|
|
16
16
|
code:
|
17
17
|
```ruby
|
18
|
-
Settings.server.host = '127.0.0.1'
|
19
|
-
Settings.server.port = '8888'
|
18
|
+
Settings.server.host = '127.0.0.1' # => '127.0.0.1'
|
19
|
+
Settings.server.port = '8888' # => '8888'
|
20
20
|
|
21
|
-
Settings.server.host
|
22
|
-
Settings.server.port
|
21
|
+
Settings.server.host # => '127.0.0.1'
|
22
|
+
Settings.server.port # => '8888'
|
23
23
|
|
24
|
-
Settings.server.smtp.host = '127.0.0.1'
|
25
|
-
Settings.server.smtp.port = 25
|
24
|
+
Settings.server.smtp.host = '127.0.0.1' # => '127.0.0.1'
|
25
|
+
Settings.server.smtp.port = 25 # => 25
|
26
26
|
|
27
|
-
Settings.server.smtp.host
|
28
|
-
Settings.server.smtp.port
|
27
|
+
Settings.server.smtp.host # => "127.0.0.1"
|
28
|
+
Settings.server.smtp.port # => 25
|
29
29
|
|
30
30
|
Settings.from_hash({hello: :world})
|
31
|
-
Settings.hello
|
31
|
+
Settings.hello # => :world
|
32
32
|
|
33
|
-
Settings.not_found
|
34
|
-
Settings.not_found.nil?
|
35
|
-
Settings.not_found.empty?
|
36
|
-
Settings.not_found.blank?
|
37
|
-
Settings.not_found.present?
|
33
|
+
Settings.not_found # => ""
|
34
|
+
Settings.not_found.nil? # => true
|
35
|
+
Settings.not_found.empty? # => true
|
36
|
+
Settings.not_found.blank? # => true
|
37
|
+
Settings.not_found.present? # => false
|
38
38
|
|
39
39
|
# but
|
40
40
|
Settings.not_found.class # => Settings(id: integer, key: string, value: text, created_at: datetime, updated_at: datetime)
|
41
|
+
|
42
|
+
Settings.all # =>
|
43
|
+
# [#<Settings id: 1, key: "server.host", value: "127.0.0.1", created_at: "2015-12-08 15:17:56", updated_at: "2015-12-08 15:17:56">,
|
44
|
+
# #<Settings id: 2, key: "server.port", value: "8888", created_at: "2015-12-08 15:17:56", updated_at: "2015-12-08 15:17:56">,
|
45
|
+
# #<Settings id: 3, key: "server.smtp.host", value: "127.0.0.1", created_at: "2015-12-08 15:18:21", updated_at: "2015-12-08 15:18:21">,
|
46
|
+
# #<Settings id: 4, key: "server.smtp.port", value: 25, created_at: "2015-12-08 15:18:22", updated_at: "2015-12-08 15:18:22">,
|
47
|
+
# #<Settings id: 5, key: "hello", value: :world, created_at: "2015-12-08 15:18:32", updated_at: "2015-12-08 15:18:32">]
|
48
|
+
```
|
49
|
+
|
50
|
+
### Cached values
|
51
|
+
|
52
|
+
Caching implemented via ActiveSupport::Cache::Store [(read more)](http://guides.rubyonrails.org/caching_with_rails.html).
|
53
|
+
|
54
|
+
By default 10 seconds:
|
55
|
+
```ruby
|
56
|
+
Settings.hello.world = 'message' # => 'message'
|
57
|
+
Settings.cached.hello.world # => 'message' with asking DB
|
58
|
+
Settings.cached.hello.world # => 'message' without asking DB
|
59
|
+
sleep 11
|
60
|
+
Settings.cached.hello.world # => 'message' with asking DB
|
61
|
+
```
|
62
|
+
|
63
|
+
You can change time to Live:
|
64
|
+
```ruby
|
65
|
+
Settings.cached(30.minutes).hello.world # => 'message'
|
66
|
+
# same as
|
67
|
+
Settings.cached(1800).hello.world # => 'message'
|
41
68
|
```
|
42
69
|
|
43
70
|
### Using with [Simple Form](https://github.com/plataformatec/simple_form) and [Haml](https://github.com/haml/haml)
|
@@ -65,16 +92,17 @@ end
|
|
65
92
|
```
|
66
93
|
|
67
94
|
## The initial values
|
95
|
+
New way:
|
68
96
|
in config/initializers/settingson.rb
|
69
|
-
|
70
97
|
```ruby
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
98
|
+
Settings.defaults do
|
99
|
+
Settings.server.smtp.host? || Settings.server.smtp.host = 'host'
|
100
|
+
Settings.server.smtp.port? || Settings.server.smtp.port = 25
|
101
|
+
end
|
75
102
|
```
|
76
103
|
|
77
104
|
Old way:
|
105
|
+
in config/initializers/settingson.rb
|
78
106
|
```ruby
|
79
107
|
Rails.application.config.after_initialize do
|
80
108
|
begin
|
@@ -25,6 +25,37 @@ module Settingson::Base
|
|
25
25
|
|
26
26
|
alias empty? nil?
|
27
27
|
|
28
|
+
def _settingson_fresh_value
|
29
|
+
self.class.find_by(key: @settingson)
|
30
|
+
end
|
31
|
+
|
32
|
+
def _settingson_cached(expires_in:)
|
33
|
+
@_settingson_cached = expires_in
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def _settingson_cached_value
|
38
|
+
Rails.cache.fetch("settingson_cache/#{@settingson}", expires_in: @_settingson_cached) do
|
39
|
+
_settingson_fresh_value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def _settingson_value
|
44
|
+
if @_settingson_cached
|
45
|
+
_settingson_cached_value
|
46
|
+
else
|
47
|
+
_settingson_fresh_value
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def _settingson_find_or_create(key)
|
52
|
+
if @settingson.blank?
|
53
|
+
@settingson = key
|
54
|
+
else
|
55
|
+
@settingson += ".#{key}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
28
59
|
def method_missing(symbol, *args)
|
29
60
|
super
|
30
61
|
rescue NoMethodError
|
@@ -32,35 +63,33 @@ module Settingson::Base
|
|
32
63
|
case symbol.to_s
|
33
64
|
when /(.+)=/ # setter
|
34
65
|
|
35
|
-
|
36
|
-
@settingson = $1
|
37
|
-
else
|
38
|
-
@settingson += ".#{$1}"
|
39
|
-
end
|
66
|
+
_settingson_find_or_create($1)
|
40
67
|
|
41
|
-
if
|
68
|
+
if args.first.nil? and record = _settingson_fresh_value
|
42
69
|
record.destroy
|
43
|
-
elsif record
|
44
|
-
record.update(value: args.first)
|
70
|
+
elsif record = _settingson_fresh_value
|
71
|
+
record.update!(value: args.first)
|
45
72
|
else
|
46
73
|
self.class.create(key: @settingson, value: args.first)
|
47
74
|
end
|
48
75
|
|
76
|
+
Rails.cache.delete("settingson_cache/#{@settingson}")
|
77
|
+
|
49
78
|
when /(.+)\?$/ # returns boolean
|
50
|
-
|
51
|
-
|
79
|
+
|
80
|
+
_settingson_find_or_create($1)
|
81
|
+
_settingson_value.present?
|
82
|
+
|
52
83
|
when /(.+)\!$/ # returns self or nil
|
53
|
-
|
54
|
-
|
84
|
+
|
85
|
+
_settingson_find_or_create($1)
|
86
|
+
_settingson_value
|
87
|
+
|
55
88
|
else # returns values or self
|
56
89
|
|
57
|
-
|
58
|
-
@settingson = "#{symbol.to_s}"
|
59
|
-
else
|
60
|
-
@settingson += ".#{symbol.to_s}"
|
61
|
-
end
|
90
|
+
_settingson_find_or_create(symbol.to_s)
|
62
91
|
|
63
|
-
if record =
|
92
|
+
if record = _settingson_value
|
64
93
|
record.value
|
65
94
|
else
|
66
95
|
self
|
@@ -109,7 +138,9 @@ module Settingson::Base
|
|
109
138
|
elsif record
|
110
139
|
record.update(value: args.first)
|
111
140
|
else
|
112
|
-
create(key: @settingson,
|
141
|
+
self.create(key: @settingson,
|
142
|
+
value: args.first,
|
143
|
+
settingson: @settingson)
|
113
144
|
end
|
114
145
|
when /(.+)\?$/ # returns boolean
|
115
146
|
find_by(key: $1).present?
|
@@ -126,7 +157,10 @@ module Settingson::Base
|
|
126
157
|
end
|
127
158
|
end
|
128
159
|
|
160
|
+
def cached(expires_in = 10.seconds)
|
161
|
+
new._settingson_cached(expires_in: expires_in)
|
162
|
+
end
|
129
163
|
|
130
|
-
end
|
164
|
+
end # module ClassMethods
|
131
165
|
|
132
166
|
end
|
data/lib/settingson/version.rb
CHANGED
@@ -69,4 +69,29 @@ describe Settings do
|
|
69
69
|
expect( Settings.not_found.present? ).to be(false)
|
70
70
|
end
|
71
71
|
end
|
72
|
+
|
73
|
+
describe '::cached' do
|
74
|
+
it 'not raises error without params' do
|
75
|
+
expect{ Settings.cached }.to_not raise_error
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'not raises error with params' do
|
79
|
+
expect{ Settings.cached(Random.rand(10)) }.to_not raise_error
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'returns instance of self class' do
|
83
|
+
expect( Settings.cached ).to be_a(Settings)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'returns same value for complex key #1' do
|
87
|
+
word = Faker::Lorem.word
|
88
|
+
Settings.hello.hello = word
|
89
|
+
expect( Settings.cached.hello.hello ).to eq(word)
|
90
|
+
end
|
91
|
+
it 'returns same value for complex key #2' do
|
92
|
+
word = Faker::Lorem.word
|
93
|
+
Settings.i.hello = word
|
94
|
+
expect( Settings.cached.i.hello ).to eq(word)
|
95
|
+
end
|
96
|
+
end
|
72
97
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: settingson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|