flop 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +89 -0
- data/Rakefile +7 -0
- data/flop.gemspec +27 -0
- data/lib/flop.rb +19 -0
- data/lib/flop/feature.rb +43 -0
- data/lib/flop/repository/memory.rb +17 -0
- data/lib/flop/repository/redis.rb +28 -0
- data/lib/flop/version.rb +3 -0
- data/test/flop/feature_test.rb +56 -0
- data/test/flop/repository/generic_repo_example.rb +13 -0
- data/test/flop/repository/memory_test.rb +10 -0
- data/test/flop/repository/redis_test.rb +11 -0
- data/test/flop_test.rb +7 -0
- data/test/test_helper.rb +5 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1be972aa3ae27087ea067523e5c91ae74cbfd123
|
4
|
+
data.tar.gz: fe5db96c968e40888f2443c53e5f3a8d18b55390
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9853f11882c1edbe0e32dd802210fb411d1ff0bdc1198d43ffe51dcba250cf0e7a82878e4fbe4d79147628971e681d641fa8b3bb4a41f050f35946386102d1c
|
7
|
+
data.tar.gz: 7f439d941911c295240329035ce5704c91e31855dd017b5bc2bac67b8e6277b70ae526e54cf802537613b810c84d985221e4a2eeb31b8818c07bbd6f3f508342
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Guillaume DOTT
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Flop
|
2
|
+
|
3
|
+
Flop is a feature flipper gem for ruby.
|
4
|
+
|
5
|
+
Toggle features on and off easily and store feature states in memory or in redis
|
6
|
+
with the different repositories.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'flop'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install flop
|
21
|
+
|
22
|
+
## Repositories
|
23
|
+
|
24
|
+
First, you need to configure where to store everything by setting a repository.
|
25
|
+
|
26
|
+
Available repositories are :
|
27
|
+
- `Flop::Repository::Memory`
|
28
|
+
- `Flop::Repository::Redis`
|
29
|
+
|
30
|
+
To set the repository, create a new object and affect it to `Flop.repo`.
|
31
|
+
```ruby
|
32
|
+
Flop.repo = Flop::Repository::Memory.new
|
33
|
+
|
34
|
+
require 'redis'
|
35
|
+
|
36
|
+
Flop.repo = Flop::Repository::Redis.new(Redis.new)
|
37
|
+
```
|
38
|
+
|
39
|
+
### Redis
|
40
|
+
|
41
|
+
The `Redis` repository requires a `Redis` connection as its first parameter.
|
42
|
+
The default namespace can be passed as a second parameter.
|
43
|
+
|
44
|
+
The default namespace can be a string, an array or a proc.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
Flop::Repository::Redis.new(Redis.new, "flop:namespace")
|
48
|
+
Flop::Repository::Redis.new(Redis.new, [:flop, :namespace])
|
49
|
+
Flop::Repository::Redis.new(Redis.new, -> { ENV['NAMESPACE'] })
|
50
|
+
```
|
51
|
+
|
52
|
+
## Features
|
53
|
+
|
54
|
+
To access a feature, you can call the Flop[] method or create a new `Flop::Feature` object.
|
55
|
+
```ruby
|
56
|
+
Flop[:example_feature] # is the same as
|
57
|
+
Flop::Feature.new(:example_feature)
|
58
|
+
```
|
59
|
+
|
60
|
+
Available methods on `Flop::Feature` are :
|
61
|
+
- `feature.active?` returns true if the feature is active
|
62
|
+
- `feature.inactive?` returns true if the feature is inactive
|
63
|
+
- `feature.set(boolean)` sets the state of the feature to the passed value
|
64
|
+
- `feature.activate` activates the feature
|
65
|
+
- `feature.deactivate` deactivates the feature
|
66
|
+
- `feature.toggle` toggles the state of the feature
|
67
|
+
- `feature.with { }` executes the block if the feature is active
|
68
|
+
- `feature.without { }` executes the block if the feature is inactive
|
69
|
+
|
70
|
+
## Examples
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Flop.repo = Flop::Repository::Memory.new
|
74
|
+
|
75
|
+
Flop[:feature].active? # => false
|
76
|
+
Flop[:feature].toggle # => true
|
77
|
+
|
78
|
+
Flop[:feature].with do
|
79
|
+
# code
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
1. Fork it ( https://github.com/gdott9/flop/fork )
|
86
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
87
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
88
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
89
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/flop.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flop/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "flop"
|
8
|
+
spec.version = Flop::VERSION
|
9
|
+
spec.authors = ["Guillaume DOTT"]
|
10
|
+
spec.email = ["guillaume+github@dott.fr"]
|
11
|
+
spec.summary = %q{A simple ruby feature flipper}
|
12
|
+
spec.description = %q{Activate and deactivate features easily}
|
13
|
+
spec.homepage = 'https://github.com/gdott9/flop'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'minitest', '~> 5.4'
|
25
|
+
spec.add_development_dependency 'minitest-reporters', '~> 1.0'
|
26
|
+
spec.add_development_dependency 'fakeredis', '~> 0.5'
|
27
|
+
end
|
data/lib/flop.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'flop/version'
|
2
|
+
|
3
|
+
require 'flop/feature'
|
4
|
+
require 'flop/repository/memory'
|
5
|
+
require 'flop/repository/redis'
|
6
|
+
|
7
|
+
module Flop
|
8
|
+
class << self
|
9
|
+
attr_writer :repo
|
10
|
+
|
11
|
+
def repo
|
12
|
+
@repo ||= Flop::Repository::Memory.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](feature)
|
16
|
+
Flop::Feature.new(feature)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/flop/feature.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module Flop
|
2
|
+
class Feature
|
3
|
+
attr_accessor :name, :repo
|
4
|
+
|
5
|
+
def initialize(name, repo = Flop.repo)
|
6
|
+
self.name = name.to_sym
|
7
|
+
self.repo = repo
|
8
|
+
end
|
9
|
+
|
10
|
+
def active?
|
11
|
+
repo.get(name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def inactive?
|
15
|
+
!active?
|
16
|
+
end
|
17
|
+
|
18
|
+
def set(value)
|
19
|
+
repo.set(name, value)
|
20
|
+
value
|
21
|
+
end
|
22
|
+
|
23
|
+
def activate
|
24
|
+
set true
|
25
|
+
end
|
26
|
+
|
27
|
+
def deactivate
|
28
|
+
set false
|
29
|
+
end
|
30
|
+
|
31
|
+
def toggle
|
32
|
+
set !active?
|
33
|
+
end
|
34
|
+
|
35
|
+
def with(&block)
|
36
|
+
yield block if active?
|
37
|
+
end
|
38
|
+
|
39
|
+
def without(&block)
|
40
|
+
yield block if inactive?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Flop
|
2
|
+
module Repository
|
3
|
+
class Redis
|
4
|
+
def initialize(redis, namespace = '')
|
5
|
+
@redis = redis
|
6
|
+
@namespace = namespace
|
7
|
+
end
|
8
|
+
|
9
|
+
def get(name)
|
10
|
+
@redis.get(key(name)).to_i == 1
|
11
|
+
end
|
12
|
+
|
13
|
+
def set(name, value)
|
14
|
+
@redis.set(key(name), value ? 1 : 0)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def key(name)
|
20
|
+
[namespace, name].flatten.join(':')
|
21
|
+
end
|
22
|
+
|
23
|
+
def namespace
|
24
|
+
@namespace.is_a?(Proc) ? @namespace.call : @namespace
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/flop/version.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Flop::FeatureTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@feature = Flop::Feature.new(:test)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_it_is_inactive_by_default
|
9
|
+
assert Flop::Feature.new(:inactive_feature).inactive?
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_that_name_is_symbol
|
13
|
+
assert_equal :string, Flop::Feature.new('string').name
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_set_specified_value
|
17
|
+
@feature.set true
|
18
|
+
assert @feature.active?
|
19
|
+
|
20
|
+
@feature.set false
|
21
|
+
assert @feature.inactive?
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_toggle
|
25
|
+
@feature.deactivate
|
26
|
+
|
27
|
+
@feature.toggle
|
28
|
+
assert @feature.active?
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_activate
|
32
|
+
@feature.activate
|
33
|
+
assert @feature.active?
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_deactivate
|
37
|
+
@feature.deactivate
|
38
|
+
assert @feature.inactive?
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_with_executes_block
|
42
|
+
@feature.deactivate
|
43
|
+
assert_nil @feature.with { true }
|
44
|
+
|
45
|
+
@feature.activate
|
46
|
+
assert @feature.with { true }
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_without_executes_block
|
50
|
+
@feature.deactivate
|
51
|
+
assert @feature.without { true }
|
52
|
+
|
53
|
+
@feature.activate
|
54
|
+
assert_nil @feature.without { true }
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Flop::Repository::GenericRepoExample
|
2
|
+
def test_default_value_is_false
|
3
|
+
refute @repo.get(:unknown_key), 'default value is not false'
|
4
|
+
end
|
5
|
+
|
6
|
+
def test_get_value
|
7
|
+
@repo.set(:test, true)
|
8
|
+
@repo.set(:test2, false)
|
9
|
+
|
10
|
+
assert_equal true, @repo.get(:test)
|
11
|
+
assert_equal false, @repo.get(:test2)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'flop/repository/generic_repo_example'
|
3
|
+
require 'fakeredis'
|
4
|
+
|
5
|
+
class Flop::Repository::RedisTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@repo = Flop::Repository::Redis.new(Redis.new)
|
8
|
+
end
|
9
|
+
|
10
|
+
include Flop::Repository::GenericRepoExample
|
11
|
+
end
|
data/test/flop_test.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flop
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Guillaume DOTT
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-reporters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: fakeredis
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.5'
|
83
|
+
description: Activate and deactivate features easily
|
84
|
+
email:
|
85
|
+
- guillaume+github@dott.fr
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- flop.gemspec
|
96
|
+
- lib/flop.rb
|
97
|
+
- lib/flop/feature.rb
|
98
|
+
- lib/flop/repository/memory.rb
|
99
|
+
- lib/flop/repository/redis.rb
|
100
|
+
- lib/flop/version.rb
|
101
|
+
- test/flop/feature_test.rb
|
102
|
+
- test/flop/repository/generic_repo_example.rb
|
103
|
+
- test/flop/repository/memory_test.rb
|
104
|
+
- test/flop/repository/redis_test.rb
|
105
|
+
- test/flop_test.rb
|
106
|
+
- test/test_helper.rb
|
107
|
+
homepage: https://github.com/gdott9/flop
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.2.2
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: A simple ruby feature flipper
|
131
|
+
test_files:
|
132
|
+
- test/flop/feature_test.rb
|
133
|
+
- test/flop/repository/generic_repo_example.rb
|
134
|
+
- test/flop/repository/memory_test.rb
|
135
|
+
- test/flop/repository/redis_test.rb
|
136
|
+
- test/flop_test.rb
|
137
|
+
- test/test_helper.rb
|