can-do 0.1.0
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 +11 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/can_do.gemspec +21 -0
- data/lib/can_do.rb +73 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6cdc01b713da704aca92b3bf3f7479b3acab2910
|
4
|
+
data.tar.gz: 6f526ea285fd00623b423faeba9d8f66b2712578
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f890889c4cb09d3a2da84256acc35a0747c42358691cd98304b27f38f72947ad12bb25578d9a6e9cd6743c241d5171f3d66ee0da8f9807532db2590715af89d1
|
7
|
+
data.tar.gz: 51e3b27b2b563bc03662fd6eab2b16bd02ef08c9b1fadecb0a4e393abd052a65b4e4ef39560cf7e4410f48dbd4f23eba6e15b32733b39f7b98aea2d211b9828f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Blacklane GmbH
|
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,76 @@
|
|
1
|
+
# CanDo
|
2
|
+
|
3
|
+
Flips your features based on a `config/features.yml` file or environment variables. No data store required.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Add `can-do` to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# Gemfile
|
11
|
+
|
12
|
+
gem "can-do", require: "can_do"
|
13
|
+
```
|
14
|
+
|
15
|
+
Inside the `config` folder relative to your working directory create a file called `features.yml`. Within this file,
|
16
|
+
place your *default feature flags* within the `defaults` key. All available features should be listed here, together with
|
17
|
+
their default values. Add *environment-specific* feature flags under the environment name.
|
18
|
+
|
19
|
+
```
|
20
|
+
# config/features.yml
|
21
|
+
|
22
|
+
defaults:
|
23
|
+
some_feature: false
|
24
|
+
other_feature: true
|
25
|
+
development:
|
26
|
+
some_feature: true
|
27
|
+
production:
|
28
|
+
other_feature: false
|
29
|
+
```
|
30
|
+
|
31
|
+
Check if a feature is enabled by calling `CanDo.feature?(:some_feature)`:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require "can_do"
|
35
|
+
|
36
|
+
CanDo.feature?(:some_feature)
|
37
|
+
```
|
38
|
+
|
39
|
+
Or by using a block:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require "can_do"
|
43
|
+
|
44
|
+
CanDo.feature?(:some_feature) do
|
45
|
+
# This block is only evaluated if some_feature is enabled
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
If a feature is not found, `CanDo::NotAFeature` is raised.
|
50
|
+
|
51
|
+
## Environment variables
|
52
|
+
|
53
|
+
You can use environment variables to flip your features. Environment variables **always take precedence** over anything
|
54
|
+
within your `config/features.yml` file.
|
55
|
+
|
56
|
+
```sh
|
57
|
+
> RAILS_ENV=development rails console
|
58
|
+
CanDo.feature?(:some_feature) => false
|
59
|
+
|
60
|
+
> SOME_FEATURE=true RAILS_ENV=development rails console
|
61
|
+
CanDo.feature?(:some_feature) => true
|
62
|
+
|
63
|
+
> SOME_FEATURE=true RAILS_ENV=development rails console
|
64
|
+
CanDo.feature?(:some_feature) => true
|
65
|
+
|
66
|
+
> OTHER_FEATURE=true RAILS_ENV=production rails console
|
67
|
+
CanDo.feature?(:other_feature) => true
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it ( https://github.com/blacklane/can_do/fork )
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create a new Pull Request
|
data/can_do.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "can-do"
|
6
|
+
spec.version = "0.1.0"
|
7
|
+
spec.authors = ["Florin Lipan"]
|
8
|
+
spec.email = ["florin.lipan@blacklane.com"]
|
9
|
+
|
10
|
+
spec.summary = %q{Simple feature flags.}
|
11
|
+
spec.description = %q{Simple feature flags based on a YAML config file and/or environment variables.}
|
12
|
+
spec.homepage = "https://github.com/blacklane/can-do"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
end
|
data/lib/can_do.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require "singleton"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
# Flips your features based on a config/features.yml file or environment variables.
|
5
|
+
# Environment variables always take precedence over the settings in your YAML file.
|
6
|
+
#
|
7
|
+
# @example config/features.yml
|
8
|
+
# defaults:
|
9
|
+
# some_feature: false
|
10
|
+
# other_feature: true
|
11
|
+
# development:
|
12
|
+
# some_feature: true
|
13
|
+
#
|
14
|
+
# @example test if a feature should be enabled
|
15
|
+
# > RAILS_ENV=development rails console
|
16
|
+
# CanDo.feature?(:some_feature) # => false
|
17
|
+
#
|
18
|
+
# @example overwrite setting with environment variable
|
19
|
+
# > SOME_FEATURE=true RAILS_ENV=development rails console
|
20
|
+
# CanDo.feature?(:some_feature) # => true
|
21
|
+
#
|
22
|
+
# @example call with a block
|
23
|
+
# CanDo.feature?(:some_feature) do
|
24
|
+
# # this block get's called if some_feature is enabled
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
class CanDo
|
28
|
+
include Singleton
|
29
|
+
|
30
|
+
NotAFeature = Class.new(StandardError)
|
31
|
+
|
32
|
+
THE_TRUTH = /^(true|t|yes|y|1)$/i
|
33
|
+
DEFAULT_NAMESPACE = "defaults".freeze
|
34
|
+
|
35
|
+
attr_reader :features
|
36
|
+
|
37
|
+
def self.features
|
38
|
+
instance.features
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.feature?(name, &block)
|
42
|
+
name = name.to_s
|
43
|
+
env_name = name.upcase
|
44
|
+
|
45
|
+
fail NotAFeature.new(name) unless features.key?(name)
|
46
|
+
|
47
|
+
is_enabled =
|
48
|
+
if ENV.key?(env_name)
|
49
|
+
!!(ENV[env_name] =~ THE_TRUTH)
|
50
|
+
else
|
51
|
+
features[name] == true
|
52
|
+
end
|
53
|
+
|
54
|
+
# If no block is passed, return true or false
|
55
|
+
return is_enabled unless block_given?
|
56
|
+
|
57
|
+
# If a block is passed, return block or nil
|
58
|
+
yield if is_enabled
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def initialize
|
64
|
+
yaml = File.read(File.expand_path("config/features.yml", Dir.pwd))
|
65
|
+
data = YAML.load(yaml)
|
66
|
+
|
67
|
+
@features = (data.fetch(DEFAULT_NAMESPACE, {})).merge(data.fetch(env, {}))
|
68
|
+
end
|
69
|
+
|
70
|
+
def env
|
71
|
+
ENV["RAILS_ENV"] || ENV["RACK_ENV"]
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: can-do
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florin Lipan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-08 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
|
+
description: Simple feature flags based on a YAML config file and/or environment variables.
|
28
|
+
email:
|
29
|
+
- florin.lipan@blacklane.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- can_do.gemspec
|
39
|
+
- lib/can_do.rb
|
40
|
+
homepage: https://github.com/blacklane/can-do
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.4.8
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Simple feature flags.
|
64
|
+
test_files: []
|
65
|
+
has_rdoc:
|