lib/switches.rb in switches-0.1.3 vs lib/switches.rb in switches-0.1.4
- old
+ new
@@ -1,8 +1,8 @@
require 'yaml'
-require 'rubygems'
-require 'activesupport'
+require 'fileutils'
+require 'active_support'
# TODO not agnostic, expects RAILS_ROOT
module Switches
CONFIG_DIR = File.join RAILS_ROOT, 'config', 'switches'
@@ -11,19 +11,18 @@
CAPISTRANO_LOAD_PATH = CAPISTRANO_PATH.gsub "#{RAILS_ROOT}/", '' # => 'config/switches/capistrano_tasks.rb'
CAPFILE_PATH = File.join RAILS_ROOT, 'Capfile'
CURRENT_PATH = File.join CONFIG_DIR, 'current.yml'
DEFAULT_PATH = File.join CONFIG_DIR, 'default.yml'
BACKUP_PATH = File.join CONFIG_DIR, 'backup.yml'
+ TRANSACTION_PID_PATH = File.join CONFIG_DIR, 'transaction.pid'
class << self
def say(str)
$stderr.puts "[SWITCHES GEM] #{str.gsub "#{RAILS_ROOT}/", ''}"
end
def setup
- require 'fileutils'
-
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."
@@ -66,36 +65,42 @@
# taken from ActiveSupport::StringInquirer
def method_missing(method_name, *args)
suffix = method_name.to_s[-1,1]
key = method_name.to_s[0..-2]
- if suffix == "?" and current.has_key?(key)
- current[key]
+ 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
def default
return @_default unless @_default.nil?
- # say "file system read #{DEFAULT_PATH}"
- @_default = YAML.load(File.read(DEFAULT_PATH))
+ # 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
def current
return @_current unless @_current.nil?
+ resolve_transaction!
if File.exist?(CURRENT_PATH)
- # say "file system read #{CURRENT_PATH}"
- @_current = YAML.load(File.read(CURRENT_PATH))
+ # say "file system activity #{CURRENT_PATH}"
+ @_current = YAML.load(IO.read(CURRENT_PATH))
@_current.stringify_keys!
else
@_current = default.dup
end
@_current
@@ -141,23 +146,49 @@
@_current = nil
end
def backup
write_current
+ start_transaction!
+ # say "file system activity #{BACKUP_PATH}"
FileUtils.cp CURRENT_PATH, BACKUP_PATH
end
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
def write_current
current # load it first!
File.open(CURRENT_PATH, 'w') { |f| f.write current.stringify_keys.to_yaml }
+ end
+
+ 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
+
+ def resolve_transaction!
+ if transaction_pid.present? and transaction_pid != Process.pid
+ say "Resolving... calling restore"
+ restore
+ end
+ end
+
+ def start_transaction!
+ resolve_transaction!
+ say "Starting transaction"
+ File.open(TRANSACTION_PID_PATH, 'w') { |f| f.write Process.pid }
+ end
+
+ def end_transaction!
+ say "Finishing transaction"
+ FileUtils.rm_f TRANSACTION_PID_PATH
end
end
end