simple_switch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e196b4dbccf7c2c73739986749bb98cd95ddd98
4
+ data.tar.gz: 9e6b4e88f48d8b07bf82565349c689bbb439c1c3
5
+ SHA512:
6
+ metadata.gz: 0a9c14464bc5a31d7066fd9f69ef076cdb54b412c675d54265dc8c10ccdf343ef9fb322b183ad25759a6ab9aab1b6b54a1bd499c39f2261fbd1356d4d17e6b54
7
+ data.tar.gz: b6e2fcfca4bdbdbfb32cee9a3279781181ec733ca666323cf14e802b061e8cc333cac4978774abfe3d211b5b340333ac006e7829efcfd6007e5275f9455a50a7
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ .idea
3
+ .ruby-version
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # simple_switch Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_switch.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
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/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Simple Switch
2
+
3
+ Simple Feature Switch Engine
4
+
5
+ [![Code Climate](https://codeclimate.com/github/Sen-Zhang/simple_switch/badges/gpa.svg)](https://codeclimate.com/github/Sen-Zhang/simple_switch)
6
+
7
+
8
+ ## Requirement
9
+ * Ruby 2.0+
10
+ * Rails 3.0+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'simple_switch'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install simple_switch
27
+
28
+ ## Usage
29
+
30
+ TODO
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/codeclimate.yml ADDED
@@ -0,0 +1,3 @@
1
+ engines:
2
+ rubocop:
3
+ enabled: true
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/base'
2
+
3
+ module SimpleSwitch
4
+ module Generators
5
+
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../../templates', __FILE__)
8
+
9
+ desc 'Copy initializer file simple_switch.rb to config/initializers.'
10
+
11
+ def copy_initializer
12
+ copy_file 'simple_switch.rb', 'config/initializers/simple_switch.rb'
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,17 @@
1
+ # Example:
2
+ #
3
+ # feature_foo:
4
+ # development: true
5
+ # test: true
6
+ # dev: true
7
+ # qa: true
8
+ # prodstage: true
9
+ # production: false
10
+ #
11
+ # feature_bar:
12
+ # development: true
13
+ # test: true
14
+ # dev: true
15
+ # qa: true
16
+ # prodstage: true
17
+ # production: true
@@ -0,0 +1,10 @@
1
+ SimpleSwitch.setup do |config|
2
+
3
+ # feature switch configuration yaml file stored location, by default it is stored
4
+ # under config directory
5
+ config.feature_config_file_dir = 'config'
6
+
7
+ # feature switch configuration yaml file name, by default it is 'feature_config.yml'
8
+ config.feature_config_file_name = 'feature_config.yml'
9
+
10
+ end
@@ -0,0 +1,26 @@
1
+ require 'simple_switch/version'
2
+
3
+ module SimpleSwitch
4
+ mattr_accessor :feature_config_file_dir
5
+ @@feature_config_file_name = 'config'
6
+
7
+ mattr_accessor :feature_config_file_name
8
+ @@feature_config_file_name = 'feature_config.yml'
9
+
10
+ def self.setup
11
+ yield self
12
+ end
13
+
14
+ def self.feature_manager
15
+ SimpleSwitch::Switch.instance
16
+ end
17
+
18
+ class Engine < ::Rails::Engine
19
+ end
20
+ end
21
+
22
+ require 'simple_switch/switch'
23
+ require 'simple_switch/shared_methods'
24
+ require 'simple_switch/shared_controller_methods'
25
+ require 'simple_switch/action_controller/base'
26
+ require 'simple_switch/active_record/base'
@@ -0,0 +1,10 @@
1
+ require 'action_controller'
2
+
3
+ module SimpleSwitch
4
+ class ActionController::Base
5
+ include SimpleSwitch::SharedMethods
6
+ include SimpleSwitch::SharedControllerMethods
7
+
8
+ helper_method :feature_on?, :feature_off?
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+
3
+ module SimpleSwitch
4
+ class ActiveRecord::Base
5
+ extend SimpleSwitch::SharedMethods
6
+ include SimpleSwitch::SharedMethods
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ module SimpleSwitch
2
+ module SharedControllerMethods
3
+
4
+ def feature_config_info
5
+ SimpleSwitch.feature_manager.feature_config
6
+ end
7
+
8
+ def update_feature(feature, env, new_value)
9
+ SimpleSwitch.feature_manager.update(feature, env, new_value)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module SimpleSwitch
2
+ module SharedMethods
3
+
4
+ def feature_on?(feature)
5
+ SimpleSwitch.feature_manager.on?(feature)
6
+ end
7
+
8
+ def feature_off?(feature)
9
+ !feature_on?(feature)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,77 @@
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
@@ -0,0 +1,3 @@
1
+ module SimpleSwitch
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,24 @@
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
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_switch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sen Zhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-03 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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
+ description: Simple Feature Switch Engine
42
+ email:
43
+ - solowolf21@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - codeclimate.yml
55
+ - lib/generators/simple_switch/install_generator.rb
56
+ - lib/generators/simple_switch/install_yaml_generator.rb
57
+ - lib/generators/templates/feature_config_sample.yml
58
+ - lib/generators/templates/simple_switch.rb
59
+ - lib/simple_switch.rb
60
+ - lib/simple_switch/action_controller/base.rb
61
+ - lib/simple_switch/active_record/base.rb
62
+ - lib/simple_switch/shared_controller_methods.rb
63
+ - lib/simple_switch/shared_methods.rb
64
+ - lib/simple_switch/switch.rb
65
+ - lib/simple_switch/version.rb
66
+ - simple_switch.gemspec
67
+ homepage: https://github.com/Sen-Zhang/simple_switch
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: 2.0.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.4.8
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Simple Feature Switch Engine
91
+ test_files: []
92
+ has_rdoc: