toggleable 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +45 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +57 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +87 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/env.sample +2 -0
- data/lib/toggleable.rb +30 -0
- data/lib/toggleable/base.rb +73 -0
- data/lib/toggleable/configuration.rb +12 -0
- data/lib/toggleable/feature_toggler.rb +54 -0
- data/lib/toggleable/logger_abstract.rb +18 -0
- data/lib/toggleable/storage.rb +6 -0
- data/lib/toggleable/storage/abstract.rb +35 -0
- data/lib/toggleable/storage/memory_store.rb +44 -0
- data/lib/toggleable/storage/redis_store.rb +36 -0
- data/lib/toggleable/version.rb +5 -0
- data/toggleable.gemspec +34 -0
- metadata +207 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b6582cb5bf57ac079c042f34076f5c1e7b55356b
|
4
|
+
data.tar.gz: 5e34f8dfb293f5f28e95854f4450db297af0dac1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e5005b7ca2fcdb42fcbb8b2f1eaeabc8b4815fb669ca46f160c462c1aeb3c845564b548b4ba6b6f86276e87f0a4ebab2bca369e5096c8a77a35f891c3a1aae7d
|
7
|
+
data.tar.gz: 067608f70cca8c0bd25e3ca172cad91eaa6d45c65650eeef3d452e25361b64eab4cd8d80f3967afd55a4a4d53c86510849ac38957163ed19e825b9303cbdb679
|
@@ -0,0 +1,45 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
build:
|
4
|
+
docker:
|
5
|
+
- image: ruby:2.3.0
|
6
|
+
- image: redis:3.2-alpine
|
7
|
+
working_directory: ~/toggleable
|
8
|
+
|
9
|
+
steps:
|
10
|
+
- checkout
|
11
|
+
- restore_cache:
|
12
|
+
keys:
|
13
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
14
|
+
- v1-dependencies-
|
15
|
+
- run:
|
16
|
+
name: Installing gems
|
17
|
+
command: |
|
18
|
+
gem install bundler -v 1.14.6
|
19
|
+
- run:
|
20
|
+
name: install dependencies
|
21
|
+
command: |
|
22
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
23
|
+
- save_cache:
|
24
|
+
paths:
|
25
|
+
- ./vendor/bundle
|
26
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
27
|
+
- run:
|
28
|
+
name: run rubocop
|
29
|
+
command: |
|
30
|
+
bundle exec rake lint
|
31
|
+
- run:
|
32
|
+
name: run tests
|
33
|
+
command: |
|
34
|
+
mkdir /tmp/test-results
|
35
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
|
36
|
+
|
37
|
+
bundle exec rspec --format progress \
|
38
|
+
--format RspecJunitFormatter \
|
39
|
+
--out /tmp/test-results/rspec.xml \
|
40
|
+
$TEST_FILES
|
41
|
+
- store_test_results:
|
42
|
+
path: /tmp/test-results
|
43
|
+
- store_artifacts:
|
44
|
+
path: /tmp/test-results
|
45
|
+
destination: test-results
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Exclude:
|
4
|
+
- Rakefile
|
5
|
+
- Gemfile
|
6
|
+
- bin/*
|
7
|
+
- '*.gemspec'
|
8
|
+
- spec/**/*.rb
|
9
|
+
- vendor/**/*
|
10
|
+
|
11
|
+
Lint/HandleExceptions:
|
12
|
+
Enabled: true
|
13
|
+
|
14
|
+
Lint/UnusedMethodArgument:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Metrics/LineLength:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics/MethodLength:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Metrics/ClassLength:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Metrics/BlockLength:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Metrics/AbcSize:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Metrics/CyclomaticComplexity:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Metrics/PerceivedComplexity:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Layout/EmptyLinesAroundModuleBody:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Layout/EmptyLinesAroundClassBody:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Layout/EmptyLinesAroundBlockBody:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Layout/SpaceBeforeBlockBraces:
|
48
|
+
EnforcedStyle: no_space
|
49
|
+
|
50
|
+
Style/AsciiComments:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
Style/Semicolon:
|
54
|
+
AllowAsExpressionSeparator: true
|
55
|
+
|
56
|
+
Naming/FileName:
|
57
|
+
Enabled: false
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
toggleable (0.1.5)
|
5
|
+
activesupport (>= 4.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.0)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
ast (2.4.0)
|
16
|
+
codecov (0.1.10)
|
17
|
+
json
|
18
|
+
simplecov
|
19
|
+
url
|
20
|
+
concurrent-ruby (1.0.5)
|
21
|
+
diff-lcs (1.3)
|
22
|
+
docile (1.3.1)
|
23
|
+
dotenv (2.4.0)
|
24
|
+
i18n (1.0.1)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
jaro_winkler (1.5.1)
|
27
|
+
json (2.1.0)
|
28
|
+
minitest (5.11.3)
|
29
|
+
parallel (1.12.1)
|
30
|
+
parser (2.5.1.0)
|
31
|
+
ast (~> 2.4.0)
|
32
|
+
powerpack (0.1.2)
|
33
|
+
rainbow (3.0.0)
|
34
|
+
rake (10.5.0)
|
35
|
+
redis (3.3.5)
|
36
|
+
rspec (3.7.0)
|
37
|
+
rspec-core (~> 3.7.0)
|
38
|
+
rspec-expectations (~> 3.7.0)
|
39
|
+
rspec-mocks (~> 3.7.0)
|
40
|
+
rspec-core (3.7.1)
|
41
|
+
rspec-support (~> 3.7.0)
|
42
|
+
rspec-expectations (3.7.0)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.7.0)
|
45
|
+
rspec-mocks (3.7.0)
|
46
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
47
|
+
rspec-support (~> 3.7.0)
|
48
|
+
rspec-support (3.7.1)
|
49
|
+
rspec_junit_formatter (0.4.1)
|
50
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
51
|
+
rubocop (0.57.2)
|
52
|
+
jaro_winkler (~> 1.5.1)
|
53
|
+
parallel (~> 1.10)
|
54
|
+
parser (>= 2.5)
|
55
|
+
powerpack (~> 0.1)
|
56
|
+
rainbow (>= 2.2.2, < 4.0)
|
57
|
+
ruby-progressbar (~> 1.7)
|
58
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
59
|
+
ruby-progressbar (1.9.0)
|
60
|
+
simplecov (0.16.1)
|
61
|
+
docile (~> 1.1)
|
62
|
+
json (>= 1.8, < 3)
|
63
|
+
simplecov-html (~> 0.10.0)
|
64
|
+
simplecov-html (0.10.2)
|
65
|
+
thread_safe (0.3.6)
|
66
|
+
tzinfo (1.2.5)
|
67
|
+
thread_safe (~> 0.1)
|
68
|
+
unicode-display_width (1.4.0)
|
69
|
+
url (0.3.2)
|
70
|
+
|
71
|
+
PLATFORMS
|
72
|
+
ruby
|
73
|
+
|
74
|
+
DEPENDENCIES
|
75
|
+
bundler (~> 1.14)
|
76
|
+
codecov
|
77
|
+
dotenv (>= 2.4.0)
|
78
|
+
rake (~> 10.0)
|
79
|
+
redis (~> 3.0)
|
80
|
+
rspec (~> 3.0)
|
81
|
+
rspec_junit_formatter
|
82
|
+
rubocop
|
83
|
+
simplecov (>= 0.16.1)
|
84
|
+
toggleable!
|
85
|
+
|
86
|
+
BUNDLED WITH
|
87
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Budi Pangestu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# toggleable
|
2
|
+
[![CircleCI](https://circleci.com/gh/bukalapak/toggleable.svg?style=svg)](https://circleci.com/gh/bukalapak/toggleable)
|
3
|
+
[![codecov](https://codecov.io/gh/bukalapak/toggleable/branch/master/graph/badge.svg)](https://codecov.io/gh/bukalapak/toggleable)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/02e63a9f70c9c06ffed5/maintainability)](https://codeclimate.com/github/bukalapak/toggleable/maintainability)
|
5
|
+
|
6
|
+
Gem that provides toggle functionality.
|
7
|
+
|
8
|
+
## Getting started
|
9
|
+
|
10
|
+
Install with:
|
11
|
+
|
12
|
+
```
|
13
|
+
$ gem install toggleable
|
14
|
+
```
|
15
|
+
|
16
|
+
You should initialize the toggleable first:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require "toggleable"
|
20
|
+
|
21
|
+
Toggleable.configure do |t|
|
22
|
+
t.expiration_time = 3.minutes
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
You can pass the configurations for toggleable in the block above. Here is the configurable list:
|
27
|
+
|
28
|
+
* use_memoization : set `true` to use memoization, so it doesn't hit your storage often. Default: `false`
|
29
|
+
* expiration_time : Duration for memoization expiry. Default: `5 minutes`
|
30
|
+
* storage : Storage persistence to use, you should pass an object that responds to methods that specified in `Toggleable::StorageAbstract` class or use the provided implementation in `toggleable/storage/*.rb`. If not provided, it will use memory store as persistence. Default: `Toggleable::MemoryStore`
|
31
|
+
* namespace : Prefix namespace for your stored keys. Default: `toggleable`
|
32
|
+
* logger : Logger to use, you should pass an object that respond to methods that speciied in `Toggleable::LoggerAbstract` class. It will not log if none provided. Default: `none`
|
33
|
+
|
34
|
+
### Usage
|
35
|
+
|
36
|
+
You could include `Toggleable::Base` to a class to provide toggling ability for that class.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class SampleFeature
|
40
|
+
include Toggleable::Base
|
41
|
+
|
42
|
+
DESC = 'this class can now be toggled'.freeze
|
43
|
+
end
|
44
|
+
|
45
|
+
SampleFeature.active?
|
46
|
+
# => 'false'
|
47
|
+
|
48
|
+
SampleFeature.activate!
|
49
|
+
# => "true"
|
50
|
+
|
51
|
+
# supply an actor for logging purposes
|
52
|
+
SampleFeature.deactivate! actor: user.id
|
53
|
+
# => 'false'
|
54
|
+
```
|
55
|
+
|
56
|
+
### Managing Toggles
|
57
|
+
|
58
|
+
You could manage your toggles using `Toggleable::FeatureToggler` class.
|
59
|
+
|
60
|
+
```
|
61
|
+
# This will get all keys and its value
|
62
|
+
Toggleable::FeatureToggler.instance.available_features
|
63
|
+
# => {'key': 'true', 'other_key': 'false'}
|
64
|
+
|
65
|
+
Toggleable::FeatureToggler.mass_toggle!(mapping, actor: user.id)
|
66
|
+
# => 'true'
|
67
|
+
```
|
68
|
+
|
69
|
+
### Redis Store Implementation
|
70
|
+
|
71
|
+
Redis implementation is also provided, you only need to pass your redis instance when configuring.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
require 'redis'
|
75
|
+
|
76
|
+
redis = Redis.new
|
77
|
+
|
78
|
+
Toggleable.configure do |t|
|
79
|
+
t.storage = Toggleable::RedisStore.new(redis)
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
## Testing
|
84
|
+
|
85
|
+
This gem is tested against recent Ruby and ActiveSupport versions.
|
86
|
+
|
87
|
+
|
88
|
+
## Contributor
|
89
|
+
|
90
|
+
[BUKALAPAK](https://github.com/bukalapak/toggleable/graphs/contributors) TEAM
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Fork the project and send pull requests.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "toggleable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/env.sample
ADDED
data/lib/toggleable.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'toggleable/version'
|
4
|
+
require 'toggleable/configuration'
|
5
|
+
require 'toggleable/storage'
|
6
|
+
require 'toggleable/logger_abstract'
|
7
|
+
require 'toggleable/feature_toggler'
|
8
|
+
require 'toggleable/base'
|
9
|
+
|
10
|
+
# Toggleable is a gem for toggling purposes.
|
11
|
+
module Toggleable
|
12
|
+
class << self
|
13
|
+
attr_accessor :configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
module_function
|
17
|
+
|
18
|
+
def configuration
|
19
|
+
@configuration ||= Toggleable::Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def configure
|
23
|
+
yield(configuration) if block_given?
|
24
|
+
|
25
|
+
# set default configuration for storage and namespace if none was provided
|
26
|
+
configuration.storage ||= Toggleable::MemoryStore.new
|
27
|
+
configuration.namespace ||= 'toggleable'
|
28
|
+
configuration.expiration_time ||= 5.minutes
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/inflector'
|
5
|
+
require 'active_support/core_ext/numeric/time'
|
6
|
+
|
7
|
+
module Toggleable
|
8
|
+
# Toggleable::Base provides basic functionality for toggling into a class.
|
9
|
+
module Base
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
DEFAULT_VALUE = false
|
13
|
+
|
14
|
+
included do
|
15
|
+
Toggleable::FeatureToggler.instance.register(key)
|
16
|
+
end
|
17
|
+
|
18
|
+
# it will generate these methods into included class.
|
19
|
+
module ClassMethods
|
20
|
+
def active?
|
21
|
+
return to_bool(toggle_active.to_s) unless toggle_active.nil?
|
22
|
+
|
23
|
+
# Lazily register the key
|
24
|
+
Toggleable.configuration.storage.set_if_not_exist(key, DEFAULT_VALUE, namespace: Toggleable.configuration.namespace)
|
25
|
+
DEFAULT_VALUE
|
26
|
+
end
|
27
|
+
|
28
|
+
def activate!(actor: nil)
|
29
|
+
toggle_key(true, actor)
|
30
|
+
end
|
31
|
+
|
32
|
+
def deactivate!(actor: nil)
|
33
|
+
toggle_key(false, actor)
|
34
|
+
end
|
35
|
+
|
36
|
+
def key
|
37
|
+
@key ||= name.underscore
|
38
|
+
end
|
39
|
+
|
40
|
+
def description
|
41
|
+
name
|
42
|
+
end
|
43
|
+
|
44
|
+
# should we encourage proxy classes
|
45
|
+
def process
|
46
|
+
yield if active?
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def toggle_key(value, actor)
|
52
|
+
Toggleable.configuration.logger&.log(key: key, value: value, actor: actor)
|
53
|
+
Toggleable.configuration.storage.set(key, value, namespace: Toggleable.configuration.namespace)
|
54
|
+
end
|
55
|
+
|
56
|
+
def toggle_active
|
57
|
+
return @_toggle_active if defined?(@_toggle_active) && !read_expired? && Toggleable.configuration.use_memoization
|
58
|
+
@_last_read_at = Time.now.localtime
|
59
|
+
@_toggle_active = Toggleable.configuration.storage.get(key, namespace: Toggleable.configuration.namespace)
|
60
|
+
end
|
61
|
+
|
62
|
+
def read_expired?
|
63
|
+
@_last_read_at < Time.now.localtime - Toggleable.configuration.expiration_time
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_bool(value)
|
67
|
+
return true if value =~ /^(true|t|yes|y|1)$/i
|
68
|
+
return false if value.empty? || value =~ /^(false|f|no|n|0)$/i
|
69
|
+
raise ArgumentError, "invalid value for Boolean: \"#{value}\""
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Toggleable
|
4
|
+
# Toggleable::Configuration yields the configuration of toggleable.
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :expiration_time ## expiration time for memoization. default: 5 minutes
|
7
|
+
attr_accessor :storage ## storage used. default: memory store
|
8
|
+
attr_accessor :namespace ## required for prefixing the keys. default: `toggleable``
|
9
|
+
attr_accessor :logger ## optional, it will not log if not configured.
|
10
|
+
attr_accessor :use_memoization ## set true to use memoization. default: false
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'singleton'
|
4
|
+
|
5
|
+
module Toggleable
|
6
|
+
# Toggleable::FeatureToggler provides an instance to manage all toggleable keys.
|
7
|
+
class FeatureToggler
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
attr_reader :features
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@features = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def register(key)
|
17
|
+
features << key
|
18
|
+
end
|
19
|
+
|
20
|
+
def available_features(memoize: Toggleable.configuration.use_memoization)
|
21
|
+
available_features = memoize ? memoized_keys : keys
|
22
|
+
available_features.slice(*features)
|
23
|
+
end
|
24
|
+
|
25
|
+
def mass_toggle!(mapping, actor: nil)
|
26
|
+
log_changes(mapping, actor) if Toggleable.configuration.logger
|
27
|
+
Toggleable.configuration.storage.mass_set(mapping, namespace: Toggleable.configuration.namespace)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def keys
|
33
|
+
Toggleable.configuration.storage.get_all(namespace: Toggleable.configuration.namespace)
|
34
|
+
end
|
35
|
+
|
36
|
+
def memoized_keys
|
37
|
+
return @_memoized_keys if defined?(@_memoized_keys) && !read_expired?
|
38
|
+
@_last_read_at = Time.now.localtime
|
39
|
+
@_memoized_keys = Toggleable.configuration.storage.get_all(namespace: Toggleable.configuration.namespace)
|
40
|
+
end
|
41
|
+
|
42
|
+
def read_expired?
|
43
|
+
@_last_read_at < Time.now.localtime - Toggleable.configuration.expiration_time
|
44
|
+
end
|
45
|
+
|
46
|
+
def log_changes(mapping, actor)
|
47
|
+
previous_values = available_features
|
48
|
+
mapping.each do |key, val|
|
49
|
+
next if previous_values[key].to_s == val.to_s
|
50
|
+
Toggleable.configuration.logger.log(key: key, value: val, actor: actor)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Toggleable
|
4
|
+
# Toggleable::LoggerAbstract describes the interface class for logger.
|
5
|
+
class LoggerAbstract
|
6
|
+
# the redis you provide must implement these methods
|
7
|
+
|
8
|
+
def log(_key:, _value:, _actor:)
|
9
|
+
raise NotImplementedError, "You must implement #{method_name}"
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def method_name
|
15
|
+
caller_locations(1, 1)[0].label
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Toggleable
|
4
|
+
# Toggleable::StorageAbstract describes the interface class for storage.
|
5
|
+
class StorageAbstract
|
6
|
+
## the storage you provide must implement these methods
|
7
|
+
## namespace parameter is optional, only if you provide namespace configuration
|
8
|
+
|
9
|
+
def get(_key, _namespace: nil)
|
10
|
+
raise NotImplementedError, "You must implement #{method_name}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_all(_namespace: nil)
|
14
|
+
raise NotImplementedError, "You must implement #{method_name}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def set(_key, _value, _namespace: nil)
|
18
|
+
raise NotImplementedError, "You must implement #{method_name}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_if_not_exist(_key, _value, namespace: nil)
|
22
|
+
raise NotImplementedError, "You must implement #{method_name}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def mass_set(_mappings, namespace: nil)
|
26
|
+
raise NotImplementedError, "You must implement #{method_name}"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def method_name
|
32
|
+
caller_locations(1, 1)[0].label
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/cache'
|
4
|
+
|
5
|
+
module Toggleable
|
6
|
+
# Toggleable::selfAbstract describes the interface class for
|
7
|
+
class MemoryStore < ActiveSupport::Cache::MemoryStore
|
8
|
+
## the self you provide must implement these methods
|
9
|
+
## namespace parameter is optional, only if you provide namespace configuration
|
10
|
+
|
11
|
+
def get(key, namespace:)
|
12
|
+
read(key, namespace: namespace)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_all(namespace:)
|
16
|
+
read_multi(*keys, namespace: namespace)
|
17
|
+
end
|
18
|
+
|
19
|
+
def set(key, value, namespace:)
|
20
|
+
write(key, value, namespace: namespace)
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_if_not_exist(key, value, namespace:)
|
24
|
+
fetch(key, namespace: namespace) do
|
25
|
+
value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def mass_set(mappings, namespace:)
|
30
|
+
write_multi(mappings, namespace: namespace)
|
31
|
+
end
|
32
|
+
|
33
|
+
def keys
|
34
|
+
cache_keys = @data.keys
|
35
|
+
normalize_keys(cache_keys)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def normalize_keys(cache_keys)
|
41
|
+
cache_keys.map{ |k| k.sub("#{Toggleable.configuration.namespace}:", '') }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'toggleable/storage/abstract'
|
4
|
+
|
5
|
+
module Toggleable
|
6
|
+
# Toggleable::RedisStore is storage implementation using redis, you should specify namespace to use it.
|
7
|
+
# Also pass the redis instance used in when initializing.
|
8
|
+
class RedisStore < Toggleable::StorageAbstract
|
9
|
+
attr_accessor :storage
|
10
|
+
|
11
|
+
def initialize(redis_instance)
|
12
|
+
@storage = redis_instance
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(key, namespace:)
|
16
|
+
storage.hget(namespace, key)
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_all(namespace:)
|
20
|
+
storage.hgetall(namespace)
|
21
|
+
end
|
22
|
+
|
23
|
+
def set(key, value, namespace:)
|
24
|
+
storage.hset(namespace, key, value)
|
25
|
+
end
|
26
|
+
|
27
|
+
def set_if_not_exist(key, value, namespace:)
|
28
|
+
storage.hsetnx(namespace, key, value)
|
29
|
+
end
|
30
|
+
|
31
|
+
def mass_set(mappings, namespace:)
|
32
|
+
mappings = mappings.flatten if mappings.is_a? Hash
|
33
|
+
storage.hmset(namespace, mappings)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/toggleable.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "toggleable/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "toggleable"
|
8
|
+
spec.version = Toggleable::VERSION
|
9
|
+
spec.authors = ["bukalapak"]
|
10
|
+
spec.email = ["product@bukalapak.com"]
|
11
|
+
|
12
|
+
spec.summary = 'Toggleable gem provides basic toggle functionality'
|
13
|
+
spec.summary = %q{Toggleable gem provides basic toggle functionality}
|
14
|
+
spec.description = %q{Toggleable gem provides basic toggle functionality}
|
15
|
+
spec.homepage = 'https://github.com/bukalapak/toggleable'
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "activesupport", ">= 4.0.0"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "simplecov", ">= 0.16.1"
|
28
|
+
spec.add_development_dependency "redis", "~> 3.0"
|
29
|
+
spec.add_development_dependency "dotenv", ">= 2.4.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "rspec_junit_formatter"
|
32
|
+
spec.add_development_dependency "rubocop"
|
33
|
+
spec.add_development_dependency "codecov"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toggleable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- bukalapak
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.16.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.16.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: redis
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: dotenv
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.4.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.4.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec_junit_formatter
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
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
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: codecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Toggleable gem provides basic toggle functionality
|
154
|
+
email:
|
155
|
+
- product@bukalapak.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".circleci/config.yml"
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- Gemfile
|
165
|
+
- Gemfile.lock
|
166
|
+
- LICENSE.txt
|
167
|
+
- README.md
|
168
|
+
- Rakefile
|
169
|
+
- bin/console
|
170
|
+
- bin/setup
|
171
|
+
- env.sample
|
172
|
+
- lib/toggleable.rb
|
173
|
+
- lib/toggleable/base.rb
|
174
|
+
- lib/toggleable/configuration.rb
|
175
|
+
- lib/toggleable/feature_toggler.rb
|
176
|
+
- lib/toggleable/logger_abstract.rb
|
177
|
+
- lib/toggleable/storage.rb
|
178
|
+
- lib/toggleable/storage/abstract.rb
|
179
|
+
- lib/toggleable/storage/memory_store.rb
|
180
|
+
- lib/toggleable/storage/redis_store.rb
|
181
|
+
- lib/toggleable/version.rb
|
182
|
+
- toggleable.gemspec
|
183
|
+
homepage: https://github.com/bukalapak/toggleable
|
184
|
+
licenses:
|
185
|
+
- MIT
|
186
|
+
metadata: {}
|
187
|
+
post_install_message:
|
188
|
+
rdoc_options: []
|
189
|
+
require_paths:
|
190
|
+
- lib
|
191
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
192
|
+
requirements:
|
193
|
+
- - ">="
|
194
|
+
- !ruby/object:Gem::Version
|
195
|
+
version: '0'
|
196
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
|
+
requirements:
|
198
|
+
- - ">="
|
199
|
+
- !ruby/object:Gem::Version
|
200
|
+
version: '0'
|
201
|
+
requirements: []
|
202
|
+
rubyforge_project:
|
203
|
+
rubygems_version: 2.5.1
|
204
|
+
signing_key:
|
205
|
+
specification_version: 4
|
206
|
+
summary: Toggleable gem provides basic toggle functionality
|
207
|
+
test_files: []
|