Class: Maid::Maid

Inherits:
Object
  • Object
show all
Includes:
Tools
Defined in:
lib/maid/maid.rb

Overview

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

Constant Summary collapse

DEFAULTS =
{
  :progname     => 'Maid',
  :log_device   => File.expand_path('~/.maid/maid.log'),
  :rules_path   => File.expand_path('~/.maid/rules.rb'),
  :trash_path   => File.expand_path('~/.Trash'),
  :file_options => {:noop => false}, # for FileUtils
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools

#dir, #disk_usage, #downloaded_from, #duration_s, #find, #git_piston, #last_accessed, #locate, #move, #trash, #zipfile_contents

Constructor Details

#initialize(options = {}) ⇒ Maid

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/.local/share/Trash/files/')


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/maid/maid.rb', line 23

def initialize(options = {})
  options = DEFAULTS.merge(options.reject { |k, v| v.nil? })

  @log_device = options[:log_device]
  FileUtils.mkdir_p(File.dirname(@log_device)) unless @log_device.kind_of?(IO)
  @logger = Logger.new(@log_device)
  @logger.progname  = options[:progname]
  @logger.formatter = options[:log_formatter] if options[:log_formatter]

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

  @rules = []
end

Instance Attribute Details

#file_optionsObject (readonly)

Returns the value of attribute file_options.



14
15
16
# File 'lib/maid/maid.rb', line 14

def file_options
  @file_options
end

#log_deviceObject (readonly)

Returns the value of attribute log_device.



14
15
16
# File 'lib/maid/maid.rb', line 14

def log_device
  @log_device
end

#rulesObject (readonly)

Returns the value of attribute rules.



14
15
16
# File 'lib/maid/maid.rb', line 14

def rules
  @rules
end

#rules_pathObject (readonly)

Returns the value of attribute rules_path.



14
15
16
# File 'lib/maid/maid.rb', line 14

def rules_path
  @rules_path
end

#trash_pathObject (readonly)

Returns the value of attribute trash_path.



14
15
16
# File 'lib/maid/maid.rb', line 14

def trash_path
  @trash_path
end

Instance Method Details

#add_rules(path) ⇒ Object

Add the rules at path.



55
56
57
58
59
60
61
62
63
# File 'lib/maid/maid.rb', line 55

def add_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

#cleanObject

Start cleaning, based on the rules defined at rules_path.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/maid/maid.rb', line 40

def clean
  unless @log_device.kind_of?(IO)
    @logger.info "v#{Maid::VERSION}"
    @logger.info 'Started'
  end

  add_rules(@rules_path)
  follow_rules

  unless @log_device.kind_of?(IO)
    @logger.info 'Finished'
  end
end

#cmd(command) ⇒ Object

Run a shell command. – Delegates to Kernel.‘. Made primarily for testing other commands and some error handling.



81
82
83
84
85
86
87
# File 'lib/maid/maid.rb', line 81

def cmd(command) #:nodoc:
  if supported_command?(command)
    %x(#{command})
  else
    raise ArgumentError, "Unsupported system command: #{command.inspect}"
  end
end

#follow_rulesObject

Follow all registered rules.



71
72
73
74
75
76
# File 'lib/maid/maid.rb', line 71

def follow_rules
  @rules.each do |rule|
    @logger.info("Rule: #{rule.description}")
    rule.follow
  end
end

#rule(description, &instructions) ⇒ Object

Register a rule with a description and instructions (lambda function).



66
67
68
# File 'lib/maid/maid.rb', line 66

def rule(description, &instructions)
  @rules << ::Maid::Rule.new(description, instructions)
end