class Fixnames::Engine

the main filtering-engine that fixes a single filename

Attributes

dir[R]
fixed[R]
option[R]
orig[R]
to_s[R]

Public Class Methods

new(name, opts=Option.new) click to toggle source

Creates an engine to fix a single filename.

@param name [String] The filename to be fixed @param options [{Symbol => Object}] An options hash

# File lib/fixnames/engine.rb, line 21
def initialize(name, opts=Option.new)
  @option = opts

  @dir  = File.dirname(name)
  @orig = File.basename(name)

  if option.recursive && File.directory?(@orig)
    @scandir = ScanDir.new(@orig, option)
  end

  @fixed = @orig.dup

  option.filter_order.each do |optname|
    if option.send(optname) and respond_to?(optname)
      debug "FILTER[:#{optname}]"
      old = fixed.dup
      case method(optname).arity
      when 1 then send optname, option.send(optname)
      when 0 then send optname
      else raise "Unsupported arity in ##{optname}"
      end
      if old != fixed
        debug "\t    old -- #{old.inspect}"
        debug "\t    new -- #{fixed.inspect}"
      end
    end
  end
end

Public Instance Methods

changed?() click to toggle source
# File lib/fixnames/engine.rb, line 62
def changed?
  fixed != orig
end
collision?() click to toggle source
# File lib/fixnames/engine.rb, line 66
def collision?
  File.exists? fixed_path
end
fix!() click to toggle source
# File lib/fixnames/engine.rb, line 70
def fix!
  @scandir.fix! if @scandir

  if changed?
    if collision?
      warn "NAME COLLISION: #{fixed_path.inspect}"
    else
      note "mv #{orig_path.inspect} #{fixed_path.inspect}"
      File.rename orig_path, fixed_path unless option.pretend
    end
  else
    info "no change: #{orig_path.inspect}"
  end
  self
end
fixed_path() click to toggle source
# File lib/fixnames/engine.rb, line 54
def fixed_path
  "#{dir}/#{fixed}"
end
orig_path() click to toggle source
# File lib/fixnames/engine.rb, line 50
def orig_path
  "#{dir}/#{orig}"
end
scandir_changed?() click to toggle source
# File lib/fixnames/engine.rb, line 58
def scandir_changed?
  @scandir ? @scandir.changed? : false
end