Class: AbsoluteRenamer::GeneralModule

Inherits:
IModule show all
Defined in:
lib/absolute_renamer/external/modules/core/general/module.rb

Instance Method Summary collapse

Methods inherited from IModule

#process, symbol

Methods inherited from WithChildren

inherited

Methods included from UseConfig

#conf

Constructor Details

#initializeGeneralModule

Returns a new instance of GeneralModule.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 3

def initialize
    @actions = {'*'  => :file_camelize,
                '$'  => :file_original,
                '%'  => :file_downcase,
                '&'  => :file_upcase,
                '\\' => :file_strip,
                '#'  => :count
               }

    @case_filters = ['(\\\\\*)',    # \*
                     '(\\\\\$)',    # \$
                     '(\\\%)',      # \%
                     '(\\\&)',      # \&
                     '(\\\\\\\)',   # \\
                     '(\*)',        # *
                     '(\$)',        # $
                     '(%)',         # %
                     '(&)',         # &
                     '(\\\)'        # \
                    ]

    # matches strings like [42-43] [42-] [*42-43] [42;43] etc...
    @part_filters = ['(\[(.)?(\d+)(((-)(\d+)?)|((;)(\d+)))?\])']

    # matches counters like # ### #{2} ##{2;42} or [length-42]
    @misc_filters = ['(/)', '(#+(\{.*\})?)', '(\[length(--?\d+)?\])']

    @filters = @case_filters + @part_filters + @misc_filters
end

Instance Method Details

#count(file, infos, type) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 95

def count(file, infos, type)
    matched = infos[0].match(/(#+)(\{((-?\d+)(;(-?\d+)?)?)?\})?/)
    @counter ||= []
    @last ||= nil
    @current ||= 0

    @current = 0 if @last != file
    @current += 1 if @last == file

    start = matched[4] || 1
    step = matched[6] || 1
    start = start.to_i
    step = step.to_i

    @counter[@current] ||= {:start => start,
                            :step => step,
                            :current => start - step
                           }

    @counter[@current][:current] += @counter[@current][:step]
    @last = file
    val = @counter[@current][:current].to_s.rjust(matched[1].length, '0')
    val.gsub!(/(0+)-/, '-\1')
    val
end

#file_camelize(file, infos, type) ⇒ Object



47
48
49
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 47

def file_camelize(file, infos, type)
    file.send(type).camelize
end

#file_downcase(file, infos, type) ⇒ Object



55
56
57
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 55

def file_downcase(file, infos, type)
    file.send(type).downcase
end

#file_original(file, infos, type) ⇒ Object



51
52
53
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 51

def file_original(file, infos, type)
    file.send(type)
end

#file_part(file, infos, type) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 67

def file_part(file, infos, type)
    matched = infos[0].match(/(\[([^\d])?(\d+)(((;)(\d+))|((-)(\d+)?))?\])/)

    modifier = matched[2]
    x = matched[3].to_i - 1
    y = matched[7] || matched[10]
    y = y.to_i unless y.nil?        
    action = matched[6] || matched[9]

    str = file.send(type)

    if (action == '-')
        y -= 1 unless y.nil?
        y ||= str.length
        val = str[x..y]
    elsif (action == ';')
        val = str[x, y]
    else
        val = str[x].chr
    end

    unless modifier.nil?
        mp = CaseModule.method(CaseModule.actions[modifier])
        val = mp.call(val)
    end
    val
end

#file_strip(file, infos, type) ⇒ Object



63
64
65
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 63

def file_strip(file, infos, type)
    file.send(type).strip
end

#file_upcase(file, infos, type) ⇒ Object



59
60
61
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 59

def file_upcase(file, infos, type)
    file.send(type).upcase
end

#interpret(file, infos, type) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 33

def interpret(file, infos, type)
    if (infos[0].length == 1)
        self.method(@actions[infos[0][0].chr]).call(file, infos, type)
    elsif (infos[0][1..6] == 'length')
        self.length(file, infos, type)
    elsif (infos[0][0].chr == '[')
        self.file_part(file, infos, type)
    elsif (infos[0][0].chr == '#')
        self.count(file, infos, type)
    else
        infos[0][1].chr
    end
end

#length(file, infos, type) ⇒ Object



121
122
123
124
# File 'lib/absolute_renamer/external/modules/core/general/module.rb', line 121

def length(file, infos, type)
    matched = infos[0].match(/\[length(-(-?\d+))?\]/)
    file.name.length - matched[2].to_i
end