class Object

Constants

FIELDS
RENAME_CHARS
RENAME_FS_CHARS
VERSION

Public Instance Methods

extract(tag, spec, filename) click to toggle source
# File bin/taffy, line 121
def extract(tag, spec, filename)
  name = File.basename(filename)
  name = name.slice(0, name.size - File.extname(name).size)
  regexp = Regexp.new(spec.gsub(/%(\W|_)?([lrcgt])/, '(.+)').
                           gsub(/%(\W|_)?([ny])/, '(\d+)'))
  if filematch = regexp.match(name)
    i = 1
    loop do
      specmatch = /%(?:\W|_)?([lrcgtny])/.match(spec)
      break unless specmatch
      FIELDS.each do |f|
        next if f[0] != specmatch[1]
        val = 'ny'.include?(f[0]) ? filematch[i].to_i : filematch[i]
        tag.send("#{f[1]}=", val)
      end
      spec = specmatch.post_match
      i += 1
    end
  else
    warn("#{filename} did not match extraction spec")
    $status = 2
  end
end
print_info(filename, tag) click to toggle source
rename(tag, spec, special_chars) click to toggle source
# File bin/taffy, line 152
def rename(tag, spec, special_chars)
  spec = spec.dup

  FIELDS.each do |f|
    tag_string = tag.send(f[1]).to_s
    tag_string = tag_string.rjust(2, '0') if f[1] == 'track'
    spec.gsub!(/%(\W|_)?(#{f[0]}|#{f[0].upcase})/) do |match|
      sub = $2 == f[0] ? tag_string.downcase : tag_string
      strip_special_chars(sub, $1 || '', special_chars)
    end
  end

  spec
end
safe_save(filename, fileref) click to toggle source
# File bin/taffy, line 167
def safe_save(filename, fileref)
  lowername = filename.downcase

  if lowername.end_with?('.ogg') or lowername.end_with?('.oga')
    warn("Not saving Ogg Vorbis file: #{filename} - see " +
      "https://github.com/robinst/taglib-ruby/issues/82")
    return false
  else
    return fileref.save
  end
end
strip_special_chars(str, subchar, special_chars) click to toggle source
# File bin/taffy, line 145
def strip_special_chars(str, subchar, special_chars)
  # removing single quotes is usually better than remapping them
  str.delete(special_chars == RENAME_CHARS ? "'" : '')
    .gsub(special_chars, subchar).gsub(/(#{subchar})+/, subchar)
    .gsub(/(^#{subchar}|#{subchar}$)/, '') || '_'
end