Module: TivoHMO::API::Node

Extended by:
ActiveSupport::Concern
Includes:
GemLogger::LoggerSupport
Included in:
Container, Item
Defined in:
lib/tivohmo/api/node.rb

Overview

A tree node. Nodes have a parent, children and a root, with the tree itself representing the app/container/items heirarchy

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def app
  @app
end

#childrenObject

Returns the value of attribute children.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def children
  @children
end

#content_typeObject

Returns the value of attribute content_type.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def content_type
  @content_type
end

#created_atObject

Returns the value of attribute created_at.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def created_at
  @created_at
end

#identifierObject

Returns the value of attribute identifier.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def identifier
  @identifier
end

#modified_atObject

Returns the value of attribute modified_at.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def modified_at
  @modified_at
end

#parentObject

Returns the value of attribute parent.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def parent
  @parent
end

#rootObject

Returns the value of attribute root.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def root
  @root
end

#source_formatObject

Returns the value of attribute source_format.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def source_format
  @source_format
end

#titleObject

Returns the value of attribute title.



14
15
16
# File 'lib/tivohmo/api/node.rb', line 14

def title
  @title
end

Instance Method Details

#add_child(child) ⇒ Object Also known as: <<

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/tivohmo/api/node.rb', line 33

def add_child(child)
  raise ArgumentError, "Not a node: #{child}" unless child.is_a?(Node)
  child.parent = self
  child.root = self.root if self.root
  child.app = self.app if self.app
  @children << child
  child
end

#app?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/tivohmo/api/node.rb', line 48

def app?
  self == self.app
end

#find(title_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tivohmo/api/node.rb', line 52

def find(title_path)

  unless title_path.is_a?(Array)
    title_path = title_path.split('/')
    return root if title_path.blank?
    if title_path.first == ""
      return root.find(title_path[1..-1])
    end
  end

  next_title, rest = title_path[0], title_path[1..-1]

  self.children.find do |c|
    if c.title == next_title
      if rest.blank?
        return c
      else
        return c.find(rest)
      end
    end
  end

  return nil
end

#initialize(identifier) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/tivohmo/api/node.rb', line 25

def initialize(identifier)
  self.identifier = identifier
  self.title = identifier.to_s
  self.created_at = Time.now
  self.modified_at = Time.now
  @children = []
end

#root?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/tivohmo/api/node.rb', line 44

def root?
  self == self.root
end

#title_pathObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tivohmo/api/node.rb', line 77

def title_path
  if self == root
    "/"
  else
    if parent == root
      "/#{self.title}"
    else
      "#{parent.title_path}/#{self.title}"
    end
  end
end

#to_sObject



108
109
110
# File 'lib/tivohmo/api/node.rb', line 108

def to_s
  "<#{self.class.name}: #{self.identifier}>"
end

#tree_stringObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tivohmo/api/node.rb', line 89

def tree_string
  result = ""
  if self.root
    n = root
  else
    result << "(no root)\n"
    n = self
  end
  queue = [n]
  queue.each do |node|
    ident = node.title_path
    result << ident << "\n"
    if node.children.present?
      queue.concat(node.children)
    end
  end
  result
end