paddock 0.0.1

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 (5) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +56 -0
  3. data/lib/paddock.rb +60 -0
  4. data/spec/paddock_spec.rb +154 -0
  5. metadata +65 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Pivotal Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # Paddock
2
+
3
+ "...even Nedry knew better than to mess with the raptor fences."
4
+
5
+ Paddock is a feature switch system.
6
+
7
+ Define which features should be used in certain environments.
8
+
9
+ ## Definition
10
+
11
+ Put this somewhere like: `config/initializers/paddock.rb`
12
+
13
+ Paddock(Rails.env) do
14
+ enable :phone_system, :in => [:development, :test]
15
+ enable :door_locks, :in => :development
16
+ enable :raptor_fences
17
+ disable :cryo_security
18
+ disable :tyranosaur_fences, :in => :production
19
+ end
20
+
21
+ You name it, we got it.
22
+
23
+ ## Usage
24
+
25
+ # Check if feature is enabled
26
+ if feature(:perimeter_fence)
27
+ # do work
28
+ end
29
+
30
+ # Guard a block
31
+ feature(:perimeter_fence) do
32
+ # do work
33
+ end
34
+
35
+ This is a unix system. I know this.
36
+
37
+ ## Testing
38
+
39
+ You can define which features are enabled in a test:
40
+
41
+ before(:each) do
42
+ Paddock.enable :feature_i_am_testing
43
+ end
44
+
45
+ You think that kind of automation is easy? Or cheap?
46
+
47
+ ## Authors
48
+
49
+ We're not computer nerds. We prefer to be called "hackers."
50
+
51
+ * Pat Nakajima
52
+ * Brandon Keene
53
+
54
+ ![system secured](http://ak2.static.dailymotion.com/static/video/024/680/8086420:jpeg_preview_large.jpg)
55
+
56
+ (c) Copyright 2010 Pivotal Labs, see LICENSE
@@ -0,0 +1,60 @@
1
+ module Paddock
2
+ class FeatureNotFound < StandardError
3
+ end
4
+
5
+ class Feature
6
+ def self.add(name, envs, disabled=false)
7
+ @features ||= {}
8
+ @features[name] = new(name, envs, disabled)
9
+ end
10
+
11
+ def self.get(name)
12
+ (@features ||= {})[name] || raise(Paddock::FeatureNotFound.new("#{name} is not a valid feature."))
13
+ end
14
+
15
+ def initialize(name, envs, disabled)
16
+ @name, @envs, @disabled = name, envs, disabled
17
+ end
18
+
19
+ def enabled?
20
+ result = true if @envs == :all
21
+ result ||= Array(@envs).include?(Paddock.environment.to_sym)
22
+ @disabled ? (!result) : result
23
+ end
24
+ end
25
+
26
+ def self.environment=(env)
27
+ @environment = env
28
+ end
29
+
30
+ def self.environment
31
+ @environment
32
+ end
33
+
34
+ def self.features(&block)
35
+ instance_eval(&block)
36
+ end
37
+
38
+ def self.enable(name, options={})
39
+ Paddock::Feature.add(name, (options[:in] || :all))
40
+ end
41
+
42
+ def self.disable(name, options={})
43
+ Paddock::Feature.add(name, (options[:in] || :all), :disabled)
44
+ end
45
+
46
+ def feature(name)
47
+ enabled = Paddock::Feature.get(name).enabled?
48
+ enabled && yield if block_given?
49
+ enabled
50
+ end
51
+ end
52
+
53
+ def Paddock(env, &block)
54
+ if block_given?
55
+ ::Paddock.environment = env
56
+ ::Paddock.features(&block)
57
+ else
58
+ ::Paddock
59
+ end
60
+ end
@@ -0,0 +1,154 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib paddock])
2
+
3
+ describe Paddock('development') do
4
+ include Paddock
5
+
6
+ it "has an environment" do
7
+ Paddock.environment = "development"
8
+ Paddock.environment.should == "development"
9
+ end
10
+
11
+ it "raises a FeatureNotFound error when the feature don't not exist none" do
12
+ proc {
13
+ feature(:raptor_fences)
14
+ }.should raise_error(Paddock::FeatureNotFound)
15
+ end
16
+
17
+ describe "defining enabled features" do
18
+ context "when not specifying environments" do
19
+ before(:each) do
20
+ Paddock('development') do
21
+ enable :perimeter_fence
22
+ end
23
+ end
24
+
25
+ it "runs the feature block always" do
26
+ called = false
27
+ feature(:perimeter_fence) { called = true }
28
+ called.should be_true
29
+ end
30
+
31
+ it "always returns true on feature check" do
32
+ feature(:perimeter_fence).should be_true
33
+ end
34
+ end
35
+
36
+ context "when specifying environment" do
37
+ before(:each) do
38
+ Paddock('development') do
39
+ enable :perimeter_fence, :in => :development
40
+ end
41
+ end
42
+
43
+ context "when in appropriate environment" do
44
+ before(:each) do
45
+ Paddock.environment = 'development'
46
+ end
47
+
48
+ it "runs the feature block" do
49
+ called = false
50
+ feature(:perimeter_fence) { called = true }
51
+ called.should be_true
52
+ end
53
+
54
+ it "returns true from the feature check" do
55
+ feature(:perimeter_fence).should be_true
56
+ end
57
+ end
58
+ end
59
+
60
+ context "when specifying multiple environments" do
61
+ before(:each) do
62
+ Paddock('development') do
63
+ enable :perimeter_fence, :in => [:development, :test]
64
+ end
65
+ end
66
+
67
+ context "when in appropriate environment" do
68
+ before(:each) do
69
+ Paddock.environment = 'development'
70
+ end
71
+
72
+ it "runs the feature block" do
73
+ called = false
74
+ feature(:perimeter_fence) { called = true }
75
+ called.should be_true
76
+ end
77
+
78
+ it "returns true from the feature check" do
79
+ feature(:perimeter_fence).should be_true
80
+ end
81
+ end
82
+
83
+ context "when not in appropriate environment" do
84
+ before(:each) do
85
+ Paddock.environment = 'production'
86
+ end
87
+
88
+ it "does not run the feature block when not in an appropriate environment" do
89
+ called = false
90
+ feature(:perimeter_fence) { called = true }
91
+ called.should be_false
92
+ end
93
+
94
+ it "returns false from the feature check" do
95
+ feature(:perimeter_fence).should be_false
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "disabling features" do
102
+ before(:each) do
103
+ Paddock('development') do
104
+ disable :perimeter_fence
105
+ end
106
+ end
107
+
108
+ it "does not run the feature block when not in an appropriate environment" do
109
+ called = false
110
+ feature(:perimeter_fence) { called = true }
111
+ called.should be_false
112
+ end
113
+
114
+ it "returns false from the feature check" do
115
+ feature(:perimeter_fence).should be_false
116
+ end
117
+ end
118
+
119
+ describe "disabling features in certain environments" do
120
+ before(:each) do
121
+ Paddock('production') do
122
+ disable :perimeter_fence, :in => :production
123
+ end
124
+ end
125
+
126
+ it "does not run the feature block when not in an appropriate environment" do
127
+ called = false
128
+ feature(:perimeter_fence) { called = true }
129
+ called.should be_false
130
+ end
131
+
132
+ it "returns false from the feature check" do
133
+ feature(:perimeter_fence).should be_false
134
+ end
135
+ end
136
+
137
+ describe "using disabled features in different environment" do
138
+ before(:each) do
139
+ Paddock('development') do
140
+ disable :perimeter_fence, :in => :production
141
+ end
142
+ end
143
+
144
+ it "does not run the feature block when not in an appropriate environment" do
145
+ called = false
146
+ feature(:perimeter_fence) { called = true }
147
+ called.should be_true
148
+ end
149
+
150
+ it "returns false from the feature check" do
151
+ feature(:perimeter_fence).should be_true
152
+ end
153
+ end
154
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paddock
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Pat Nakajima & Brandon Keene
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-05 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: nyc+github@pivotallabs.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - LICENSE
31
+ - README.md
32
+ - lib/paddock.rb
33
+ - spec/paddock_spec.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/pivotal-nyc/paddock
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Paddock is an environment-based feature switch system.
64
+ test_files: []
65
+