class Obfusc::EncryptorCommandBase

Commom methods shared between CryptCommand and DecryptCommand models. Children models only overwrite `show_usage` and files.

Constants

COMMANDS
CURRENT_DIR

Public Class Methods

call(config, *args) click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 17
def self.call(config, *args)
  command, from, to = args
  model = new(config, from, to)
  command = 'show_usage' unless COMMANDS.include?(command)
  model.public_send(command)
end
new(config, from, to) click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 10
def initialize(config, from, to)
  @config = config

  @from = from || CURRENT_DIR
  @to = to || CURRENT_DIR
end

Public Instance Methods

copy() click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 43
def copy
  files.each_with_index do |(from, to), index|
    create_target_base_directory if index.zero?
    create_directory_from_file(to)
    @config.log("cp #{from} #{to}")
    @config.dry_run do
      FileUtils.cp(from, to, verbose: @config.verbose?)
    end
  end.size
end
files() click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 28
def files
  raise NotImplementedError
end
move() click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 32
def move
  files.each_with_index do |(from, to), index|
    create_target_base_directory if index.zero?
    create_directory_from_file(to)
    @config.log("mv #{from} #{to}")
    @config.dry_run do
      FileUtils.mv(from, to, verbose: @config.verbose?)
    end
  end.size
end
show_usage() click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 24
def show_usage
  raise NotImplementedError
end

Protected Instance Methods

create_directory_from_file(path) click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 56
def create_directory_from_file(path)
  return if File.directory?(path)
  dirname = File.dirname(path)
  return if File.expand_path(dirname) == File.expand_path(@to)

  @config.log("mkdir -p #{dirname}")
  @config.dry_run { FileUtils.mkdir_p(dirname) }
end
create_target_base_directory() click to toggle source
# File lib/obfusc/commands/concerns/encryptor_command_base.rb, line 65
def create_target_base_directory
  return if @to == CURRENT_DIR
  return if File.directory?(@to)

  @config.log("mkdir -p #{@to}")
  @config.dry_run { FileUtils.mkdir_p(@to) }
end