Class: AbsoluteRenamer::Processor

Inherits:
Object
  • Object
show all
Extended by:
UseConfig
Defined in:
lib/absolute_renamer.rb

Overview

The main class of AbsoluteRenamer.

Organizes the files and directories renaming process.

Class Method Summary collapse

Methods included from UseConfig

conf

Class Method Details

.call_entry_point(ep, params = nil) ⇒ Object

Calls the given entry point for each plugin available

Ask to each plugin if it implements the entry point and calls it with params if it isn’t null. It keeps going will all plugins return true.

returns true if all plugins returned true



90
91
92
93
94
95
96
97
98
99
# File 'lib/absolute_renamer.rb', line 90

def call_entry_point(ep, params = nil)
    puts "Plugin Entry Point: #{ep}" if conf[:debug]
    keep_going = true
    @plugins.each_value do |plugin|
        if plugin.respond_to?(ep)
            keep_going &= params.nil? ? plugin.send(ep) : plugin.send(ep, params)
        end
    end
    keep_going
end

.create_names_listObject

Creates the new names for each file passed to AbsoluteRenamer.

Asks to each module if he as something to replace in the name of each file.

Calls the before_names_generation entry point.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/absolute_renamer.rb', line 30

def create_names_list
    call_entry_point(:before_names_generation)
    return if conf[:files].empty?
    mods = {}
    conf[:files].each do |file|
        name = conf[:options][:format].clone
        ext = conf[:options][:ext_format].clone
        do_replacements(file.name, :before)
        AbsoluteRenamer::IModule.children.each do |mod|
            mod_sym = mod.symbol
            mods[mod_sym] ||= mod.new
            name = mods[mod_sym].process(file, name)
            ext = mods[mod_sym].process(file, ext, :ext) unless file.dir
        end
        do_replacements(name, :after)
        file.new_name = name
        file.new_name << '.' << ext unless (file.dir or file.ext.empty?)
    end
    if (conf[:options][:dir] and conf[:options][:rec])
      conf[:files].sort! { |a, b| AbsoluteRenamer::FileInfo.compare_level(a, b) }
    end
    mods.clear
end

.do_renamingObject

For each file/dir replaces his name by his new name.

Calls the following entry points :

- before_batch_renaming ;
- before_file_renaming ;
- after_file_renaming ;
- after_batch_renaming.


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/absolute_renamer.rb', line 61

def do_renaming
    if call_entry_point(:before_batch_renaming)
        conf[:files].each do |file|
            if call_entry_point(:before_file_renaming, :file => file)
                if file.respond_to?(conf[:options][:mode])
                    file.send(conf[:options][:mode])
                end
                call_entry_point(:after_file_renaming, :file => file)
            end
        end
        call_entry_point(:after_batch_renaming)
    end
end

.do_replacements(format, moment) ⇒ Object

Applies replacements

The replacements are stored in conf[replacements] and are provided as command line paramters.

format represents the string in which the replacements are done moment determines which replacements are done (on old or new name)



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/absolute_renamer.rb', line 108

def do_replacements(format, moment)
    begin
        replacements = conf[:options][:replacements][moment]
    rescue NoMethodError
        replacements = nil
    end
    unless replacements.nil?
        replacements.each do |repl|
            format.gsub!(repl[:pattern], repl[:replace])
        end
    end
end

.load_pluginsObject

Loads the plugins list in the @plugins variable.



76
77
78
79
80
81
# File 'lib/absolute_renamer.rb', line 76

def load_plugins
    @plugins = {}
    AbsoluteRenamer::IPlugin.children.each do |plugin|
        @plugins[plugin.symbol] = plugin.new
    end
end