feature_setting 1.5.4 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +20 -3
- data/feature_setting.gemspec +1 -2
- data/lib/feature_setting/orm/active_record/fs_feature.rb +35 -10
- data/lib/feature_setting/orm/active_record/fs_setting.rb +44 -23
- data/lib/feature_setting/version.rb +1 -1
- data/lib/feature_setting.rb +1 -0
- data/lib/generators/feature_setting/install/templates/migrations/create_fs_tables.rb +6 -1
- metadata +3 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4fa65d93cd0a275f8b1ce550a644d77e6f6f0c4
|
4
|
+
data.tar.gz: 74a5eee2cc3f170f8e8654d9d0b81e6f0b51b4bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83cb8a1d0164a2f0f13987cb8da5f7b5ef88d39917e83d3cd8d2235668d7c608bcc06e06f7ed4ebba17e591dd72e31ff2c637c63c366db3c7fab839d56575aea
|
7
|
+
data.tar.gz: 24feeeb3faea28ae6ecc6f3ea56f61bba1615586ed0596f3c0f72026a24902fad5abb62f848cd84923b1fe90c469c63ed592444d3519e7a2c4158498c65ce1ba
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -114,9 +114,18 @@ Settings.setting_one = 2000
|
|
114
114
|
Settings.setting_one # => 2000
|
115
115
|
|
116
116
|
# other ways to set setting values:
|
117
|
-
Settings.
|
118
|
-
Settings.
|
119
|
-
Settings.
|
117
|
+
Settings.update!(setting_one: 1000)
|
118
|
+
Settings.update!(:setting_one, 1000)
|
119
|
+
Settings.update!('setting_one', 1000)
|
120
|
+
```
|
121
|
+
|
122
|
+
**NEW IN VERSION 1.6:** Hashes values can be updated individually and will not overwrite the entire hash:
|
123
|
+
```ruby
|
124
|
+
Settings.setting_five = { key1: 'another_value' }
|
125
|
+
=> setting_five: { key1: 'another_value', key2: 'value2' }
|
126
|
+
|
127
|
+
Settings.setting_five = { key3: 'value3' }
|
128
|
+
=> setting_five: { key1: 'another_value', key2: 'value2', key3: 'value3' }
|
120
129
|
```
|
121
130
|
|
122
131
|
Default values for settings are defined in your class and current values are persisted in the database.
|
@@ -149,6 +158,14 @@ Features.defined_features
|
|
149
158
|
Settings.defined_settings
|
150
159
|
```
|
151
160
|
|
161
|
+
Cache settings or features:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
Features.cache_features!
|
165
|
+
Settings.cache_settings!
|
166
|
+
```
|
167
|
+
Note that a simple call to `Features.init_features!` or `Settings.init_settings!` respectively will remove caching.
|
168
|
+
|
152
169
|
You can create as many Setting or Feature classes as you desire. Here are some examples:
|
153
170
|
|
154
171
|
```ruby
|
data/feature_setting.gemspec
CHANGED
@@ -24,6 +24,5 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency 'rake', '>= 10.0'
|
25
25
|
spec.add_development_dependency 'rspec', '>= 3.0'
|
26
26
|
spec.add_development_dependency 'sqlite3'
|
27
|
-
spec.add_development_dependency 'byebug'
|
28
|
-
spec.add_development_dependency 'codeclimate-test-reporter', '>= 0'
|
27
|
+
spec.add_development_dependency 'byebug'
|
29
28
|
end
|
@@ -34,20 +34,45 @@ module FeatureSetting
|
|
34
34
|
def init_features!(remove_old_features = false)
|
35
35
|
features.each do |key, value|
|
36
36
|
self.create_with(key: key, enabled: value, klass: klass).find_or_create_by(klass: klass, key: key)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
define_singleton_method("enable_#{key}!") do
|
42
|
-
enable!(key)
|
43
|
-
end
|
44
|
-
define_singleton_method("disable_#{key}!") do
|
45
|
-
disable!(key)
|
46
|
-
end
|
37
|
+
define_checker_method(key)
|
38
|
+
define_enabler_method(key)
|
39
|
+
define_disabler_method(key)
|
47
40
|
end
|
48
41
|
remove_old_features! if remove_old_features
|
49
42
|
end
|
50
43
|
|
44
|
+
def cache_features!
|
45
|
+
features.each do |key, value|
|
46
|
+
self.create_with(key: key, enabled: value, klass: klass).find_or_create_by(klass: klass, key: key)
|
47
|
+
value = self.where(key: key, klass: klass).first.enabled
|
48
|
+
define_checker_method(key) { value }
|
49
|
+
define_enabler_method(key) { false }
|
50
|
+
define_disabler_method(key) { false }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def define_checker_method(key, &block)
|
55
|
+
block = Proc.new do
|
56
|
+
record = self.where(key: key, klass: klass).first
|
57
|
+
record.enabled
|
58
|
+
end unless block_given?
|
59
|
+
define_singleton_method("#{key}_enabled?") { block.call }
|
60
|
+
end
|
61
|
+
|
62
|
+
def define_enabler_method(key, &block)
|
63
|
+
block = Proc.new do
|
64
|
+
enable!(key)
|
65
|
+
end unless block_given?
|
66
|
+
define_singleton_method("enable_#{key}!") { block.call }
|
67
|
+
end
|
68
|
+
|
69
|
+
def define_disabler_method(key, &block)
|
70
|
+
block = Proc.new do
|
71
|
+
disable!(key)
|
72
|
+
end unless block_given?
|
73
|
+
define_singleton_method("disable_#{key}!") { block.call }
|
74
|
+
end
|
75
|
+
|
51
76
|
def remove_old_features!
|
52
77
|
self.where(key: all_stored_features - defined_features).destroy_all
|
53
78
|
end
|
@@ -4,8 +4,8 @@ require 'json'
|
|
4
4
|
module FeatureSetting
|
5
5
|
class FsSetting < ActiveRecord::Base
|
6
6
|
SETTINGS = {
|
7
|
-
test: 'value'
|
8
|
-
}
|
7
|
+
test: 'value'
|
8
|
+
}.freeze
|
9
9
|
|
10
10
|
def settings
|
11
11
|
self.class::SETTINGS
|
@@ -17,56 +17,77 @@ module FeatureSetting
|
|
17
17
|
|
18
18
|
class << self
|
19
19
|
def settings
|
20
|
-
|
20
|
+
new.settings
|
21
21
|
end
|
22
22
|
|
23
23
|
def klass
|
24
|
-
|
24
|
+
new.klass.to_s
|
25
25
|
end
|
26
26
|
|
27
27
|
def init_settings!(remove_old_settings = false)
|
28
28
|
settings.each do |key, value|
|
29
|
-
|
29
|
+
create_with(key: key, value: convert_to_string(value, value.class.to_s), value_type: value.class.to_s, klass: klass).find_or_create_by(klass: klass, key: key)
|
30
30
|
define_getter_method(key)
|
31
31
|
define_setter_method(key)
|
32
32
|
end
|
33
33
|
remove_old_settings! if remove_old_settings
|
34
34
|
end
|
35
35
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
def cache_settings!
|
37
|
+
settings.each do |key, value|
|
38
|
+
create_with(key: key, value: convert_to_string(value, value.class.to_s), value_type: value.class.to_s, klass: klass).find_or_create_by(klass: klass, key: key)
|
39
|
+
record = where(key: key, klass: klass).first
|
40
|
+
value = convert_to_type(record.value, record.value_type)
|
41
|
+
define_getter_method(key) { value }
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
45
|
+
def define_getter_method(key, &block)
|
46
|
+
block = Proc.new do
|
47
|
+
record = where(key: key, klass: klass).first
|
48
|
+
convert_to_type(record.value, record.value_type)
|
49
|
+
end unless block_given?
|
50
|
+
|
51
|
+
define_singleton_method(key.to_s) { block.call }
|
52
|
+
end
|
53
|
+
|
43
54
|
def define_setter_method(key)
|
44
55
|
define_singleton_method("#{key}=") do |value|
|
45
|
-
record = self.where(key: key, klass: klass).first
|
46
56
|
set!(key, value)
|
47
57
|
end
|
48
58
|
end
|
49
59
|
|
50
60
|
def remove_old_settings!
|
51
|
-
|
61
|
+
where(klass: klass, key: all_stored_keys - defined_keys).destroy_all
|
52
62
|
end
|
53
63
|
|
54
64
|
def reset_settings!
|
55
|
-
|
65
|
+
where(klass: klass).destroy_all
|
56
66
|
init_settings!
|
57
67
|
end
|
58
68
|
|
59
|
-
def set!(key = nil, value = nil
|
60
|
-
key
|
61
|
-
|
69
|
+
def set!(key = nil, value = nil)
|
70
|
+
raise 'ERROR: FsSetting key is missing or does not exist.' unless defined_keys.include?(key.to_s)
|
71
|
+
|
72
|
+
record = where(key: key.to_s, klass: klass).first
|
73
|
+
old_value = convert_to_type(record.value, record.value_type)
|
74
|
+
|
75
|
+
if record.value_type == 'Hash'
|
76
|
+
raise 'ERROR: The value for a setting of type Hash must be a Hash.' unless value.is_a?(Hash)
|
77
|
+
|
78
|
+
new_value = old_value.update(value)
|
79
|
+
value_type = 'Hash'
|
80
|
+
else
|
81
|
+
new_value = value
|
82
|
+
value_type = value.class.to_s
|
83
|
+
end
|
62
84
|
|
63
|
-
record = self.where(key: key, klass: klass).first
|
64
|
-
new_value = hash.values.first || value
|
65
85
|
record.update_attributes(
|
66
86
|
value: convert_to_string(new_value, new_value.class.to_s),
|
67
|
-
value_type:
|
87
|
+
value_type: value_type
|
68
88
|
)
|
69
89
|
end
|
90
|
+
alias_method :update!, :set!
|
70
91
|
|
71
92
|
def existing_key(key = nil, hash = {})
|
72
93
|
settings.key?(hash.keys.first) || settings.key?(key.to_sym)
|
@@ -81,7 +102,7 @@ module FeatureSetting
|
|
81
102
|
|
82
103
|
def stored_settings
|
83
104
|
hash = {}
|
84
|
-
|
105
|
+
where(klass: klass).each do |record|
|
85
106
|
hash[record.key.to_sym] = convert_to_type(record.value, record.value_type)
|
86
107
|
end
|
87
108
|
|
@@ -91,7 +112,7 @@ module FeatureSetting
|
|
91
112
|
private
|
92
113
|
|
93
114
|
def all_stored_keys
|
94
|
-
|
115
|
+
all.pluck(:key)
|
95
116
|
end
|
96
117
|
|
97
118
|
def convert_to_type(value, type)
|
@@ -106,6 +127,8 @@ module FeatureSetting
|
|
106
127
|
false
|
107
128
|
when 'Fixnum'
|
108
129
|
value.to_i
|
130
|
+
when 'Integer'
|
131
|
+
value.to_i
|
109
132
|
when 'Float'
|
110
133
|
value.to_f
|
111
134
|
when 'Symbol'
|
@@ -119,12 +142,10 @@ module FeatureSetting
|
|
119
142
|
|
120
143
|
def convert_to_string(value, type)
|
121
144
|
case type
|
122
|
-
when 'Hash'
|
145
|
+
when 'Hash', 'Hashie::Mash'
|
123
146
|
value.to_json
|
124
147
|
when 'Array'
|
125
148
|
value.join('|||')
|
126
|
-
when 'Hash'
|
127
|
-
value.to_json
|
128
149
|
else
|
129
150
|
value.to_s
|
130
151
|
end
|
data/lib/feature_setting.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class CreateFsTables < ActiveRecord::Migration
|
1
|
+
class CreateFsTables < ActiveRecord::Migration[4.2]
|
2
2
|
def self.up
|
3
3
|
create_table :fs_features do |t|
|
4
4
|
t.string :key
|
@@ -15,4 +15,9 @@ class CreateFsTables < ActiveRecord::Migration
|
|
15
15
|
t.timestamps null: false
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
drop_table :fs_features
|
21
|
+
drop_table :fs_settings
|
22
|
+
end
|
18
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: feature_setting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Indro De
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -122,20 +122,6 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: codeclimate-test-reporter
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - ">="
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '0'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - ">="
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '0'
|
139
125
|
description: This gem introduces the concept of "features" and "settings" to your
|
140
126
|
Rails app. It provides an easy way to define such features and settings with default
|
141
127
|
values right in your code and will persist them in the database.
|
@@ -182,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
182
168
|
version: '0'
|
183
169
|
requirements: []
|
184
170
|
rubyforge_project:
|
185
|
-
rubygems_version: 2.6.
|
171
|
+
rubygems_version: 2.6.11
|
186
172
|
signing_key:
|
187
173
|
specification_version: 4
|
188
174
|
summary: A lightweight feature/setting DSL for Rails applications.
|