Module: Switches
- Defined in:
- lib/switches.rb
Class Method Summary collapse
- .backup ⇒ Object
- .backup_path ⇒ Object
- .capfile_path ⇒ Object
- .capistrano_load_path ⇒ Object
- .capistrano_path ⇒ Object
- .clear(name) ⇒ Object
- .config_dir ⇒ Object
- .current ⇒ Object
- .current_path ⇒ Object
- .default ⇒ Object
- .default_path ⇒ Object
- .diff ⇒ Object
- .dump(method) ⇒ Object
- .end_transaction! ⇒ Object
-
.method_missing(method_name, *args) ⇒ Object
taken from ActiveSupport::StringInquirer.
- .rake_path ⇒ Object
- .reset ⇒ Object
- .resolve_transaction! ⇒ Object
- .restore ⇒ Object
- .say(str) ⇒ Object
- .setup ⇒ Object
- .start_transaction! ⇒ Object
- .transaction_pid ⇒ Object
- .transaction_pid_path ⇒ Object
- .turn_off(name) ⇒ Object
- .turn_on(name) ⇒ Object
- .write_current ⇒ Object
Class Method Details
.backup ⇒ Object
176 177 178 179 180 181 |
# File 'lib/switches.rb', line 176 def backup write_current start_transaction! # say "file system activity #{backup_path}" FileUtils.cp current_path, backup_path end |
.backup_path ⇒ Object
31 32 33 |
# File 'lib/switches.rb', line 31 def backup_path @_backup_path ||= File.join config_dir, 'backup.yml' end |
.capfile_path ⇒ Object
22 23 24 |
# File 'lib/switches.rb', line 22 def capfile_path @_capfile_path ||= File.join root_path, 'Capfile' end |
.capistrano_load_path ⇒ Object
19 20 21 |
# File 'lib/switches.rb', line 19 def capistrano_load_path @_capistrano_load_path ||= capistrano_path.gsub "#{root_path}/", '' # => 'config/switches/capistrano_tasks.rb' end |
.capistrano_path ⇒ Object
16 17 18 |
# File 'lib/switches.rb', line 16 def capistrano_path @_capistrano_path ||= File.join config_dir, 'capistrano_tasks.rb' end |
.clear(name) ⇒ Object
165 166 167 168 169 |
# File 'lib/switches.rb', line 165 def clear(name) name = name.to_s current.delete name write_current end |
.config_dir ⇒ Object
10 11 12 |
# File 'lib/switches.rb', line 10 def config_dir @_config_dir ||= File.join root_path, 'config', 'switches' end |
.current ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/switches.rb', line 123 def current return @_current unless @_current.nil? resolve_transaction! if File.exist?(current_path) # say "file system activity #{current_path}" @_current = YAML.load(IO.read(current_path)) @_current.stringify_keys! else @_current = default.dup end @_current end |
.current_path ⇒ Object
25 26 27 |
# File 'lib/switches.rb', line 25 def current_path @_current_path ||= File.join config_dir, 'current.yml' end |
.default ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/switches.rb', line 111 def default return @_default unless @_default.nil? # say "file system activity #{default_path}" resolve_transaction! @_default = YAML.load(IO.read(default_path)) @_default.stringify_keys! rescue Errno::ENOENT say "Couldn't read defaults from #{default_path}." say "You probably want to run \"./script/runner 'Switches.setup'\"." raise $! end |
.default_path ⇒ Object
28 29 30 |
# File 'lib/switches.rb', line 28 def default_path @_default_path ||= File.join config_dir, 'default.yml' end |
.diff ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/switches.rb', line 136 def diff retval = {} current.inject(retval) do |memo, ary| k, current_v = ary default_v = default[k] memo[k] = "#{default_v.nil? ? 'nil' : default_v} => #{current_v.nil? ? 'nil' : current_v}" if default_v != current_v memo end default.inject(retval) do |memo, ary| k, default_v = ary current_v = current[k] memo[k] = "#{default_v.nil? ? 'nil' : default_v} => #{current_v.nil? ? 'nil' : current_v}" if default_v != current_v memo end retval end |
.dump(method) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/switches.rb', line 38 def dump(method) if ENV['SWITCHES_XML'] == 'true' puts send(method).to_xml else pp send(method) end end |
.end_transaction! ⇒ Object
216 217 218 219 |
# File 'lib/switches.rb', line 216 def end_transaction! say "Finishing transaction" FileUtils.rm_f transaction_pid_path end |
.method_missing(method_name, *args) ⇒ Object
taken from ActiveSupport::StringInquirer
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/switches.rb', line 93 def method_missing(method_name, *args) suffix = method_name.to_s[-1,1] key = method_name.to_s[0..-2] if suffix == "?" if current.has_key?(key) current[key] # set, so could be true or false else false # unset, so always false end elsif suffix == "=" current[key] = args.first # TEMPORARY since we're not doing a write_current here else super end end |
.rake_path ⇒ Object
13 14 15 |
# File 'lib/switches.rb', line 13 def rake_path @_rake_path ||= File.join root_path, 'lib', 'tasks', 'switches.rake' end |
.reset ⇒ Object
171 172 173 174 |
# File 'lib/switches.rb', line 171 def reset FileUtils.rm_f current_path @_current = nil end |
.resolve_transaction! ⇒ Object
203 204 205 206 207 208 |
# File 'lib/switches.rb', line 203 def resolve_transaction! if transaction_pid.present? and transaction_pid != Process.pid say "Resolving... calling restore" restore end end |
.restore ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/switches.rb', line 183 def restore if File.exist?(backup_path) FileUtils.mv backup_path, current_path else raise ArgumentError, "#{backup_path} doesn't exist." end end_transaction! @_current = nil end |
.say(str) ⇒ Object
46 47 48 |
# File 'lib/switches.rb', line 46 def say(str) $stderr.puts "[SWITCHES GEM] #{str.gsub "#{root_path}/", ''}" end |
.setup ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/switches.rb', line 50 def setup say "Making #{config_dir}." FileUtils.mkdir_p config_dir if File.exists? default_path say "Not putting an example default.yml into #{default_path} because you already have one." else say "Putting an example default.yml into #{default_path}." File.open(default_path, 'w') { |f| f.write({ 'quick_brown' => true, 'fox_jumps' => false }.to_yaml) } end say "Refreshing gem-related Rake tasks at #{rake_path}." FileUtils.cp File.join(File.dirname(__FILE__), 'tasks', 'switches.rake'), rake_path say "Refreshing gem-related Capistrano tasks at #{capistrano_path}." FileUtils.cp File.join(File.dirname(__FILE__), 'tasks', 'capistrano_tasks.rb'), capistrano_path needs_append = false if not File.exists?(capfile_path) say "Creating a Capfile and including our tasks in it." needs_append = true FileUtils.touch capfile_path elsif old_capfile = IO.read(capfile_path) and old_capfile.include?(capistrano_load_path) say "Found a Capfile that already includes our tasks. Great!" else say "I'm going to add a line to your existing Capfile. Sorry if I break anything!" needs_append = true end File.open(capfile_path, 'a') do |f| say "Appending a line that loads our Capistrano tasks to your Capfile." f.write "\n# Added by switches gem #{Time.now}\nload '#{capistrano_load_path}'\n" end if needs_append say "Don't forget to:" say "* git add #{default_path}" say "* git add #{rake_path}" say "* git ignore #{capistrano_path}" say "* git ignore #{current_path}" say "You can refresh the gem tasks with Switches.setup. It won't touch anything else." end |
.start_transaction! ⇒ Object
210 211 212 213 214 |
# File 'lib/switches.rb', line 210 def start_transaction! resolve_transaction! say "Starting transaction" File.open(transaction_pid_path, 'w') { |f| f.write Process.pid } end |
.transaction_pid ⇒ Object
198 199 200 201 |
# File 'lib/switches.rb', line 198 def transaction_pid # say "file system activity #{transaction_pid_path}" IO.readlines(transaction_pid_path).first.chomp.to_i if File.exists?(transaction_pid_path) end |
.transaction_pid_path ⇒ Object
34 35 36 |
# File 'lib/switches.rb', line 34 def transaction_pid_path @_transaction_pid_path ||= File.join config_dir, 'transaction.pid' end |
.turn_off(name) ⇒ Object
153 154 155 156 157 |
# File 'lib/switches.rb', line 153 def turn_off(name) name = name.to_s current[name] = false write_current end |
.turn_on(name) ⇒ Object
159 160 161 162 163 |
# File 'lib/switches.rb', line 159 def turn_on(name) name = name.to_s current[name] = true write_current end |
.write_current ⇒ Object
193 194 195 196 |
# File 'lib/switches.rb', line 193 def write_current current # load it first! File.open(current_path, 'w') { |f| f.write current.stringify_keys.to_yaml } end |