Class: Rutaci::FilenameSource

Inherits:
Source
  • Object
show all
Defined in:
lib/rutaci/filename_source.rb

Overview

This class collects the taginfo from various sources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Source

factory

Constructor Details

#initialize(filename, options) ⇒ FilenameSource

Returns a new instance of FilenameSource.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
# File 'lib/rutaci/filename_source.rb', line 24

def initialize(filename, options)
  raise ArgumentError, "options.format doesn't exist" unless options.format
  @filename = File.expand_path filename # we may need the full path
  @wanted_fields = options.fields # if this is not nil it should contain a positive-list of fileds
  @regexp, @fieldlist = self.create_regexp options.format
  @info = self.get_fields
end

Instance Attribute Details

#infoObject (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_fieldsObject



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