Class: Rutaci::FilenameSource
- Defined in:
- lib/rutaci/filename_source.rb
Overview
This class collects the taginfo from various sources.
Instance Attribute Summary collapse
-
#info ⇒ Object
readonly
Returns the value of attribute info.
Instance Method Summary collapse
- #create_regexp(format) ⇒ Object
- #get_fields ⇒ Object
-
#initialize(filename, options) ⇒ FilenameSource
constructor
A new instance of FilenameSource.
Methods inherited from Source
Constructor Details
#initialize(filename, options) ⇒ FilenameSource
Returns a new instance of FilenameSource.
24 25 26 27 28 29 30 |
# File 'lib/rutaci/filename_source.rb', line 24 def initialize(filename, ) raise ArgumentError, "options.format doesn't exist" unless .format @filename = File. filename # we may need the full path @wanted_fields = .fields # if this is not nil it should contain a positive-list of fileds @regexp, @fieldlist = self.create_regexp .format @info = self.get_fields end |
Instance Attribute Details
#info ⇒ Object (readonly)
Returns the value of attribute info.
23 24 25 |
# File 'lib/rutaci/filename_source.rb', line 23 def info @info end |
Instance Method Details
#create_regexp(format) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rutaci/filename_source.rb', line 45 def create_regexp(format) str = String.new fieldlist = Array.new Formater.tokenize format do |token, is_placeholder| if is_placeholder str += "([^#{File::SEPARATOR}]*)" # match any char appart File::SEPARATOR into a group fieldlist.push Formater::PLACEHOLDERS[token] else str += Regexp.escape token end end # tokenize return Regexp.new(str + "$"), fieldlist end |
#get_fields ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rutaci/filename_source.rb', line 32 def get_fields match = @regexp.match @filename return nil unless match groups = match.to_a groups.shift # get rid of the first field, since it's the whole match (we need only the groups) info = Hash.new @fieldlist.each_with_index do |field, index| next if @wanted_fields and not @wanted_fields.include? field info[field] = groups[index] end return info end |