simple_switch 0.1.0 → 0.2.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 +4 -4
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +9 -0
- data/README.md +97 -1
- data/lib/simple_switch/shared_controller_methods.rb +6 -2
- data/lib/simple_switch/version.rb +1 -1
- data/simple_switch.gemspec +2 -0
- data/spec/config/feature_config.yml +9 -0
- data/spec/simple_switch_spec.rb +91 -0
- data/spec/spec_helper.rb +35 -0
- metadata +39 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f8379b3f743c3831e33cf764452d91835d03ece
|
4
|
+
data.tar.gz: 066a6820f1ae9d8dfe3b62870281baab8c4ee904
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8110097a9c077e7a8dfc7d00bd4e60c6e49f8acd1f877a1c009e7f7baecc1196b56b8418fb9c495788fad337c42950b31ea66bda24a2673b27ee1d728684630b
|
7
|
+
data.tar.gz: fbc574f38d60b26b78ecfa5a9895b931b5fc02388e4b573c1b088e9c11b00915f4fd4834a08426cd3cef3e52559abe5936373916a2a76eb6d1cc50c8cc12ecd7
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
1
|
# simple_switch Change Log
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
simple_switch is in a 0.2.0 beta state. This means that its APIs and behavior are subject to breaking changes without deprecation notices.
|
6
|
+
|
7
|
+
## [0.2.0](https://github.com/Sen-Zhang/simple_switch/releases/tag/v0.2.0) (2015-12-08)
|
8
|
+
|
9
|
+
Changes:
|
10
|
+
|
11
|
+
* Add tests
|
12
|
+
* Refactor APIs for action_controller
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
Simple Feature Switch Engine
|
4
4
|
|
5
5
|
[](https://codeclimate.com/github/Sen-Zhang/simple_switch)
|
6
|
+
[](https://travis-ci.org/Sen-Zhang/simple_switch)
|
6
7
|
|
7
8
|
|
8
9
|
## Requirement
|
@@ -27,4 +28,99 @@ Or install it yourself as:
|
|
27
28
|
|
28
29
|
## Usage
|
29
30
|
|
30
|
-
|
31
|
+
### Basic
|
32
|
+
|
33
|
+
Run install generator:
|
34
|
+
|
35
|
+
$ rails generate simple_switch:install
|
36
|
+
|
37
|
+
A initializer file named `simple_switch.rb` will be added into `config/initializers` after running
|
38
|
+
install generator. In that file, you can customize the feature configuration yaml file's name and
|
39
|
+
installation place.
|
40
|
+
|
41
|
+
# feature switch configuration yaml file stored location, by default it is stored
|
42
|
+
# under config directory
|
43
|
+
config.feature_config_file_dir = 'config'
|
44
|
+
|
45
|
+
# feature switch configuration yaml file name, by default it is 'feature_config.yml'
|
46
|
+
config.feature_config_file_name = 'feature_config.yml'
|
47
|
+
|
48
|
+
Then run the following commands to copy feature configuration yaml file to the target directory.
|
49
|
+
|
50
|
+
Run install generator:
|
51
|
+
|
52
|
+
$ rails generate simple_switch:install_yaml
|
53
|
+
|
54
|
+
Now you are ready to define features:
|
55
|
+
|
56
|
+
foo:
|
57
|
+
development: true
|
58
|
+
test: true
|
59
|
+
production: false
|
60
|
+
|
61
|
+
bar:
|
62
|
+
development: false
|
63
|
+
test: true
|
64
|
+
production: false
|
65
|
+
|
66
|
+
Now you can use it in models like this:
|
67
|
+
|
68
|
+
class TestModel < ActiveRecord::Base
|
69
|
+
...
|
70
|
+
|
71
|
+
if feature_on?(:foo)
|
72
|
+
def foo_method
|
73
|
+
...
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def bar_method
|
78
|
+
if feature_on?(:bar)
|
79
|
+
...
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
...
|
84
|
+
end
|
85
|
+
|
86
|
+
In controllers like this:
|
87
|
+
|
88
|
+
class TestController < ApplicationController
|
89
|
+
...
|
90
|
+
|
91
|
+
def index
|
92
|
+
...
|
93
|
+
|
94
|
+
if feature_on?(:foo)
|
95
|
+
redirect_to :back
|
96
|
+
end
|
97
|
+
|
98
|
+
...
|
99
|
+
end
|
100
|
+
|
101
|
+
...
|
102
|
+
end
|
103
|
+
|
104
|
+
And in views like this:
|
105
|
+
|
106
|
+
<% if feature_on?(:foo) %>
|
107
|
+
<p>Experiment foo is on</p>
|
108
|
+
<% end %>
|
109
|
+
|
110
|
+
<% if feature_off?(:bar) %>
|
111
|
+
<p>Experiment bar is off</p>
|
112
|
+
<% end %>
|
113
|
+
|
114
|
+
### Toggle Features
|
115
|
+
|
116
|
+
The following methods are only accessible in controllers:
|
117
|
+
|
118
|
+
`feature_config_info` return a hash represents the current feature configuration condition.
|
119
|
+
|
120
|
+
`turn_on(feature, env)` turn on a feature on a indicated environment.
|
121
|
+
|
122
|
+
turn_on(:foo, :development)
|
123
|
+
|
124
|
+
`turn_off(feature, env)` turn off a feature on a indicated environment.
|
125
|
+
|
126
|
+
turn_off(:foo, :development)
|
@@ -5,8 +5,12 @@ module SimpleSwitch
|
|
5
5
|
SimpleSwitch.feature_manager.feature_config
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
SimpleSwitch.feature_manager.update(feature, env,
|
8
|
+
def turn_on(feature, env)
|
9
|
+
SimpleSwitch.feature_manager.update(feature, env, true)
|
10
|
+
end
|
11
|
+
|
12
|
+
def turn_off(feature, env)
|
13
|
+
SimpleSwitch.feature_manager.update(feature, env, false)
|
10
14
|
end
|
11
15
|
|
12
16
|
end
|
data/simple_switch.gemspec
CHANGED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'SimpleSwitch' do
|
4
|
+
describe 'Switch' do
|
5
|
+
it 'on? works fine' do
|
6
|
+
allow(Rails).to receive(:env).and_return('development')
|
7
|
+
|
8
|
+
expect(SimpleSwitch.feature_manager.on?(:foo)).to be_truthy
|
9
|
+
expect(SimpleSwitch.feature_manager.on?(:bar)).to be_truthy
|
10
|
+
|
11
|
+
allow(Rails).to receive(:env).and_return('test')
|
12
|
+
|
13
|
+
expect(SimpleSwitch.feature_manager.on?(:foo)).to be_truthy
|
14
|
+
expect(SimpleSwitch.feature_manager.on?(:bar)).to be_falsey
|
15
|
+
|
16
|
+
allow(Rails).to receive(:env).and_return('production')
|
17
|
+
|
18
|
+
expect(SimpleSwitch.feature_manager.on?(:foo)).to be_falsey
|
19
|
+
expect(SimpleSwitch.feature_manager.on?(:bar)).to be_truthy
|
20
|
+
|
21
|
+
expect(SimpleSwitch.feature_manager.on?(:foo, :development)).to be_truthy
|
22
|
+
expect(SimpleSwitch.feature_manager.on?(:bar, :development)).to be_truthy
|
23
|
+
|
24
|
+
expect(SimpleSwitch.feature_manager.on?(:foo, :test)).to be_truthy
|
25
|
+
expect(SimpleSwitch.feature_manager.on?(:bar, :test)).to be_falsey
|
26
|
+
|
27
|
+
expect(SimpleSwitch.feature_manager.on?(:foo, :production)).to be_falsey
|
28
|
+
expect(SimpleSwitch.feature_manager.on?(:bar, :production)).to be_truthy
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'off? works fine' do
|
32
|
+
allow(Rails).to receive(:env).and_return('development')
|
33
|
+
|
34
|
+
expect(SimpleSwitch.feature_manager.off?(:foo)).to be_falsey
|
35
|
+
expect(SimpleSwitch.feature_manager.off?(:bar)).to be_falsey
|
36
|
+
|
37
|
+
allow(Rails).to receive(:env).and_return('test')
|
38
|
+
|
39
|
+
expect(SimpleSwitch.feature_manager.off?(:foo)).to be_falsey
|
40
|
+
expect(SimpleSwitch.feature_manager.off?(:bar)).to be_truthy
|
41
|
+
|
42
|
+
allow(Rails).to receive(:env).and_return('production')
|
43
|
+
|
44
|
+
expect(SimpleSwitch.feature_manager.off?(:foo)).to be_truthy
|
45
|
+
expect(SimpleSwitch.feature_manager.off?(:bar)).to be_falsey
|
46
|
+
|
47
|
+
expect(SimpleSwitch.feature_manager.off?(:foo, :development)).to be_falsey
|
48
|
+
expect(SimpleSwitch.feature_manager.off?(:bar, :development)).to be_falsey
|
49
|
+
|
50
|
+
expect(SimpleSwitch.feature_manager.off?(:foo, :test)).to be_falsey
|
51
|
+
expect(SimpleSwitch.feature_manager.off?(:bar, :test)).to be_truthy
|
52
|
+
|
53
|
+
expect(SimpleSwitch.feature_manager.off?(:foo, :production)).to be_truthy
|
54
|
+
expect(SimpleSwitch.feature_manager.off?(:bar, :production)).to be_falsey
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'update works fine' do
|
58
|
+
expect(SimpleSwitch.feature_manager.on?(:foo, :development)).to be_truthy
|
59
|
+
|
60
|
+
SimpleSwitch.feature_manager.update(:foo, :development, false)
|
61
|
+
|
62
|
+
expect(SimpleSwitch.feature_manager.off?(:foo, :development)).to be_truthy
|
63
|
+
|
64
|
+
expect(SimpleSwitch.feature_manager.off?(:bar, :test)).to be_truthy
|
65
|
+
|
66
|
+
SimpleSwitch.feature_manager.update(:bar, :test, true)
|
67
|
+
|
68
|
+
expect(SimpleSwitch.feature_manager.on?(:bar, :test)).to be_truthy
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'delete works fine' do
|
72
|
+
expect(SimpleSwitch.feature_manager.on?(:foo, :development)).to be_truthy
|
73
|
+
|
74
|
+
SimpleSwitch.feature_manager.delete(:foo)
|
75
|
+
|
76
|
+
expect {
|
77
|
+
SimpleSwitch.feature_manager.on?(:foo, :development)
|
78
|
+
}.to raise_error(RuntimeError, "Cannot find feature 'foo', check out your feature_config.yml file.")
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'raise errors correctly' do
|
82
|
+
expect {
|
83
|
+
SimpleSwitch.feature_manager.update(:foobar, :development, false)
|
84
|
+
}.to raise_error(RuntimeError, "Cannot find feature 'foobar', check out your feature_config.yml file.")
|
85
|
+
|
86
|
+
expect {
|
87
|
+
SimpleSwitch.feature_manager.update(:foo, :dev, false)
|
88
|
+
}.to raise_error(RuntimeError, "Cannot find environment 'dev' for feature 'foo', check out your feature_config.yml file.")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'rails'
|
3
|
+
require 'simple_switch'
|
4
|
+
|
5
|
+
Bundler.setup
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.expect_with(:rspec) { |c| c.syntax = :expect }
|
9
|
+
config.order = :random
|
10
|
+
|
11
|
+
config.before :each do
|
12
|
+
# reset feature_config.yml
|
13
|
+
File.open('spec/config/feature_config.yml', 'w') do |f|
|
14
|
+
init_hash = HashWithIndifferentAccess.new(
|
15
|
+
{
|
16
|
+
foo: {
|
17
|
+
development: true,
|
18
|
+
test: true,
|
19
|
+
production: false
|
20
|
+
},
|
21
|
+
bar: {
|
22
|
+
development: true,
|
23
|
+
test: false,
|
24
|
+
production: true
|
25
|
+
}
|
26
|
+
}
|
27
|
+
)
|
28
|
+
|
29
|
+
f.puts init_hash.to_hash.to_yaml
|
30
|
+
end
|
31
|
+
|
32
|
+
allow_any_instance_of(SimpleSwitch::Switch).to receive(:file_path).and_return('spec/config/feature_config.yml')
|
33
|
+
SimpleSwitch.feature_manager.send(:reload_config!)
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_switch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sen Zhang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.3.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.3.0
|
41
69
|
description: Simple Feature Switch Engine
|
42
70
|
email:
|
43
71
|
- solowolf21@gmail.com
|
@@ -46,6 +74,8 @@ extensions: []
|
|
46
74
|
extra_rdoc_files: []
|
47
75
|
files:
|
48
76
|
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .travis.yml
|
49
79
|
- CHANGELOG.md
|
50
80
|
- Gemfile
|
51
81
|
- LICENSE.txt
|
@@ -64,6 +94,9 @@ files:
|
|
64
94
|
- lib/simple_switch/switch.rb
|
65
95
|
- lib/simple_switch/version.rb
|
66
96
|
- simple_switch.gemspec
|
97
|
+
- spec/config/feature_config.yml
|
98
|
+
- spec/simple_switch_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
67
100
|
homepage: https://github.com/Sen-Zhang/simple_switch
|
68
101
|
licenses:
|
69
102
|
- MIT
|
@@ -88,5 +121,8 @@ rubygems_version: 2.4.8
|
|
88
121
|
signing_key:
|
89
122
|
specification_version: 4
|
90
123
|
summary: Simple Feature Switch Engine
|
91
|
-
test_files:
|
124
|
+
test_files:
|
125
|
+
- spec/config/feature_config.yml
|
126
|
+
- spec/simple_switch_spec.rb
|
127
|
+
- spec/spec_helper.rb
|
92
128
|
has_rdoc:
|