class Maid::Maid

Maid cleans up according to the given rules, logging what it does.

TODO: Rename to something less ambiguous, e.g. “cleaning agent”, “cleaner”, “vacuum”, etc. Having this class within the `Maid` module makes things confusing.

Constants

DEFAULTS

Attributes

file_options[R]
log_device[R]
logger[R]
repeats[R]
rules_path[R]
trash_path[R]
watches[R]

Public Class Methods

new(options = {}) click to toggle source

Make a new Maid, setting up paths for the log and trash.

Sane defaults for a log and trash path are set for Mac OS X, but they can easily be overridden like so:

Maid::Maid.new(:log_device => '/home/username/log/maid.log', :trash_path => '/home/username/my_trash')
# File lib/maid/maid.rb, line 32
def initialize(options = {})
  options = DEFAULTS.merge(options.reject { |k, v| v.nil? })

  # TODO: Refactor and simplify (see also https://github.com/benjaminoakes/maid/pull/48#discussion_r1683942)
  @logger = unless options[:logger]
    @log_device = options[:log_device]
    FileUtils.mkdir_p(File.dirname(@log_device)) unless @log_device.kind_of?(IO)
    @logger = Logger.new(@log_device, options[:log_shift_age], options[:log_shift_size])
  else
    options[:logger]
  end

  @logger.progname  = options[:progname]
  @logger.formatter = options[:log_formatter] if options[:log_formatter]

  @rules_path   = options[:rules_path]
  @trash_path   = options[:trash_path] || default_trash_path
  @file_options = options[:file_options]

  # Just in case they aren't there...
  FileUtils.mkdir_p(File.expand_path('~/.maid'))
  FileUtils.mkdir_p(@trash_path)

  @watches = []
  @repeats = []
  @rules = []
end

Public Instance Methods

clean() click to toggle source

Start cleaning, based on the rules defined at rules_path.

# File lib/maid/maid.rb, line 61
def clean
  unless @log_device.kind_of?(IO)
    @logger.info "v#{ Maid::VERSION }"
    @logger.info 'Started'
  end

  follow_rules

  unless @log_device.kind_of?(IO)
    @logger.info 'Finished'
  end
end
daemonize() click to toggle source

Daemonizes the process by starting all watches and repeats and joining the threads of the schedulers/watchers

# File lib/maid/maid.rb, line 98
def daemonize
  if @watches.empty? && @repeats.empty?
    STDERR.puts 'Cannot run daemon. Nothing to watch or repeat.'
  else
    all = @watches + @repeats
    all.each(&:run)
    trap("SIGINT") do
      # Running in a thread fixes celluloid ThreadError
      Thread.new do
        all.each(&:stop)
        exit!
      end.join
    end
    sleep
  end
end
load_rules() click to toggle source

Add the rules at rules_path.

# File lib/maid/maid.rb, line 75
def load_rules
  path = @rules_path

  Maid.with_instance(self) do
    # Using `Kernel` here to help with testability.
    #
    # `Kernel.load` must be used for non-".rb" files to be required, it seems.
    Kernel.load(path)
  end
rescue LoadError => e
  STDERR.puts e.message
end
repeat(timestring, &rules) click to toggle source
# File lib/maid/maid.rb, line 92
def repeat(timestring, &rules)
  @repeats << ::Maid::Repeat.new(self, timestring, &rules)
end
watch(path, options = {}, &rules) click to toggle source
# File lib/maid/maid.rb, line 88
def watch(path, options = {}, &rules)
  @watches << ::Maid::Watch.new(self, path, options, &rules)
end

Private Instance Methods

default_trash_path() click to toggle source
# File lib/maid/maid.rb, line 140
def default_trash_path
  # TODO: Refactor module declaration so this can be `Platform`
  if Maid::Platform.linux?
    # See the [FreeDesktop.org Trash specification](http://www.ramendik.ru/docs/trashspec.html)
    path = "#{ XDG['DATA_HOME'] }/Trash/files"
  elsif Maid::Platform.osx?
    path = File.expand_path('~/.Trash')
  else
    raise NotImplementedError, "Unknown default trash path (unsupported host OS: #{ Maid::Platform.host_os.inspect })"
  end

  "#{ path }/"
end