Class: Fixnames::Engine

Inherits:
Object
  • Object
show all
Includes:
Debug, Filters, Helpers
Defined in:
lib/fixnames/engine.rb,
lib/fixnames/engine/scan_dir.rb

Overview

the main filtering-engine that fixes a single filename

Defined Under Namespace

Classes: ScanDir

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filters

#and_to_dash, #banners, #brackets, #camelcase, #charstrip, #checksums, #expunge, #fix_dashes, #fix_dots, #fix_numsep, #hack_and, #junkwords, #lowercase, #semicolon, #whitespace

Methods included from Helpers

#match_bracket_close, #match_bracket_open, #remove, #remove_all, #remove_bracket_characters_from, #remove_bracket_ranges, #replace, #replace_all, #safeloop, #translate, #wrap_brackets

Methods included from Debug

#bold, #debug, #info, #note, #warn

Constructor Details

#initialize(name, opts = Option.new) ⇒ Engine

Creates an engine to fix a single filename.

Parameters:

  • name (String)

    The filename to be fixed

  • options ({Symbol => Object})

    An options hash



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



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

def dir
  @dir
end

#fixedObject (readonly) Also known as: to_s

Returns the value of attribute fixed.



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

def fixed
  @fixed
end

#optionObject (readonly)

Returns the value of attribute option.



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

def option
  @option
end

#origObject (readonly)

Returns the value of attribute orig.



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

def orig
  @orig
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/fixnames/engine.rb', line 62

def changed?
  fixed != orig
end

#collision?Boolean

Returns:

  • (Boolean)


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

def collision?
  File.exists? fixed_path
end

#fix!Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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_pathObject



54
55
56
# File 'lib/fixnames/engine.rb', line 54

def fixed_path
  "#{dir}/#{fixed}"
end

#orig_pathObject



50
51
52
# File 'lib/fixnames/engine.rb', line 50

def orig_path
  "#{dir}/#{orig}"
end

#scandir_changed?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/fixnames/engine.rb', line 58

def scandir_changed?
  @scandir ? @scandir.changed? : false
end