Module: TivoHMO::API::Node
- Extended by:
- ActiveSupport::Concern
- Includes:
- GemLogger::LoggerSupport
- 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
-
#app ⇒ Object
Returns the value of attribute app.
-
#children ⇒ Object
Returns the value of attribute children.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#modified_at ⇒ Object
Returns the value of attribute modified_at.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#root ⇒ Object
Returns the value of attribute root.
-
#source_format ⇒ Object
Returns the value of attribute source_format.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #add_child(child) ⇒ Object (also: #<<)
- #app? ⇒ Boolean
- #find(title_path) ⇒ Object
- #initialize(identifier) ⇒ Object
- #root? ⇒ Boolean
- #title_path ⇒ Object
- #to_s ⇒ Object
- #tree_string ⇒ Object
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def app @app end |
#children ⇒ Object
Returns the value of attribute children.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def children @children end |
#content_type ⇒ Object
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_at ⇒ Object
Returns the value of attribute created_at.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def created_at @created_at end |
#identifier ⇒ Object
Returns the value of attribute identifier.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def identifier @identifier end |
#modified_at ⇒ Object
Returns the value of attribute modified_at.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def modified_at @modified_at end |
#parent ⇒ Object
Returns the value of attribute parent.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def parent @parent end |
#root ⇒ Object
Returns the value of attribute root.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def root @root end |
#source_format ⇒ Object
Returns the value of attribute source_format.
14 15 16 |
# File 'lib/tivohmo/api/node.rb', line 14 def source_format @source_format end |
#title ⇒ Object
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: <<
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
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 |
#title_path ⇒ Object
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_s ⇒ Object
108 109 110 |
# File 'lib/tivohmo/api/node.rb', line 108 def to_s "<#{self.class.name}: #{self.identifier}>" end |
#tree_string ⇒ Object
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 |