simple_switch 0.2.0 → 0.3.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.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -1
  3. data/README.md +175 -58
  4. data/Rakefile +21 -1
  5. data/app/assets/javascripts/simple_switch/application.js +1 -0
  6. data/app/assets/stylesheets/simple_switch/application.css +4 -0
  7. data/app/controllers/simple_switch/application_controller.rb +4 -0
  8. data/app/helpers/simple_switch/application_helper.rb +4 -0
  9. data/app/models/simple_switch/environment.rb +8 -0
  10. data/app/models/simple_switch/feature.rb +8 -0
  11. data/app/models/simple_switch/state.rb +15 -0
  12. data/app/views/layouts/simple_switch/application.html.erb +14 -0
  13. data/config/routes.rb +2 -0
  14. data/db/migrate/20151216180633_create_simple_switch_tables.simple_switch.rb +24 -0
  15. data/lib/generators/simple_switch/initialize_generator.rb +17 -0
  16. data/lib/generators/simple_switch/install_generator.rb +13 -3
  17. data/lib/generators/templates/feature_config_sample.yml +16 -12
  18. data/lib/generators/templates/migration.rb +24 -0
  19. data/lib/generators/templates/simple_switch.rb +9 -2
  20. data/lib/simple_switch.rb +10 -6
  21. data/lib/simple_switch/engine.rb +10 -0
  22. data/lib/simple_switch/feature_manager.rb +15 -0
  23. data/lib/simple_switch/feature_manager_db.rb +88 -0
  24. data/lib/simple_switch/feature_manager_yaml.rb +68 -0
  25. data/lib/simple_switch/manager_shared_methods.rb +52 -0
  26. data/lib/simple_switch/railtie.rb +12 -0
  27. data/lib/simple_switch/shared_controller_methods.rb +2 -2
  28. data/lib/simple_switch/version.rb +1 -1
  29. data/lib/tasks/simple_switch.rake +106 -0
  30. data/spec/config/feature_config.yml +10 -6
  31. data/spec/dummy/README.rdoc +28 -0
  32. data/spec/dummy/Rakefile +6 -0
  33. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  34. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  35. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  36. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  37. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  38. data/spec/dummy/bin/bundle +3 -0
  39. data/spec/dummy/bin/rails +4 -0
  40. data/spec/dummy/bin/rake +4 -0
  41. data/spec/dummy/bin/setup +29 -0
  42. data/spec/dummy/config.ru +4 -0
  43. data/spec/dummy/config/application.rb +26 -0
  44. data/spec/dummy/config/boot.rb +5 -0
  45. data/spec/dummy/config/database.yml +25 -0
  46. data/spec/dummy/config/environment.rb +5 -0
  47. data/spec/dummy/config/environments/development.rb +41 -0
  48. data/spec/dummy/config/environments/production.rb +79 -0
  49. data/spec/dummy/config/environments/test.rb +42 -0
  50. data/spec/dummy/config/initializers/assets.rb +11 -0
  51. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  53. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  54. data/spec/dummy/config/initializers/inflections.rb +16 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  56. data/spec/dummy/config/initializers/session_store.rb +3 -0
  57. data/spec/dummy/config/initializers/simple_switch.rb +17 -0
  58. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy/config/locales/en.yml +23 -0
  60. data/spec/dummy/config/routes.rb +4 -0
  61. data/spec/dummy/config/secrets.yml +22 -0
  62. data/spec/dummy/db/development.sqlite3 +0 -0
  63. data/spec/dummy/db/schema.rb +44 -0
  64. data/spec/dummy/db/seeds.rb +17 -0
  65. data/spec/dummy/db/test.sqlite3 +0 -0
  66. data/spec/dummy/log/development.log +54 -0
  67. data/spec/dummy/log/test.log +7280 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/factories/simple_switch_environments.rb +5 -0
  70. data/spec/factories/simple_switch_features.rb +5 -0
  71. data/spec/factories/simple_switch_states.rb +5 -0
  72. data/spec/models/simple_switch/environment_spec.rb +11 -0
  73. data/spec/models/simple_switch/feature_spec.rb +11 -0
  74. data/spec/models/simple_switch/state_spec.rb +11 -0
  75. data/spec/route_helper.rb +5 -0
  76. data/spec/simple_switch_feature_managers_spec.rb +282 -0
  77. data/spec/spec_helper.rb +22 -27
  78. metadata +203 -17
  79. data/.gitignore +0 -3
  80. data/.rspec +0 -2
  81. data/.travis.yml +0 -7
  82. data/Gemfile +0 -4
  83. data/LICENSE.txt +0 -22
  84. data/codeclimate.yml +0 -3
  85. data/lib/generators/simple_switch/install_yaml_generator.rb +0 -18
  86. data/lib/simple_switch/switch.rb +0 -77
  87. data/simple_switch.gemspec +0 -26
  88. data/spec/simple_switch_spec.rb +0 -91
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- Gemfile.lock
2
- .idea
3
- .ruby-version
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --order rand
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - "2.2.0"
4
-
5
- before_install: gem install bundler -v 1.10.6
6
-
7
- script: bundle exec rspec spec
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in simple_switch.gemspec
4
- gemspec
data/LICENSE.txt DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2015 Sen Zhang
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/codeclimate.yml DELETED
@@ -1,3 +0,0 @@
1
- engines:
2
- rubocop:
3
- enabled: true
@@ -1,18 +0,0 @@
1
- require 'rails/generators/base'
2
-
3
- module SimpleSwitch
4
- module Generators
5
-
6
- class InstallYamlGenerator < Rails::Generators::Base
7
- source_root File.expand_path('../../templates', __FILE__)
8
-
9
- desc 'Copy feature configuration sample file feature_config_sample.yml to your application.'
10
-
11
- def copy_initializer
12
- copy_file 'feature_config_sample.yml',
13
- "#{SimpleSwitch::feature_config_file_dir}/#{SimpleSwitch::feature_config_file_name}"
14
- end
15
- end
16
-
17
- end
18
- end
@@ -1,77 +0,0 @@
1
- module SimpleSwitch
2
- class Switch
3
- def initialize
4
- @feature_config = load_config
5
- end
6
-
7
- def self.instance
8
- return @@instance ||= send(:new)
9
- end
10
-
11
- private_class_method :new
12
-
13
- def on?(feature, env=Rails.env)
14
- @feature_config[feature][env] if valid_feature_name_for_env?(feature, env)
15
- end
16
-
17
- def off?(feature, env=Rails.env)
18
- !on?(feature, env)
19
- end
20
-
21
- def update(feature, env, value)
22
- @feature_config[feature][env] = value if valid_feature_name_for_env?(feature, env)
23
-
24
- save_to_yaml
25
- end
26
-
27
- def delete(feature)
28
- @feature_config.delete(feature) if valid_feature_name?(feature)
29
-
30
- save_to_yaml
31
- end
32
-
33
- private
34
-
35
- def file_path
36
- Rails.root.join(SimpleSwitch::feature_config_file_dir, SimpleSwitch::feature_config_file_name)
37
- end
38
-
39
- def load_config
40
- HashWithIndifferentAccess.new(YAML::load(File.open(file_path)))
41
- end
42
-
43
- def reload_config!
44
- @feature_config = load_config
45
- end
46
-
47
- def save_to_yaml
48
- begin
49
- File.open(file_path, 'w') do |f|
50
- f.puts @feature_config.to_hash.to_yaml
51
- end
52
-
53
- true
54
- rescue
55
- false
56
- end
57
- end
58
-
59
- def valid_feature_name?(feature)
60
- reload_config! unless @feature_config.has_key?(feature)
61
-
62
- return true if @feature_config.has_key?(feature)
63
-
64
- raise "Cannot find feature '#{feature}', check out your "\
65
- "#{SimpleSwitch::feature_config_file_name} file."
66
- end
67
-
68
- def valid_feature_name_for_env?(feature, env=Rails.env)
69
- valid_feature_name?(feature)
70
-
71
- return true if @feature_config[feature].has_key?(env)
72
-
73
- raise "Cannot find environment '#{env}' for feature '#{feature}', "\
74
- "check out your #{SimpleSwitch::feature_config_file_name} file."
75
- end
76
- end
77
- end
@@ -1,26 +0,0 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
-
4
- require 'simple_switch/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'simple_switch'
8
- spec.version = SimpleSwitch::VERSION
9
- spec.authors = ['Sen Zhang']
10
- spec.email = ['solowolf21@gmail.com']
11
- spec.summary = %q{Simple Feature Switch Engine}
12
- spec.description = %q{Simple Feature Switch Engine}
13
- spec.homepage = 'https://github.com/Sen-Zhang/simple_switch'
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
- spec.required_ruby_version = '>= 2.0.0'
21
-
22
- spec.add_development_dependency 'bundler', '~> 1.10'
23
- spec.add_development_dependency 'rake', '~> 10.0'
24
- spec.add_development_dependency 'rails', '~> 4.2.0'
25
- spec.add_development_dependency 'rspec', '~> 3.3.0'
26
- end
@@ -1,91 +0,0 @@
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