switches 0.1.5 → 0.1.6

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 (6) hide show
  1. data/README.rdoc +133 -0
  2. data/VERSION +1 -1
  3. data/switches.gemspec +3 -6
  4. metadata +3 -7
  5. data/README +0 -1
  6. data/README.markdown +0 -175
@@ -0,0 +1,133 @@
1
+ = switches
2
+
3
+ Switches lets you turn on and off sections of your code with <tt>Switches.foobar?</tt> booleans.
4
+
5
+ It's an extraction from [http://brighterplanet.com](http://brighterplanet.com), where we use it as an emergency button to turn on/off API integration with Facebook, Campaign Monitor, etc.
6
+
7
+ == Quick start
8
+
9
+ Add 2 lines to <tt>config/environment.rb</tt>:
10
+
11
+ require File.join(File.dirname(__FILE__), 'boot')
12
+ [...]
13
+ require 'switches' # AFTER boot, BEFORE initializer
14
+ [...]
15
+ Rails::Initializer.run do |config|
16
+ [...]
17
+ config.gem 'switches', :lib => false # INSIDE initializer
18
+
19
+ Now run this:
20
+
21
+ my-macbook:~/my_app $ ./script/runner 'Switches.setup'
22
+
23
+ Add your defaults to <tt>config/switches/defaults.yml</tt>:
24
+
25
+ ---
26
+ ssl: true # ssl support is on by default
27
+ campaign_monitor: true # campaign monitor integration is on by default
28
+
29
+ == Tasks
30
+
31
+ RAKE TASK CAP TASK NOTES
32
+ rake s:c cap TARGET s:c show current switches
33
+ rake s:d cap TARGET s:d show difference between current and default switches
34
+ rake s:on[SWITCH] cap TARGET s:on ARG=SWITCH turn on SWITCH
35
+ rake s:off[SWITCH] cap TARGET s:off ARG=SWITCH turn off SWITCH
36
+ rake s:clear[SWITCH] cap TARGET s:clear ARG=SWITCH clear any switch for SWITCH
37
+ rake s:reset cap TARGET s:reset go back to defaults (deletes config/switches/current.yml)
38
+ rake s:backup cap TARGET s:backup backup current switches (copies to config/switches/backup.yml)
39
+ rake s:restore cap TARGET s:restore restore backed-up switches (copies backup.yml to current.yml)
40
+ rake s:default cap TARGET s:default list default settings
41
+
42
+ == Throwing switches remotely with Capistrano
43
+
44
+ This is the minimum needed in the TARGET task:
45
+
46
+ task :production do
47
+ role :app, 'ec2-88-77-66-55.compute-1.amazonaws.com'
48
+ role :app, '177.133.33.144'
49
+
50
+ set :rails_env, 'production'
51
+ set :deploy_to, '/data/my_app'
52
+ set :gfs, false
53
+ end
54
+
55
+ The switches will get applied to any role that matches <tt>/app/</tt> (so :app_master, :app, etc.)
56
+
57
+ == Throwing switches before you db:migrate
58
+
59
+ I like to use Switches to turn off <tt>%w{memoization caching facebook campaign\_monitor delayed\_job}</tt> before running rake db:migrate, so I put this in <tt>lib/tasks/zzz\_rake_switches.rake</tt>:
60
+
61
+ namespace :rake_switches do
62
+ task :turn_stuff_off do
63
+ Rake::Task['s:backup'].execute
64
+ %w{memoization caching facebook campaign_monitor delayed_job}.each do |switch|
65
+ Rake::Task['s:off'].execute(Rake::TaskArguments.new([:name], [switch]))
66
+ end
67
+ end
68
+ task :turn_stuff_back_on do
69
+ Rake::Task['s:restore'].execute
70
+ Rake::Task['cache:clear'].execute
71
+ end
72
+ end
73
+
74
+ # modify what happens on db:migrate, etc.
75
+ [ 'db:migrate', 'your:task:if:it:needs:wrapping' ].each do |task_to_wrap|
76
+ Rake::Task[task_to_wrap].enhance(['rake_switches:turn_stuff_off']) do
77
+ Rake::Task['rake_switches:turn_stuff_back_on'].invoke
78
+ end
79
+ end
80
+
81
+ Note that 's:backup' and 's:restore' are not thread safe or really GFS safe, either.
82
+
83
+ == Usage
84
+
85
+ You can do stuff like (in <tt>app/models/user.rb</tt>):
86
+
87
+ after_create :subscribe_email if Switches.campaign_monitor?
88
+ def subscribe_email
89
+ CampaignMonitor.subscribe email
90
+ end
91
+
92
+ Uhh ohh! Campaign Monitor's API is down and you need to shut off those failing after_creates, like, NOW.
93
+
94
+ production-server-1:/var/www/apps/my_app $ rake s:off[campaign_monitor]
95
+ production-server-1:/var/www/apps/my_app $ sudo monit restart all -g my_app
96
+ [...]
97
+ production-server-2:/var/www/apps/my_app $ rake s:off[campaign_monitor]
98
+ production-server-2:/var/www/apps/my_app $ sudo monit restart all -g my_app
99
+
100
+ Or, even better, do it with cap:
101
+
102
+ my-macbook:~/my_app $ cap production s:off ARG=campaign_monitor
103
+ my-macbook:~/my_app $ cap production mongrel:restart
104
+
105
+ For another example, let's say you're a developer who doesn't have a self-signed certificate:
106
+
107
+ my-macbook:~/my_app $ rake s:off[ssl]
108
+
109
+ Those changes get persisted in <tt>config/switches/current.yml</tt>.
110
+
111
+ If you want to see your switches vis-a-vis the defaults:
112
+
113
+ my-macbook:~/my_app $ rake s:d
114
+ ---
115
+ ssl: true => false
116
+
117
+ And if you want to go back to the defaults:
118
+
119
+ my-macbook:~/my_app $ rake s:reset
120
+
121
+ Remember, you should version control <tt>config/switches/defaults.yml</tt> and ignore <tt>config/switches/current.yml</tt>.
122
+
123
+ == Rationale
124
+
125
+ Sometimes you just need an easy way to "turn off" code.
126
+
127
+ == Wishlist
128
+
129
+ + ?
130
+
131
+ == Copyright
132
+
133
+ Copyright (c) 2009 Seamus Abshere. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{switches}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Seamus Abshere"]
@@ -37,16 +37,13 @@ It's inspired by ActiveSupport's StringInquirer (e.g. Rails.development?) and tr
37
37
  s.email = %q{seamus@abshere.net}
