Class: AbsoluteRenamer::IModule

Inherits:
WithChildren show all
Defined in:
lib/absolute_renamer/imodule.rb

Overview

Modules parent class. Modules must inherit of it.

Direct Known Subclasses

CaseModule, GeneralModule

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WithChildren

inherited

Methods included from UseConfig

#conf

Constructor Details

#initializeIModule

Returns a new instance of IModule.



7
8
9
# File 'lib/absolute_renamer/imodule.rb', line 7

def initialize
    @filters = []
end

Class Method Details

.symbolObject

Returns the classname symbol



12
13
14
# File 'lib/absolute_renamer/imodule.rb', line 12

def self.symbol
    name.intern
end

Instance Method Details

#interpret(file, infos, type) ⇒ Object

Interprets a matched pattern. Searchs for the corresponding callback in the current module and call it.

file: a FileInfo instance infos: the matched values depending of the pattern type: the type of the renaming format (:name or :ext)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/absolute_renamer/imodule.rb', line 53

def interpret(file, infos, type)
    modifier = infos[2]
    action = infos[3]

    return conf[:options][:default_string] unless self.respond_to?(action.intern)

    ap = self.method(action.intern)
    val = ap.call(file, infos, type)
    unless modifier.empty?
        mp = CaseModule.method(CaseModule.actions[modifier])
        val = mp.call(val)
    end
    val.empty? ? conf[:options][:default_string] : val
end

#process(file, format, type = :name) ⇒ Object

Process a file by searching for a known pattern in its name and replacing it by the corresponding value. The pattern is a regular expression obtained by concatening the @filters variable with “|”.

file: a FileInfo instance format: the format string used to rename the file type: the type of the renaming format (:name or :ext)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/absolute_renamer/imodule.rb', line 24

def process(file, format, type = :name)
    return format if @filters.empty?

    str = format
    result = []
    pattern = Regexp.new(@filters.join('|'))

    idx = str.index(pattern)
    while idx
        matched = pattern.match(str).to_a.compact
        part = str.partition(matched[0])
        result.push(part[0])
        val = self.interpret(file, matched, type)
        result.push(val)
        str = part[2]
        idx = str.index(pattern)
    end
    result.push(str) unless str.empty?
    format.replace(result.join)
    format
end