Class: Faye::Channel::Tree
- Inherits:
-
Object
- Object
- Faye::Channel::Tree
- Includes:
- Enumerable
- Defined in:
- lib/faye/channel.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #each(prefix = []) {|prefix, @value| ... } ⇒ Object
- #each_child ⇒ Object
- #glob(path = []) ⇒ Object
-
#initialize(value = nil) ⇒ Tree
constructor
A new instance of Tree.
- #traverse(path, create_if_absent = false) ⇒ Object
Constructor Details
#initialize(value = nil) ⇒ Tree
Returns a new instance of Tree.
55 56 57 58 |
# File 'lib/faye/channel.rb', line 55 def initialize(value = nil) @value = value @children = {} end |
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
53 54 55 |
# File 'lib/faye/channel.rb', line 53 def value @value end |
Instance Method Details
#[](name) ⇒ Object
69 70 71 72 |
# File 'lib/faye/channel.rb', line 69 def [](name) subtree = traverse(name) subtree ? subtree.value : nil end |
#[]=(name, value) ⇒ Object
74 75 76 77 |
# File 'lib/faye/channel.rb', line 74 def []=(name, value) subtree = traverse(name, true) subtree.value = value unless subtree.nil? end |
#each(prefix = []) {|prefix, @value| ... } ⇒ Object
64 65 66 67 |
# File 'lib/faye/channel.rb', line 64 def each(prefix = [], &block) each_child { |path, subtree| subtree.each(prefix + [path], &block) } yield(prefix, @value) unless @value.nil? end |
#each_child ⇒ Object
60 61 62 |
# File 'lib/faye/channel.rb', line 60 def each_child @children.each { |key, subtree| yield(key, subtree) } end |
#glob(path = []) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/faye/channel.rb', line 92 def glob(path = []) path = Channel.parse(path) if String === path return [] if path.nil? return @value.nil? ? [] : [@value] if path.empty? if path == [:*] return @children.inject([]) do |list, (key, subtree)| list << subtree.value unless subtree.value.nil? list end end if path == [:**] list = map { |key, value| value } list.pop return list end list = @children.values_at(path.first, :*). compact. map { |t| t.glob(path[1..-1]) } list << @children[:**].value if @children[:**] list.flatten end |
#traverse(path, create_if_absent = false) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/faye/channel.rb', line 79 def traverse(path, create_if_absent = false) path = Channel.parse(path) if String === path return nil if path.nil? return self if path.empty? subtree = @children[path.first] return nil if subtree.nil? and not create_if_absent subtree = @children[path.first] = self.class.new if subtree.nil? subtree.traverse(path[1..-1], create_if_absent) end |