lib/absolute_renamer/imodule.rb in AbsoluteRenamer-1.1.0 vs lib/absolute_renamer/imodule.rb in AbsoluteRenamer-1.1.1
- old
+ new
@@ -1,86 +1,83 @@
require 'absolute_renamer/with_children'
module AbsoluteRenamer
- # Modules parent class.
- # Modules must inherit of it.
- class IModule < AbsoluteRenamer::WithChildren
- def initialize
- @filters = []
- end
+ # Modules parent class.
+ # Modules must inherit of it.
+ class IModule < AbsoluteRenamer::WithChildren
+ def initialize
+ @filters = []
+ end
- # Returns the classname symbol
- def self.symbol
- name.intern
- end
+ # Returns the classname symbol
+ def self.symbol
+ name.intern
+ end
- # Returns a format pattern generated from pattern_string that
- # can be used to match strings like [*test] in the filename format
- # pattern('test') #=> '(\[(.)?test\])'
- def pattern(pattern_string)
- Regexp.new "(\\[(.)?#{pattern_string}\\])"
- end
+ def self.process(file, name_format, ext_format)
+ @mods ||= {}
- # Returns a value modified using a modifier defined in the Case module
- # modifiy('value', '&') #=> 'VALUE'
- # modifiy('value', '*') #=> 'Value'
- def modify(val, modifier)
- if CaseModule.actions.include?(modifier)
- mod = CaseModule.method(CaseModule.actions[modifier])
- val = mod.call(val)
- end
- val
- end
+ self.children.each do |mod|
+ mod_sym = mod.symbol
+ @mods[mod_sym] ||= mod.new
+ name_format = @mods[mod_sym].process(file, name_format)
+ ext_format = @mods[mod_sym].process(file, ext_format, :ext) unless file.dir
+ end
- # 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)
- def process(file, format, type = :name)
- return format if @filters.empty?
+ [name_format, ext_format]
+ end
- str = format
- result = []
- pattern = Regexp.union @filters
+ # Returns a format pattern generated from pattern_string that
+ # can be used to match strings like [*test] in the filename format
+ # pattern('test') #=> '(\[(.)?test\])'
+ def pattern(pattern_string)
+ Regexp.new "(\\[(.)?#{pattern_string}\\])"
+ end
- idx = str.index(pattern)
- while idx
- matched = pattern.match(str).to_a
- 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
+ # Returns a value modified using a modifier defined in the Case module
+ # modifiy('value', '&') #=> 'VALUE'
+ # modifiy('value', '*') #=> 'Value'
+ def modify(val, modifier)
+ modification = CaseModule.actions[modifier]
+ val = CaseModule.send(modification, val) unless modification.nil?
+ val
+ end
- # 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)
- def interpret(file, infos, type)
- modifier = infos[2]
- action = infos[3]
+ # 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)
+ def process(file, format, type = :name)
+ return format if @filters.empty?
- return conf[:options][:default_string] unless self.respond_to?(action.intern)
+ result = []
+ pattern = Regexp.union @filters
+ idx = format.index(pattern)
- 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
+ while idx
+ matched = pattern.match(format).to_a
+ part = format.partition(matched[0])
+ result.push(part[0])
+ result.push self.interpret(file, matched, type)
+ format = part[2]
+ idx = format.index(pattern)
+ end
+ result.push(format)
+ format.replace(result.join)
end
+
+ # 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)
+ def interpret(file, infos, type)
+ # This method has to be overriden in every module
+ end
+ end
end