Sha256: a261cd1a77f7188ac6479bb4069f2612dc21718355d20d694b222768377ac60e

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# == Synopsis
# encapsulation of all media files
class MediaFiles
  attr_reader :medias, :titles, :duplicate_titles

  # == Synopsis
  # directories => Array of String directory pathspecs
  def initialize(directories)
    @medias = find_medias(directories)
    @titles = find_titles(@medias)
    @duplicate_titles = find_duplicate_titles(@titles)
  end

  protected

  # == Synopsis
  # find all the media files in the given set of directories
  def find_medias(directories)
    medias = []
    directories.each do |dir|
      Dir.chdir(dir)
      medias += Dir.glob("**/*.{#{AppConfig[:media_extensions].join(',')}}").collect do |filename|
        Media.new(dir, filename)
      end
    end
    medias
  end

  # == Synopsis
  # return a hash where the key is the media's title and
  # the value is an Array of Media instances
  def find_titles(medias)
    titles = {}
    medias.each do |media|
      title = media.title_with_year
      titles[title] ||= []
      titles[title] << media
    end
    titles
  end

  # == Synopsis
  # find duplicate titles and return them in a hash
  # where the key is the title and the value is an
  # array of Media objects
  def find_duplicate_titles(titles)
    duplicates = {}
    titles.each do |title, medias|
      base_medias = medias.collect{|media| media.path_to(:base) }.uniq
      if base_medias.length > 1
        duplicates[title] = medias
      end
    end
    duplicates
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
royw-dvdprofiler2xbmc-0.0.18 lib/dvdprofiler2xbmc/models/media_files.rb
royw-dvdprofiler2xbmc-0.0.19 lib/dvdprofiler2xbmc/models/media_files.rb
royw-dvdprofiler2xbmc-0.1.0 lib/dvdprofiler2xbmc/models/media_files.rb
royw-dvdprofiler2xbmc-0.1.2 lib/dvdprofiler2xbmc/models/media_files.rb