Class: Tagomatic::FormatMatcher

Inherits:
Object
  • Object
show all
Includes:
Tags
Defined in:
lib/tagomatic/format_matcher.rb

Constant Summary

Constants included from Tags

Tags::FORMAT_ID_ALBUM, Tags::FORMAT_ID_ALBUM_AGAIN, Tags::FORMAT_ID_ARTIST, Tags::FORMAT_ID_ARTIST_AGAIN, Tags::FORMAT_ID_DISC, Tags::FORMAT_ID_EXTENDED_WHITESPACE, Tags::FORMAT_ID_GENRE, Tags::FORMAT_ID_IGNORE, Tags::FORMAT_ID_SURROUNDED_YEAR, Tags::FORMAT_ID_TITLE, Tags::FORMAT_ID_TRACKNUM, Tags::FORMAT_ID_WHITESPACE, Tags::FORMAT_ID_YEAR, Tags::FORMAT_REGEXP_ALBUM, Tags::FORMAT_REGEXP_ALBUM_AGAIN, Tags::FORMAT_REGEXP_ARTIST, Tags::FORMAT_REGEXP_ARTIST_AGAIN, Tags::FORMAT_REGEXP_DISC, Tags::FORMAT_REGEXP_EXTENDED_WHITESPACE, Tags::FORMAT_REGEXP_GENRE, Tags::FORMAT_REGEXP_IGNORE, Tags::FORMAT_REGEXP_SURROUNDED_YEAR, Tags::FORMAT_REGEXP_TITLE, Tags::FORMAT_REGEXP_TRACKNUM, Tags::FORMAT_REGEXP_WHITESPACE, Tags::FORMAT_REGEXP_YEAR

Instance Method Summary collapse

Constructor Details

#initialize(compiled_regexp, tag_mapping, original_format) ⇒ FormatMatcher

Returns a new instance of FormatMatcher.



9
10
11
12
13
# File 'lib/tagomatic/format_matcher.rb', line 9

def initialize(compiled_regexp, tag_mapping, original_format)
  @regexp = compiled_regexp
  @mapping = tag_mapping
  @format = original_format
end

Instance Method Details

#match(file_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tagomatic/format_matcher.rb', line 15

def match(file_path)
  matchdata = @regexp.match(file_path)
  return nil unless matchdata
  return nil unless matchdata.captures.size == @mapping.size
  @tags = {}
  0.upto(@mapping.size - 1) do |index|
    value = matchdata.captures[index]
    value = normalize(value) if value
    @tags[@mapping[index]] = value
  end
  @tags[FORMAT_ID_YEAR] ||= @tags[FORMAT_ID_SURROUNDED_YEAR]
  return nil unless valid_constraints?
  @tags
end

#to_sObject



30
31
32
# File 'lib/tagomatic/format_matcher.rb', line 30

def to_s
  @format
end