if_statement 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +50 -0
- data/Rakefile +25 -0
- data/if_statement.gemspec +20 -0
- data/install.rb +1 -0
- data/lib/if_statement/features.rb +26 -0
- data/lib/if_statement/import.rb +6 -0
- data/lib/if_statement/version.rb +3 -0
- data/lib/if_statement.rb +42 -0
- data/rails/init.rb +2 -0
- data/spec/if_statement_features_spec.rb +81 -0
- data/spec/if_statement_spec.rb +61 -0
- data/spec/spec_helper.rb +1 -0
- data/uninstall.rb +1 -0
- metadata +99 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# IfStatement
|
2
|
+
|
3
|
+
> a colossally stupid Rails feature flipper plugin
|
4
|
+
|
5
|
+
IfStatement executes code based on whether it meets some condition. A condition evaluates to false by default. The evaluation can be done in models, controllers, helpers, or anywhere that includes `IfStatement::Features`.
|
6
|
+
|
7
|
+
## Why
|
8
|
+
|
9
|
+
Because none of the existing ones worked exactly like I wanted. Also, I hadn't written a Rails plugin before.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
in a view
|
14
|
+
|
15
|
+
``` erb
|
16
|
+
<%= feature :nuts do %>
|
17
|
+
<ul>
|
18
|
+
<li>Almonds</li>
|
19
|
+
<li>Walnuts</li>
|
20
|
+
<li>Hazelnuts</li>
|
21
|
+
</ul>
|
22
|
+
<% end %>
|
23
|
+
```
|
24
|
+
|
25
|
+
everywhere else
|
26
|
+
|
27
|
+
``` ruby
|
28
|
+
feature :nuke do
|
29
|
+
kill_everyone!
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Setting what these thing evalute to
|
34
|
+
|
35
|
+
The best place for now would be `config/initializers/if_statement.rb`:
|
36
|
+
|
37
|
+
``` ruby
|
38
|
+
IfStatement.setup do
|
39
|
+
set(:nuts) { current_person.has_no :allergies }
|
40
|
+
set :nuke, Rails.env.production?
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Probably better bets
|
45
|
+
|
46
|
+
* [Rollout](https://github.com/jamesgolick/rollout)
|
47
|
+
* [Dolphin](https://github.com/grillpanda/dolphin)
|
48
|
+
* [Feature Flipper](https://github.com/qype/feature_flipper)
|
49
|
+
|
50
|
+
Copyright (c) 2011 Andrew Lunny, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rdoc/task'
|
5
|
+
require 'rspec/core'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
require 'bundler/gem_helper'
|
8
|
+
Bundler::GemHelper.install_tasks
|
9
|
+
|
10
|
+
desc 'Default: run unit tests.'
|
11
|
+
task :default => :spec
|
12
|
+
|
13
|
+
desc 'Run the if_statement specs'
|
14
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
15
|
+
t.pattern = './spec/**/*_spec.rb'
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'Generate documentation for the if_statement plugin.'
|
19
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'IfStatement'
|
22
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
23
|
+
rdoc.rdoc_files.include('README')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "if_statement/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "if_statement"
|
7
|
+
s.version = IfStatement::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Andrew Lunny"]
|
10
|
+
s.email = ["alunny@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/if_statement"
|
12
|
+
s.summary = %q{Dumb Rails Feature Flipper}
|
13
|
+
s.description = %q{A feature flipper. Designed to be a Rails plugin}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec", "~> 2.6.0"
|
20
|
+
end
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module IfStatement
|
2
|
+
module Features
|
3
|
+
# executes &block if feature identifier is enabled
|
4
|
+
def feature identifier, &block
|
5
|
+
if feature? identifier
|
6
|
+
if respond_to? :with_output_buffer
|
7
|
+
with_output_buffer &block
|
8
|
+
else
|
9
|
+
yield
|
10
|
+
end
|
11
|
+
end
|
12
|
+
#"OH HAI"
|
13
|
+
#yield if feature? identifier
|
14
|
+
end
|
15
|
+
|
16
|
+
# checks whether identifier is enabled in current env
|
17
|
+
def feature? identifier
|
18
|
+
value = IfStatement.read identifier
|
19
|
+
if value.respond_to? :call
|
20
|
+
!!instance_eval(&value)
|
21
|
+
else
|
22
|
+
value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/if_statement.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'if_statement/version'
|
2
|
+
require 'if_statement/features'
|
3
|
+
|
4
|
+
module IfStatement
|
5
|
+
class << self
|
6
|
+
# set up a bunch of features
|
7
|
+
#
|
8
|
+
# usage
|
9
|
+
# IfStatement.setup do
|
10
|
+
# set :messaging, true
|
11
|
+
# set(:admin) { current_user.admin? }
|
12
|
+
# end
|
13
|
+
def setup &block
|
14
|
+
def set identifier, value=nil, &block
|
15
|
+
define identifier, value, &block
|
16
|
+
end
|
17
|
+
|
18
|
+
class_eval(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
# define a specific feature
|
22
|
+
# give a boolean value, or a block
|
23
|
+
def define identifier, value=nil, &block
|
24
|
+
identifier = identifier.to_sym
|
25
|
+
if block
|
26
|
+
features[identifier] = block
|
27
|
+
else
|
28
|
+
features[identifier] = !!value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# reads the current value of identifier
|
33
|
+
def read identifier
|
34
|
+
value = features[identifier]
|
35
|
+
value.nil? ? false : value
|
36
|
+
end
|
37
|
+
|
38
|
+
def features
|
39
|
+
@features ||= {}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class User
|
4
|
+
include IfStatement::Features
|
5
|
+
|
6
|
+
def admin?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def one_hundred!
|
12
|
+
10 * 10
|
13
|
+
end
|
14
|
+
|
15
|
+
class View
|
16
|
+
include IfStatement::Features
|
17
|
+
|
18
|
+
def with_output_buffer
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
IfStatement.define(:dummy, true)
|
23
|
+
IfStatement.define(:count) { 7 + 5 }
|
24
|
+
IfStatement.define(:admin) { admin? }
|
25
|
+
IfStatement.define(:falsy) { true and false }
|
26
|
+
|
27
|
+
describe "IfStatement::Features" do
|
28
|
+
before do
|
29
|
+
@user = User.new
|
30
|
+
@view = View.new
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#feature?" do
|
34
|
+
it "should return the value of a Boolean feature" do
|
35
|
+
@user.feature?(:dummy).should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should evaluate the result of a block argument as truthy" do
|
39
|
+
@user.feature?(:count).should == true
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should give the block argument access to its methods" do
|
43
|
+
@user.feature?(:admin).should == true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#feature" do
|
48
|
+
describe "when the feature is enabled" do
|
49
|
+
it "should call with_output_buffer if it's available" do
|
50
|
+
@view.should_receive(:with_output_buffer)
|
51
|
+
|
52
|
+
@view.feature(:dummy) { nil }
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should execute the block otherwise" do
|
56
|
+
actual = @user.feature :admin do
|
57
|
+
one_hundred!
|
58
|
+
end
|
59
|
+
|
60
|
+
actual.should == 100
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return nil if the feature is disabled" do
|
65
|
+
actual = @user.feature :falsy do
|
66
|
+
one_hundred!
|
67
|
+
end
|
68
|
+
|
69
|
+
actual.should be_nil
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return nil if the feature is undefined" do
|
73
|
+
actual = @user.feature :not_defined do
|
74
|
+
one_hundred!
|
75
|
+
end
|
76
|
+
|
77
|
+
actual.should be_nil
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "IfStatement" do
|
4
|
+
describe "#setup" do
|
5
|
+
it "should set Boolean arguments" do
|
6
|
+
IfStatement.setup do
|
7
|
+
set :messaging, true
|
8
|
+
end
|
9
|
+
|
10
|
+
IfStatement.features[:messaging].should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should set block arguments" do
|
14
|
+
block = Proc.new { 1 + 2 }
|
15
|
+
IfStatement.setup do
|
16
|
+
set :addition, &block
|
17
|
+
end
|
18
|
+
|
19
|
+
IfStatement.features[:addition].should == block
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#define" do
|
24
|
+
it "should accept a boolean value" do
|
25
|
+
IfStatement.define :sidebar, false
|
26
|
+
|
27
|
+
IfStatement.features[:sidebar].should_not be_nil
|
28
|
+
IfStatement.features[:sidebar].should be_false
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should accept a block" do
|
32
|
+
block = Proc.new { 1 + 2 }
|
33
|
+
IfStatement.define :addition, &block
|
34
|
+
|
35
|
+
IfStatement.features[:addition].should == block
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should force the identifier to a symbol" do
|
39
|
+
IfStatement.define 'odeo', true
|
40
|
+
|
41
|
+
IfStatement.features['odeo'].should be_nil
|
42
|
+
IfStatement.features[:odeo].should be_true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#read" do
|
47
|
+
it "should return false for nil values" do
|
48
|
+
undefined = IfStatement.read(:not_defined)
|
49
|
+
|
50
|
+
undefined.should_not be_nil
|
51
|
+
undefined.should be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return the block for block values" do
|
55
|
+
block = Proc.new { 3 + 4 }
|
56
|
+
IfStatement.define :seven, &block
|
57
|
+
|
58
|
+
IfStatement.read(:seven).should == block
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'if_statement'
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: if_statement
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Andrew Lunny
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-29 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 6
|
33
|
+
- 0
|
34
|
+
version: 2.6.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: A feature flipper. Designed to be a Rails plugin
|
38
|
+
email:
|
39
|
+
- alunny@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- if_statement.gemspec
|
52
|
+
- install.rb
|
53
|
+
- lib/if_statement.rb
|
54
|
+
- lib/if_statement/features.rb
|
55
|
+
- lib/if_statement/import.rb
|
56
|
+
- lib/if_statement/version.rb
|
57
|
+
- rails/init.rb
|
58
|
+
- spec/if_statement_features_spec.rb
|
59
|
+
- spec/if_statement_spec.rb
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
- uninstall.rb
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: http://rubygems.org/gems/if_statement
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.6.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Dumb Rails Feature Flipper
|
96
|
+
test_files:
|
97
|
+
- spec/if_statement_features_spec.rb
|
98
|
+
- spec/if_statement_spec.rb
|
99
|
+
- spec/spec_helper.rb
|