ar-settings 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -7
- data/lib/settings.rb +14 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 514beb60ab432643297491c91545d87e947d8511
|
4
|
+
data.tar.gz: a46e908a7042f8e06cc808a3e5350a6904a08917
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eab67515991b4b247af548b9deb81aaca83356b7b03f3573f6b162ffde144f4658bdaed92240854b78de1d3931d8bcea5788b99d78a3df334ed7f42351933a81
|
7
|
+
data.tar.gz: 680617188cca46bd1f943527578f90cf2d6cbddd4a2b29023cad80d47df5729b6a0b3a963fcfe255288acde91fbc0bd3c951d33988be9fbae119354ac6c7d0c6
|
data/README.md
CHANGED
@@ -18,18 +18,40 @@ $ rails generate install_settings
|
|
18
18
|
```
|
19
19
|
|
20
20
|
## Usage
|
21
|
-
Gem implements minimal amount of public methods needed to manage settings
|
21
|
+
Gem implements minimal amount of public methods needed to manage settings
|
22
|
+
|
23
|
+
### Storing and fetching settings
|
24
|
+
|
25
|
+
```rb
|
26
|
+
Settings.set(:key, 'value') # store a value
|
27
|
+
Settings.get(:key) # fetch a value
|
28
|
+
```
|
29
|
+
|
30
|
+
```rb
|
31
|
+
Settings.key = 'value' # equivalent to Settings.set(...)
|
32
|
+
Settings.key # equivalent to Settings.get(...)
|
33
|
+
```
|
34
|
+
|
35
|
+
### Mass update
|
36
|
+
|
37
|
+
Method convenient for handling settings form submission.
|
22
38
|
```rb
|
23
|
-
Settings.
|
24
|
-
|
39
|
+
Settings.update({
|
40
|
+
key: 'value',
|
41
|
+
key2: 'value2',
|
42
|
+
...
|
43
|
+
})
|
44
|
+
```
|
25
45
|
|
26
|
-
|
27
|
-
|
46
|
+
### Additional methods
|
47
|
+
```rb
|
48
|
+
Settings.has(:key) # check if setting field exists
|
49
|
+
Settings.unset(:key) # remove stored value if exists
|
28
50
|
```
|
29
51
|
|
52
|
+
|
30
53
|
## To do
|
31
|
-
1.
|
32
|
-
2. Utilize `Rails.cache` to reduce overhead of querying db
|
54
|
+
1. Utilize `Rails.cache` to reduce overhead of querying db
|
33
55
|
|
34
56
|
## Licence
|
35
57
|
Licensed under the MIT license.
|
data/lib/settings.rb
CHANGED
@@ -17,4 +17,18 @@ class Settings < ActiveRecord::Base
|
|
17
17
|
def self.unset(key)
|
18
18
|
find_by(key: key)&.destroy
|
19
19
|
end
|
20
|
+
|
21
|
+
def self.update(fields)
|
22
|
+
fields.each { |key, value| set(key, value) }
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.method_missing(method_name, *arguments)
|
28
|
+
if method_name =~ /=$/
|
29
|
+
set method_name.to_s.gsub('=', ''), arguments.first
|
30
|
+
else
|
31
|
+
get method_name
|
32
|
+
end
|
33
|
+
end
|
20
34
|
end
|