Class: TivoHMO::Adapters::Plex::QualifiedCategory

Inherits:
Object
  • Object
show all
Includes:
GemLogger::LoggerSupport, MonitorMixin, TivoHMO::API::Container
Defined in:
lib/tivohmo/adapters/plex/qualified_category.rb

Instance Attribute Summary collapse

Attributes included from TivoHMO::API::Container

#presorted, #uuid

Attributes included from TivoHMO::API::Node

#app, #content_type, #created_at, #identifier, #modified_at, #parent, #root, #source_format, #title

Instance Method Summary collapse

Methods included from TivoHMO::API::Container

#refresh

Methods included from TivoHMO::API::Node

#add_child, #app?, #find, #root?, #title_path, #to_s, #tree_string

Constructor Details

#initialize(delegate, category_type, category_qualifier) ⇒ QualifiedCategory

Returns a new instance of QualifiedCategory.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tivohmo/adapters/plex/qualified_category.rb', line 13

def initialize(delegate, category_type, category_qualifier)
  # delegate is a Plex::Section
  @delegate = delegate

  super(delegate.key)
  self.presorted = true

  self.category_type = category_type
  self.category_qualifier = category_qualifier
  self.title = category_type.to_s.titleize
  self.modified_at = Time.at(delegate.updated_at.to_i)
  self.created_at = Time.now
end

Instance Attribute Details

#category_qualifierObject

Returns the value of attribute category_qualifier.



11
12
13
# File 'lib/tivohmo/adapters/plex/qualified_category.rb', line 11

def category_qualifier
  @category_qualifier
end

#category_typeObject

Returns the value of attribute category_type.



11
12
13
# File 'lib/tivohmo/adapters/plex/qualified_category.rb', line 11

def category_type
  @category_type
end

#delegateObject (readonly)

Returns the value of attribute delegate.



10
11
12
# File 'lib/tivohmo/adapters/plex/qualified_category.rb', line 10

def delegate
  @delegate
end

Instance Method Details

#child_countObject



29
30
31
# File 'lib/tivohmo/adapters/plex/qualified_category.rb', line 29

def child_count
  super_children.size
end

#childrenObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tivohmo/adapters/plex/qualified_category.rb', line 33

def children
  synchronize do

    # updated_at doesn't get updated for automatic updates, only
    # for updating from within plex media server web ui
    section_id = delegate.key.split('/').last
    new_delegate = delegate.library.section!(section_id)
    new_modified_at = new_delegate.updated_at.to_i
    if new_modified_at > modified_at.to_i
      logger.info "Plex section was updated, refreshing"
      @delegate = new_delegate
      self.modified_at = Time.at(new_modified_at)
      super.clear
    end

    if super.blank?
      qualified = Array(delegate.send(category_qualifier))
      # Sort by title descending so that creation times are
      # correct for tivo sort of newest first (Time.now for
      # created_at in Category)
      qualified = qualified.sort_by{|c| c[:title] }
      qualified.each do |category_value|
        add_child(Category.new(delegate, category_type, category_value))
      end
    end
  end

  super
end

#super_childrenObject



27
# File 'lib/tivohmo/adapters/plex/qualified_category.rb', line 27

alias_method :super_children, :children