Class: Maid::Maid
Overview
Maid cleans up according to the given rules, logging what it does.
Constant Summary collapse
- DEFAULTS =
{ :progname => 'Maid', :log_device => File.('~/.maid/maid.log'), :rules_path => File.('~/.maid/rules.rb'), :trash_path => File.('~/.Trash'), :file_options => {:noop => false}, # for FileUtils }.freeze
Instance Attribute Summary collapse
-
#file_options ⇒ Object
readonly
Returns the value of attribute file_options.
-
#log_device ⇒ Object
readonly
Returns the value of attribute log_device.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#rules_path ⇒ Object
readonly
Returns the value of attribute rules_path.
-
#trash_path ⇒ Object
readonly
Returns the value of attribute trash_path.
Instance Method Summary collapse
-
#add_rules(path) ⇒ Object
Add the rules at path.
-
#clean ⇒ Object
Start cleaning, based on the rules defined at rules_path.
-
#cmd(command) ⇒ Object
Run a shell command.
-
#follow_rules ⇒ Object
Follow all registered rules.
-
#initialize(options = {}) ⇒ Maid
constructor
Make a new Maid, setting up paths for the log and trash.
-
#rule(description, &instructions) ⇒ Object
Register a rule with a description and instructions (lambda function).
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( = {}) = DEFAULTS.merge(.reject { |k, v| v.nil? }) @log_device = [:log_device] FileUtils.mkdir_p(File.dirname(@log_device)) unless @log_device.kind_of?(IO) @logger = Logger.new(@log_device) @logger.progname = [:progname] @logger.formatter = [:log_formatter] if [:log_formatter] @rules_path = [:rules_path] @trash_path = [:trash_path] @file_options = [:file_options] @rules = [] end |
Instance Attribute Details
#file_options ⇒ Object (readonly)
Returns the value of attribute file_options.
14 15 16 |
# File 'lib/maid/maid.rb', line 14 def @file_options end |
#log_device ⇒ Object (readonly)
Returns the value of attribute log_device.
14 15 16 |
# File 'lib/maid/maid.rb', line 14 def log_device @log_device end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
14 15 16 |
# File 'lib/maid/maid.rb', line 14 def rules @rules end |
#rules_path ⇒ Object (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_path ⇒ Object (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. end |
#clean ⇒ Object
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_rules ⇒ Object
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 |