class Swappy::Commands::Swap

Constants

ConfigSetNotFoundError

Attributes

name[R]

Public Class Methods

new(options={}) click to toggle source
Calls superclass method Swappy::Commands::Command::new
# File lib/swappy/commands/swap.rb, line 10
def initialize(options={})
  super options
  @name = options[:name]
  raise ConfigSetNotFoundError unless config_set
end

Public Instance Methods

call() click to toggle source
# File lib/swappy/commands/swap.rb, line 16
def call
  switch_to_config_set
end

Private Instance Methods

backup(link_path) click to toggle source
# File lib/swappy/commands/swap.rb, line 31
def backup(link_path)
  if File.exists?(link_path)
    backup_path = backup_path(link_path)
    puts "backing up #{File.symlink?(link_path) ? 'symlink ' : ' '}#{link_path} to #{backup_path}"
    File.rename(link_path, backup_path)
  end
end
backup_path(link_path) click to toggle source
# File lib/swappy/commands/swap.rb, line 48
def backup_path(link_path)
  timestamp = Time.now.to_i.to_s
  new_basename = "#{File.basename(link_path)}.backup.#{timestamp}"
  Pathname.new(File.join(File.dirname(link_path), new_basename))
end
config_set() click to toggle source
# File lib/swappy/commands/swap.rb, line 44
def config_set
  @config_set ||= app_config.find_config_set_by_name(name)
end
switch_to_config_set() click to toggle source
# File lib/swappy/commands/swap.rb, line 22
def switch_to_config_set
  puts "Switching to config set #{config_set.name}..."
  config_set.each do |config|
    backup(config.link_path)
    link(config.source_path, config.link_path)
  end
  puts "Done!"
end