38
38
  s.extra_rdoc_files = [
39
39
  "LICENSE",
40
- "README",
41
- "README.html",
42
- "README.markdown"
40
+ "README.rdoc"
43
41
  ]
44
42
  s.files = [
45
43
  ".document",
46
44
  ".gitignore",
47
45
  "LICENSE",
48
- "README",
49
- "README.markdown",
46
+ "README.rdoc",
50
47
  "Rakefile",
51
48
  "VERSION",
52
49
  "lib/switches.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switches
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seamus Abshere
@@ -55,15 +55,12 @@ extensions: []
55
55
 
56
56
  extra_rdoc_files:
57
57
  - LICENSE
58
- - README
59
- - README.html
60
- - README.markdown
58
+ - README.rdoc
61
59
  files:
62
60
  - .document
63
61
  - .gitignore
64
62
  - LICENSE
65
- - README
66
- - README.markdown
63
+ - README.rdoc
67
64
  - Rakefile
68
65
  - VERSION
69
66
  - lib/switches.rb
@@ -73,7 +70,6 @@ files:
73
70
  - spec/spec_helper.rb
74
71
  - spec/switches_spec.rb
75
72
  - switches.gemspec
76
- - README.html
77
73
  has_rdoc: true
78
74
  homepage: http://github.com/seamusabshere/switches
79
75
  licenses: []
data/README DELETED
@@ -1 +0,0 @@
1
- Please see README.markdown or visit http://github.com/seamusabshere/switches
@@ -1,175 +0,0 @@
1
- # switches #
2
-
3
- Switches lets you turn on and off sections of your code with <tt>Switches.foobar?</tt> booleans.
4
-
5
- It's an extraction from [http://brighterplanet.com](http://brighterplanet.com), where we use it as an emergency button to turn on/off API integration with Facebook, Campaign Monitor, etc.
6
-
7
- ## Quick start ##
8
-
9
- Add 2 lines to <tt>config/environment.rb</tt>:
10
-
11
- require File.join(File.dirname(__FILE__), 'boot')
12
- [...]
13
- require 'switches' # AFTER boot, BEFORE initializer
14
- [...]
15
- Rails::Initializer.run do |config|
16
- [...]
17
- config.gem 'switches', :lib => false # INSIDE initializer
18
-
19
- Now run this:
20
-
21
- my-macbook:~/my_app $ ./script/runner 'Switches.setup'
22
-
23
- Add your defaults to <tt>config/switches/defaults.yml</tt>:
24
-
25
- ---
26
- ssl: true # ssl support is on by default
27
- campaign_monitor: true # campaign monitor integration is on by default
28
-
29
- ## Tasks ##
30
-
31
- <table>
32
- <tr>
33
- <th>Rake task</th>
34
- <th>Cap task</th>
35
- <th>Notes</th>
36
- </tr>
37
- <tr>
38
- <td>rake s:c</td>
39
- <td>cap TARGET s:c</td>
40
- <td>show current switches</td>
41
- </tr>
42
- <tr>
43
- <td>rake s:d</td>
44
- <td>cap TARGET s:d</td>
45
- <td>show difference between current and default switches</td>
46
- </tr>
47
- <tr>
48
- <td>rake s:on[SWITCH]</td>
49
- <td>cap TARGET s:on ARG=SWITCH</td>
50
- <td>turn on SWITCH</td>
51
- </tr>
52
- <tr>
53
- <td>rake s:off[SWITCH]</td>
54
- <td>cap TARGET s:off ARG=SWITCH</td>
55
- <td>turn off SWITCH</td>
56
- </tr>
57
- <tr>
58
- <td>rake s:clear[SWITCH]</td>
59
- <td>cap TARGET s:clear ARG=SWITCH</td>
60
- <td>clear any switch for SWITCH</td>
61
- </tr>
62
- <tr>
63
- <td>rake s:reset</td>
64
- <td>cap TARGET s:reset</td>
65
- <td>go back to defaults (deletes <tt>config/switches/current.yml</tt>)</td>
66
- </tr>
67
- <tr>
68
- <td>rake s:backup</td>
69
- <td>cap TARGET s:backup</td>
70
- <td>backup current switches (copies to <tt>config/switches/backup.yml</tt>)</td>
71
- </tr>
72
- <tr>
73
- <td>rake s:restore</td>
74
- <td>cap TARGET s:restore</td>
75
- <td>restore backed-up switches (copies <tt>backup.yml</tt> to <tt>current.yml</tt>)</td>
76
- </tr>
77
- <tr>
78
- <td>rake s:default</td>
79
- <td>cap TARGET s:default</td>
80
- <td>list default settings</td>
81
- </tr>
82
- </table>
83
-
84
- ## Throwing switches remotely with Capistrano ##
85
-
86
- This is the minimum needed in the TARGET task:
87
-
88
- task :production do
89
- role :app, 'ec2-88-77-66-55.compute-1.amazonaws.com'
90
- role :app, '177.133.33.144'
91
-
92
- set :rails_env, 'production'
93
- set :deploy_to, '/data/my_app'
94
- set :gfs, false
95
- end
96
-
97
- The switches will get applied to any role that matches <tt>/app/</tt> (so :app_master, :app, etc.)
98
-
99
- ## Throwing switches before you db:migrate ##
100
-
101
- I like to use Switches to turn off <tt>%w{memoization caching facebook campaign\_monitor delayed\_job}</tt> before running rake db:migrate, so I put this in <tt>lib/tasks/zzz\_rake_switches.rake</tt>:
102
-
103
- namespace :rake_switches do
104
- task :turn_stuff_off do
105
- Rake::Task['s:backup'].execute
106
- %w{memoization caching facebook campaign_monitor delayed_job}.each do |switch|
107
- Rake::Task['s:off'].execute(Rake::TaskArguments.new([:name], [switch]))
108
- end
109
- end
110
- task :turn_stuff_back_on do
111
- Rake::Task['s:restore'].execute
112
- Rake::Task['cache:clear'].execute
113
- end
114
- end
115
-
116
- # modify what happens on db:migrate, etc.
117
- [ 'db:migrate', 'your:task:if:it:needs:wrapping' ].each do |task_to_wrap|
118
- Rake::Task[task_to_wrap].enhance(['rake_switches:turn_stuff_off']) do
119
- Rake::Task['rake_switches:turn_stuff_back_on'].invoke
120
- end
121
- end
122
-
123
- Note that 's:backup' and 's:restore' are not thread safe or really GFS safe, either.
124
-
125
- ## Usage ##
126
-
127
- You can do stuff like (in <tt>app/models/user.rb</tt>):
128
-
129
- after_create :subscribe_email if Switches.campaign_monitor?
130
- def subscribe_email
131
- CampaignMonitor.subscribe email
132
- end
133
-
134
- Uhh ohh! Campaign Monitor's API is down and you need to shut off those failing after_creates, like, NOW.
135
-
136
- production-server-1:/var/www/apps/my_app $ rake s:off[campaign_monitor]
137
- production-server-1:/var/www/apps/my_app $ sudo monit restart all -g my_app
138
- [...]
139
- production-server-2:/var/www/apps/my_app $ rake s:off[campaign_monitor]
140
- production-server-2:/var/www/apps/my_app $ sudo monit restart all -g my_app
141
-
142
- Or, even better, do it with cap:
143
-
144
- my-macbook:~/my_app $ cap production s:off ARG=campaign_monitor
145
- my-macbook:~/my_app $ cap production mongrel:restart
146
-
147
- For another example, let's say you're a developer who doesn't have a self-signed certificate:
148
-
149
- my-macbook:~/my_app $ rake s:off[ssl]
150
-
151
- Those changes get persisted in <tt>config/switches/current.yml</tt>.
152
-
153
- If you want to see your switches vis-a-vis the defaults:
154
-
155
- my-macbook:~/my_app $ rake s:d
156
- ---
157
- ssl: true => false
158
-
159
- And if you want to go back to the defaults:
160
-
161
- my-macbook:~/my_app $ rake s:reset
162
-
163
- Remember, you should version control <tt>config/switches/defaults.yml</tt> and ignore <tt>config/switches/current.yml</tt>.
164
-
165
- ## Rationale ##
166
-
167
- Sometimes you just need an easy way to "turn off" code.
168
-
169
- ## Wishlist ##
170
-
171
- + ?
172
-
173
- ## Copyright ##
174
-
175
- Copyright (c) 2009 Seamus Abshere. See LICENSE for details.