padlock 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +64 -0
  3. data/lib/padlock.rb +69 -0
  4. data/spec/padlock_spec.rb +211 -0
  5. metadata +70 -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,64 @@
1
+ # Padlock
2
+
3
+ "...even Nedry knew better than to mess with the raptor fences."
4
+
5
+ Padlock is a component switch system.
6
+
7
+ Define which components should be used in certain environments.
8
+
9
+ Padlock is a fork of Paddock, but uses the term component instead of feature to reduce domain collisions.
10
+
11
+ ## Setup
12
+
13
+ Put this somewhere like: `config/initializers/padlock.rb`
14
+
15
+ include Padlock
16
+
17
+ Padlock(Rails.env) do
18
+ enable :phone_system, :in => [:development, :test]
19
+ enable :door_locks, :in => :development
20
+ enable :raptor_fences
21
+ disable :cryo_security
22
+ disable :tyranosaur_fences, :in => :production
23
+ end
24
+
25
+ You name it, we got it.
26
+
27
+ ## Usage
28
+
29
+ # Check if component is enabled
30
+ if component(:perimeter_fence)
31
+ # do work
32
+ end
33
+
34
+ # Guard a block
35
+ component(:perimeter_fence) do
36
+ # do work
37
+ end
38
+
39
+ This is a unix system. I know this.
40
+
41
+ ## Testing
42
+
43
+ This might need some work.
44
+
45
+ You can define which components are enabled in a test:
46
+
47
+ before(:each) do
48
+ Padlock.enable :component_i_am_testing
49
+ end
50
+
51
+ You think that kind of automation is easy? Or cheap?
52
+
53
+ ## Authors
54
+
55
+ We're not computer nerds. We prefer to be called "hackers."
56
+
57
+ * Pat Nakajima
58
+ * Brandon Keene
59
+
60
+ I prefer not to be too closely associated with the aforementioned "hackers."
61
+
62
+ * Todd Persen
63
+
64
+ (c) Copyright 2010 Pivotal Labs, see LICENSE
@@ -0,0 +1,69 @@
1
+ module Padlock
2
+ class ComponentNotFound < StandardError
3
+ end
4
+
5
+ class Component
6
+ def self.add(name, envs, disabled=false)
7
+ @components ||= {}
8
+ @components[name] = new(name, envs, disabled)
9
+ end
10
+
11
+ def self.get(name)
12
+ (@components ||= {})[name] || raise(Padlock::ComponentNotFound.new("#{name} is not a valid component."))
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).map { |env| env.to_sym }.include?(Padlock.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.components(&block)
35
+ @block = block
36
+ instance_eval(&block)
37
+ end
38
+
39
+ def self.reset!
40
+ if block = @block
41
+ components(&block)
42
+ else
43
+ raise "No padlock block defined!"
44
+ end
45
+ end
46
+
47
+ def self.enable(name, options={})
48
+ Padlock::Component.add(name, (options[:in] || :all))
49
+ end
50
+
51
+ def self.disable(name, options={})
52
+ Padlock::Component.add(name, (options[:in] || :all), :disabled)
53
+ end
54
+
55
+ def component(name)
56
+ enabled = Padlock::Component.get(name).enabled?
57
+ enabled && yield if block_given?
58
+ enabled
59
+ end
60
+ end
61
+
62
+ def Padlock(env, &block)
63
+ if block_given?
64
+ ::Padlock.environment = env
65
+ ::Padlock.components(&block)
66
+ else
67
+ ::Padlock
68
+ end
69
+ end
@@ -0,0 +1,211 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib padlock])
2
+
3
+ describe Padlock('development') do
4
+ include Padlock
5
+
6
+ it "has an environment" do
7
+ Padlock.environment = "development"
8
+ Padlock.environment.should == "development"
9
+ end
10
+
11
+ it "raises a ComponentNotFound error when the component don't not exist none" do
12
+ proc {
13
+ component(:raptor_fences)
14
+ }.should raise_error(Padlock::ComponentNotFound)
15
+ end
16
+
17
+ describe "defining enabled components" do
18
+ context "when not specifying environments" do
19
+ before(:each) do
20
+ Padlock('development') do
21
+ enable :perimeter_fence
22
+ end
23
+ end
24
+
25
+ it "runs the component block always" do
26
+ called = false
27
+ component(:perimeter_fence) { called = true }
28
+ called.should be_true
29
+ end
30
+
31
+ it "always returns true on component check" do
32
+ component(:perimeter_fence).should be_true
33
+ end
34
+ end
35
+
36
+ context "when specifying environment" do
37
+ before(:each) do
38
+ Padlock('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
+ Padlock.environment = 'development'
46
+ end
47
+
48
+ it "runs the component block" do
49
+ called = false
50
+ component(:perimeter_fence) { called = true }
51
+ called.should be_true
52
+ end
53
+
54
+ it "returns true from the component check" do
55
+ component(:perimeter_fence).should be_true
56
+ end
57
+
58
+ it "is indifferent to strings/symbols" do
59
+ Padlock('development') do
60
+ enable :perimeter_fence, :in => 'development'
61
+ end
62
+
63
+ called = false
64
+ component(:perimeter_fence) { called = true }
65
+ called.should be_true
66
+ end
67
+ end
68
+ end
69
+
70
+ context "when specifying multiple environments" do
71
+ before(:each) do
72
+ Padlock('development') do
73
+ enable :perimeter_fence, :in => [:development, :test]
74
+ end
75
+ end
76
+
77
+ context "when in appropriate environment" do
78
+ before(:each) do
79
+ Padlock.environment = 'development'
80
+ end
81
+
82
+ it "runs the component block" do
83
+ called = false
84
+ component(:perimeter_fence) { called = true }
85
+ called.should be_true
86
+ end
87
+
88
+ it "returns true from the component check" do
89
+ component(:perimeter_fence).should be_true
90
+ end
91
+
92
+ it "is indifferent to strings/symbols" do
93
+ Padlock('development') do
94
+ enable :perimeter_fence, :in => %w[development test]
95
+ end
96
+
97
+ called = false
98
+ component(:perimeter_fence) { called = true }
99
+ called.should be_true
100
+ end
101
+ end
102
+
103
+ context "when not in appropriate environment" do
104
+ before(:each) do
105
+ Padlock.environment = 'production'
106
+ end
107
+
108
+ it "does not run the component block when not in an appropriate environment" do
109
+ called = false
110
+ component(:perimeter_fence) { called = true }
111
+ called.should be_false
112
+ end
113
+
114
+ it "returns false from the component check" do
115
+ component(:perimeter_fence).should be_false
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ describe "disabling components" do
122
+ before(:each) do
123
+ Padlock('development') do
124
+ disable :perimeter_fence
125
+ end
126
+ end
127
+
128
+ it "does not run the component block when not in an appropriate environment" do
129
+ called = false
130
+ component(:perimeter_fence) { called = true }
131
+ called.should be_false
132
+ end
133
+
134
+ it "returns false from the component check" do
135
+ component(:perimeter_fence).should be_false
136
+ end
137
+ end
138
+
139
+ describe "disabling components in certain environments" do
140
+ before(:each) do
141
+ Padlock('production') do
142
+ disable :perimeter_fence, :in => :production
143
+ end
144
+ end
145
+
146
+ it "does not run the component block when not in an appropriate environment" do
147
+ called = false
148
+ component(:perimeter_fence) { called = true }
149
+ called.should be_false
150
+ end
151
+
152
+ it "returns false from the component check" do
153
+ component(:perimeter_fence).should be_false
154
+ end
155
+
156
+ it "is indifferent to strings/symbols" do
157
+ Padlock('development') do
158
+ disable :perimeter_fence, :in => 'production'
159
+ end
160
+
161
+ called = false
162
+ component(:perimeter_fence) { called = true }
163
+ called.should be_true
164
+ end
165
+ end
166
+
167
+ describe "using disabled components in different environment" do
168
+ before(:each) do
169
+ Padlock('development') do
170
+ disable :perimeter_fence, :in => :production
171
+ end
172
+ end
173
+
174
+ it "does not run the component block when not in an appropriate environment" do
175
+ called = false
176
+ component(:perimeter_fence) { called = true }
177
+ called.should be_true
178
+ end
179
+
180
+ it "returns false from the component check" do
181
+ component(:perimeter_fence).should be_true
182
+ end
183
+
184
+ it "is indifferent to strings/symbols" do
185
+ Padlock('development') do
186
+ disable :perimeter_fence, :in => %w[production other]
187
+ end
188
+
189
+ called = false
190
+ component(:perimeter_fence) { called = true }
191
+ called.should be_true
192
+ end
193
+ end
194
+
195
+ describe "resetting changes" do
196
+ before(:each) do
197
+ Padlock('development') do
198
+ disable :disabled_component
199
+ enable :enabled_component
200
+ end
201
+ end
202
+
203
+ it "restores original component behavior" do
204
+ Padlock.disable :enabled_component
205
+ component(:enabled_component).should be_false
206
+
207
+ Padlock.reset!
208
+ component(:enabled_component).should be_true
209
+ end
210
+ end
211
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: padlock
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Pat Nakajima, Brandon Keene & Todd Persen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-18 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: todd@pivotallabs.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - LICENSE
32
+ - README.md
33
+ - lib/padlock.rb
34
+ - spec/padlock_spec.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/toddboom/padlock
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Padlock is an environment-based component switch system.
69
+ test_files: []
70
+