module TivoHMO::API::Node
A tree node. Nodes have a parent, children and a root, with the tree itself representing the app/container/items heirarchy
Attributes
app[RW]
children[RW]
content_type[RW]
created_at[RW]
identifier[RW]
modified_at[RW]
parent[RW]
root[RW]
source_format[RW]
title[RW]
Public Class Methods
new(identifier)
click to toggle source
# 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
Public Instance Methods
add_child(child)
click to toggle source
# 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
Also aliased as: <<
app?()
click to toggle source
# File lib/tivohmo/api/node.rb, line 48 def app? self == self.app end
find(title_path)
click to toggle source
# 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
root?()
click to toggle source
# File lib/tivohmo/api/node.rb, line 44 def root? self == self.root end
title_path()
click to toggle source
# 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_s()
click to toggle source
# File lib/tivohmo/api/node.rb, line 108 def to_s "<#{self.class.name}: #{self.identifier}>" end
tree_string()
click to toggle source
# 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