class TivoHMO::Adapters::Filesystem::FolderContainer

A Container based on a filesystem folder

Constants

VIDEO_EXTENSIONS

Attributes

allowed_item_extensions[RW]
allowed_item_types[RW]
full_path[R]

Public Class Methods

new(identifier) click to toggle source
Calls superclass method TivoHMO::API::Container::new
# File lib/tivohmo/adapters/filesystem/folder_container.rb, line 30
def initialize(identifier)
  @full_path = File.expand_path(identifier)
  raise ArgumentError,
        "Must provide an existing directory: #{full_path}" unless File.directory?(full_path)

  super(full_path)

  self.allowed_item_types = %i[file dir]
  self.allowed_item_extensions = VIDEO_EXTENSIONS

  self.title = File.basename(self.identifier).titleize
  self.modified_at = File.mtime(self.identifier)
  self.created_at = File.ctime(self.identifier)

  @subtitles = config_get(:enable_subtitles)
end

Public Instance Methods

children() click to toggle source
Calls superclass method
# File lib/tivohmo/adapters/filesystem/folder_container.rb, line 47
def children
  synchronize do
    if super.blank? || @subtitles != config_get(:enable_subtitles)
      super.clear
      @subtitles = config_get(:enable_subtitles)

      items = Dir["#{self.full_path}/*"].group_by do |path|
        if allowed_container?(path)
          :dir
        elsif allowed_item?(path)
          :file
        else
          :skipped
        end
      end

      Array(items[:dir]).each {|path| add_child(FolderContainer.new(path)) }
      Array(items[:file]).each {|path| add_grouped(path) }
      Array(items[:skipped]).each {|path| logger.debug "Ignoring: #{path}" } if logger.debug?

    end
  end

  super
end

Protected Instance Methods

add_grouped(path) click to toggle source
# File lib/tivohmo/adapters/filesystem/folder_container.rb, line 86
def add_grouped(path)
  primary = FileItem.new(path)

  if @subtitles
    subs = SubtitlesUtil.instance.subtitles_for_media_file(path)

    if subs.size > 0
      group = Group.new(primary.identifier, primary.title)
      add_child(group)
      group.add_child(primary)
      subs.each {|s| group.add_child(FileItem.new(path, s)) }
    else
      add_child(primary)
    end
  else
    add_child(primary)
  end
end
allowed_container?(path) click to toggle source
# File lib/tivohmo/adapters/filesystem/folder_container.rb, line 75
def allowed_container?(path)
  File.directory?(path) && allowed_item_types.include?(:dir)
end
allowed_item?(path) click to toggle source
# File lib/tivohmo/adapters/filesystem/folder_container.rb, line 79
def allowed_item?(path)
  ext = File.extname(path).gsub(/^./, '')
  File.file?(path) &&
      allowed_item_types.include?(:file) &&
      allowed_item_extensions.include?(ext)
end