module NameTag

Constants

DATE
DEFAULTS

default options

Info

info

Options

options

PROCESS

tr all values

RX

default pattern; matches artist, album, track, title, ext; optionally: album_n, year; matches e.g. “Between_the_Buried_and_Me/04-Colors_(2007)/05-Ants_of_the_Sky.mp3”

TR

default character substitution

TR_F

substitute characters

VERSION

Public Class Methods

_first_map(xs, &f) click to toggle source

lazy xs.map(&f).first

# File lib/nametag.rb, line 123
def self._first_map(xs, &f)
  xs.each { |x| y = f[x]; return y if y }; nil
end
configure(c = DEFAULTS, &b) click to toggle source

configure nametag by changing DEFAULTS, which is passed to the block

# File lib/nametag.rb, line 81
def self.configure(c = DEFAULTS, &b)
  b[c]; c
end
parse(filename, opts) click to toggle source

parse filename; tries each regex in turn @return [Info, nil]

# File lib/nametag.rb, line 89
def self.parse(filename, opts)
  m = _first_map(opts.regexes) { |rx| rx.match filename }
  m ? Info.new(Hash[m.names.map { |x| [x, m[x]] }]) : nil
end
process(info, opts) click to toggle source

process info object; tries each processing function in turn @return [Info]

# File lib/nametag.rb, line 96
def self.process(info, opts)                                  # {{{1
  ps    = opts._process + [PROCESS]; tr = TR_F[opts]
  info_ = _first_map(ps) { |p| p[info, opts, tr] }
  case info_
  when Info ; info_
  when Hash ; Info.new(info_)
  when Array; Info.new(Hash[info_])
  else raise 'processing function dit not return Info|Hash|Array'
  end
end
tag(filename, info) click to toggle source

process file: set tags from info, save file

# File lib/nametag.rb, line 108
def self.tag(filename, info)                                  # {{{1
  TagLib::FileRef.open(filename) do |file|
    tag         = file.tag
    tag.artist  = info[:artist]
    tag.album   = info[:album]
    tag.track   = info[:track].to_i
    tag.title   = info[:title] || "[track #{info[:track]}]"
    tag.year    = info[:year].to_i
    file.save
  end
end