Sha256: b07c40b819ff09f241b45789f04250207505796c6e42e621e0fbc5483e03e80c

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'tagomatic/tags'

module Tagomatic

  class FormatMatcher

    include Tagomatic::Tags

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

    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) 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

    def to_s
      @format
    end

    protected

    def normalize(value)
      value = value.gsub('_', ' ')
      parts = value.split(' ')
      capitalized = parts.map {|p| p.capitalize}
      capitalized.join(' ')
    end

    def valid_constraints?
      valid_double_match_with_same_value?(FORMAT_ID_ARTIST, FORMAT_ID_ARTIST_AGAIN) &&
      valid_double_match_with_same_value?(FORMAT_ID_ALBUM, FORMAT_ID_ALBUM_AGAIN)
    end

    def valid_double_match_with_same_value?(base_tag, again_tag)
      return true unless @tags.has_key?(again_tag)
      return false unless @tags.has_key?(base_tag)
      return @tags[base_tag] == @tags[again_tag]
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tagomatic-0.1.7 lib/tagomatic/format_matcher.rb