Sha256: e08056139a4903051045d6d1b77e2ec21f356cd85e3aaa3af5b06738f0e348ae

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

require 'fileutils'
require 'logger'

# Maid cleans up according to the given rules, logging what it does.
class Maid::Maid
  DEFAULTS = {
    :progname   => 'Maid',
    :log_path   => File.expand_path('~/.maid/maid.log'),
    :rules_path => File.expand_path('~/.maid/rules.rb'),
    :trash_path => File.expand_path('~/.Trash'),
    :file_options => {:noop => true}, # for FileUtils
  }.freeze

  include ::Maid::Tools
  attr_reader :file_options, :log_path, :rules, :rules_path, :trash_path

  # 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_path => '/home/username/log/maid.log', :trash_path => '/home/username/.local/share/Trash/files/')
  # 
  def initialize(options = {})
    options = DEFAULTS.merge(options.reject { |k, v| v.nil? })

    @log_path = options[:log_path]
    FileUtils.mkdir_p(File.dirname(@log_path)) unless @log_path.kind_of?(IO)
    @logger = Logger.new(@log_path)
    @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
  
  # Start cleaning, based on the rules defined at rules_path.
  def clean
    @logger.info 'Started'
    add_rules(@rules_path)
    follow_rules
    @logger.info 'Finished'
  end

  # Add the rules at path.
  def add_rules(path)
    Maid.with_instance(self) do
      # Using 'Kernel' here to help with testability
      Kernel.require(path)
    end
  end

  # Register a rule with a description and instructions (lambda function).
  def rule(description, &instructions)
    @rules << ::Maid::Rule.new(description, instructions)
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
maid-0.1.0.beta2 lib/maid/maid.rb
maid-0.1.0.beta1 lib/maid/maid.rb