simple_flag 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +14 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/LICENSE +21 -0
- data/README.md +90 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/simple_flag.rb +168 -0
- data/lib/simple_flag/version.rb +5 -0
- data/simple_flag.gemspec +36 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4f2041da22824b405194cc6203aa275e474ee4055f733d4959c60256d0984d51
|
4
|
+
data.tar.gz: d5c4f449cadb4febda25719c8653eeff8d1f4fc65c42c72f9f0d1661cae3eac1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d95d77afd1ee6c44acf335e977034920f7f70eba17f52577823489240bfa3089ee5348a762fd7f2a192ed7ac29b93a1aca52d35567fc4e80363d751bf2602a47
|
7
|
+
data.tar.gz: 440b21b9616ff41ff02597088ec1da841224cbfa513c6c3f5c309ef576e5480fac001399715a316edc7929c6aa76385609327fdea6f5b3b357d2ace469e1f223
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Layout/LineLength:
|
2
|
+
Max: 120
|
3
|
+
Layout/MultilineMethodCallIndentation:
|
4
|
+
Exclude:
|
5
|
+
- ./spec/**/*_spec.rb
|
6
|
+
Metrics/BlockLength:
|
7
|
+
Exclude:
|
8
|
+
- ./spec/**/*_spec.rb
|
9
|
+
Metrics/MethodLength:
|
10
|
+
Max: 20
|
11
|
+
Style/GuardClause:
|
12
|
+
Enabled: false
|
13
|
+
Style/NumericPredicate:
|
14
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Vojtech Kusy
|
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,90 @@
|
|
1
|
+
# SimpleFlag
|
2
|
+
|
3
|
+
Simple but powerful feature flag implementation in 90 LOC.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'simple_flag'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install simple_flag
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Configuration
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
FEATURE = SimpleFlag.new do |feature|
|
27
|
+
feature.define(:new_user_profile) do |user_id:|
|
28
|
+
Admin.where(user_id: user_id).exists?
|
29
|
+
end
|
30
|
+
feature.define(:third_party_analytics) do
|
31
|
+
not Rails.env.production?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
### Usage in app code
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class ProfilesController < ApplicationController
|
40
|
+
def show
|
41
|
+
FEATURE.with(:new_user_profile, user_id: current_user.id) do
|
42
|
+
return render :new_user_profile, locals: { user: NewUserProfilePresenterV2.new(current_user) }
|
43
|
+
end
|
44
|
+
render :show, locals: { user: UserProfilePresenterV1.new(current_user) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
### Testing with before...after
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
describe "User profiles" do
|
53
|
+
after { FEATURE.reset_all_overrides }
|
54
|
+
|
55
|
+
context "with new_user_profile feature enabled" do
|
56
|
+
before { FEATURE.override(:new_user_profile, true) }
|
57
|
+
it "renders new profile"
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with new_user_profile feature disabled" do
|
61
|
+
before { FEATURE.override(:new_user_profile, false) }
|
62
|
+
it "renders old profile"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
### Testing with inline block
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
it "shows new user profile" do
|
71
|
+
FEATURE.override_with(:new_user_profile, true) do
|
72
|
+
expect( FEATURE.active?(:new_user_profile) ).to be_truthy
|
73
|
+
end
|
74
|
+
expect( FEATURE.active?(:new_user_profile) ).to be_falsy
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
81
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
82
|
+
|
83
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
84
|
+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
|
85
|
+
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wojtha/simple_flag.
|
90
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'simple_flag'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/simple_flag.rb
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'simple_flag/version'
|
4
|
+
|
5
|
+
# Class FeatureFlag defines and stores feature flags
|
6
|
+
#
|
7
|
+
# @example Configuration
|
8
|
+
#
|
9
|
+
# FEATURE = FeatureFlag.new do |feature|
|
10
|
+
# feature.define(:new_user_profile) do |user_id:|
|
11
|
+
# Admin.where(user_id: user_id).exists?
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# feature.define(:third_party_analytics) do
|
15
|
+
# not Rails.env.production?
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# @example Usage
|
20
|
+
#
|
21
|
+
# class ProfilesController < ApplicationController
|
22
|
+
# def show
|
23
|
+
# FEATURE.with(:new_user_profile, user_id: current_user.id) do
|
24
|
+
# return render :new_user_profile, locals: { user: NewUserProfilePresenterV2.new(current_user) }
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# render :show, locals: { user: UserProfilePresenterV1.new(current_user) }
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# @example Testing with before...after
|
32
|
+
#
|
33
|
+
# describe "User profiles" do
|
34
|
+
# before { FEATURE.override(:new_user_profile, true) }
|
35
|
+
# after { FEATURE.reset_all_overrides }
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# @example Testing with inline block
|
39
|
+
#
|
40
|
+
# it "shows new user profile" do
|
41
|
+
# FEATURE.override_with(:new_user_profile, true) do
|
42
|
+
# expect( FEATURE.active?(:new_user_profile) ).to be_truthy
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# expect( FEATURE.active?(:new_user_profile) ).to be_falsy
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# @see http://blog.arkency.com/2015/11/simple-feature-toggle-for-rails-app/
|
49
|
+
#
|
50
|
+
class SimpleFlag
|
51
|
+
FlagAlreadyDefined = Class.new(StandardError)
|
52
|
+
FlagNotDefined = Class.new(StandardError)
|
53
|
+
FlagNotOverridden = Class.new(StandardError)
|
54
|
+
FlagArgumentsMismatch = Class.new(StandardError)
|
55
|
+
|
56
|
+
attr_reader :env
|
57
|
+
|
58
|
+
def initialize(env: nil)
|
59
|
+
@env = env
|
60
|
+
@flags = {}
|
61
|
+
@overrides = {}
|
62
|
+
yield self if block_given?
|
63
|
+
end
|
64
|
+
|
65
|
+
def define(name, &block)
|
66
|
+
raise FlagAlreadyDefined, "Feature flag `#{name}` is already defined" if flag?(name)
|
67
|
+
|
68
|
+
@flags[name] = block
|
69
|
+
end
|
70
|
+
|
71
|
+
def redefine(name, &block)
|
72
|
+
@flags[name] = block
|
73
|
+
end
|
74
|
+
|
75
|
+
def flags
|
76
|
+
@flags.keys
|
77
|
+
end
|
78
|
+
|
79
|
+
def with(name, *args, &block)
|
80
|
+
block.call if active?(name, *args)
|
81
|
+
end
|
82
|
+
|
83
|
+
def override(name, result = true, &block)
|
84
|
+
raise FlagNotDefined, "Feature flag `#{name}` is not defined" unless flag?(name)
|
85
|
+
raise FlagAlreadyDefined, "Feature flag `#{name}` is already overridden" if overridden?(name)
|
86
|
+
|
87
|
+
# We are using proc, not lambda, because proc does not check for number of arguments
|
88
|
+
original_block = @flags[name]
|
89
|
+
@overrides[name] = original_block
|
90
|
+
@flags[name] =
|
91
|
+
if block_given?
|
92
|
+
validate_flag_arity(name, original_block.arity, block.arity)
|
93
|
+
block
|
94
|
+
else
|
95
|
+
proc { |*_args| result }
|
96
|
+
end
|
97
|
+
|
98
|
+
original_block
|
99
|
+
end
|
100
|
+
|
101
|
+
def reset_override(name)
|
102
|
+
raise FlagNotOverridden, "Feature flag `#{name}` was not overridden" unless overridden?(name)
|
103
|
+
|
104
|
+
original_block = @overrides.delete(name)
|
105
|
+
@flags[name] = original_block
|
106
|
+
end
|
107
|
+
|
108
|
+
def reset_all_overrides
|
109
|
+
@overrides.each_key { |name| reset_override(name) }
|
110
|
+
end
|
111
|
+
|
112
|
+
def override_with(name, result = true, &block)
|
113
|
+
raise FlagNotDefined, "Feature flag `#{name}` is not defined" unless flag?(name)
|
114
|
+
|
115
|
+
original_block = @flags[name]
|
116
|
+
@flags[name] = proc { |*_args| result }
|
117
|
+
block.call
|
118
|
+
ensure
|
119
|
+
@flags[name] = original_block
|
120
|
+
end
|
121
|
+
|
122
|
+
def flag?(name)
|
123
|
+
@flags.key?(name)
|
124
|
+
end
|
125
|
+
|
126
|
+
def overridden?(name)
|
127
|
+
@overrides.key?(name)
|
128
|
+
end
|
129
|
+
|
130
|
+
def active?(name, *args)
|
131
|
+
flag = @flags.fetch(name, proc { |*_args| false })
|
132
|
+
validate_flag_arguments(name, flag.arity, args.size)
|
133
|
+
flag.call(*args)
|
134
|
+
end
|
135
|
+
alias enabled? active?
|
136
|
+
|
137
|
+
def inactive?(name, *args)
|
138
|
+
!active?(name, *args)
|
139
|
+
end
|
140
|
+
alias disabled? inactive?
|
141
|
+
|
142
|
+
def env?(*args)
|
143
|
+
[*args].map(&:to_s).include?(env.to_s)
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
def validate_flag_arity(flag_name, flag_arity, override_arity)
|
149
|
+
original_arity = flag_arity < 0 ? flag_arity.abs - 1 : flag_arity
|
150
|
+
|
151
|
+
if original_arity != override_arity
|
152
|
+
raise FlagArgumentsMismatch, "Flag '#{flag_name}' expects #{flag_arity} arguments, " \
|
153
|
+
"but #{override_arity} arguments were given"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def validate_flag_arguments(flag_name, flag_arity, args_size)
|
158
|
+
if flag_arity < 0 && (flag_arity.abs - 1) > args_size
|
159
|
+
# Contains variable -n-1 arguments
|
160
|
+
raise FlagArgumentsMismatch, "Flag '#{flag_name}' expects #{flag_arity.abs - 1} or more arguments, " \
|
161
|
+
"but #{args_size} arguments were given"
|
162
|
+
elsif flag_arity >= 0 && flag_arity != args_size
|
163
|
+
# Contains zero or fixed number of arguments
|
164
|
+
raise FlagArgumentsMismatch, "Flag '#{flag_name}' expects #{flag_arity} arguments, " \
|
165
|
+
"but #{args_size} arguments were given"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
data/simple_flag.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/simple_flag/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'simple_flag'
|
7
|
+
spec.authors = ['Vojtěch Kusý']
|
8
|
+
spec.email = ['wojtha@gmail.com']
|
9
|
+
spec.license = 'MIT'
|
10
|
+
spec.version = SimpleFlag::VERSION
|
11
|
+
|
12
|
+
spec.summary = 'Simple feature flags'
|
13
|
+
spec.description = 'Simple but powerful feature flag implementation in 90 LOC.'
|
14
|
+
spec.homepage = 'https://github.com/wojtha/simple_flag'
|
15
|
+
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
18
|
+
spec.metadata['changelog_uri'] = 'https://github.com/wojtha/simple_flag/blob/master/CHANGELOG.md'
|
19
|
+
spec.metadata['source_code_uri'] = 'https://github.com/wojtha/simple_flag'
|
20
|
+
spec.metadata['bug_tracker_uri'] = 'https://github.com/wojtha/simple_flag/issues'
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.4.0'
|
23
|
+
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
27
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
+
end
|
29
|
+
spec.bindir = 'bin'
|
30
|
+
spec.executables = []
|
31
|
+
spec.require_paths = ['lib']
|
32
|
+
|
33
|
+
spec.add_development_dependency 'bundler'
|
34
|
+
spec.add_development_dependency 'rake'
|
35
|
+
spec.add_development_dependency 'rspec'
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_flag
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vojtěch Kusý
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-29 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Simple but powerful feature flag implementation in 90 LOC.
|
56
|
+
email:
|
57
|
+
- wojtha@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/simple_flag.rb
|
73
|
+
- lib/simple_flag/version.rb
|
74
|
+
- simple_flag.gemspec
|
75
|
+
homepage: https://github.com/wojtha/simple_flag
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata:
|
79
|
+
homepage_uri: https://github.com/wojtha/simple_flag
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
changelog_uri: https://github.com/wojtha/simple_flag/blob/master/CHANGELOG.md
|
82
|
+
source_code_uri: https://github.com/wojtha/simple_flag
|
83
|
+
bug_tracker_uri: https://github.com/wojtha/simple_flag/issues
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 2.4.0
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubygems_version: 3.1.1
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Simple feature flags
|
103
|
+
test_files: []
|