Class: Faye::Channel::Tree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/faye/protocol/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Tree

Returns a new instance of Tree.



55
56
57
58
# File 'lib/faye/protocol/channel.rb', line 55

def initialize(value = nil)
  @value = value
  @children = {}
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



53
54
55
# File 'lib/faye/protocol/channel.rb', line 53

def value
  @value
end

Instance Method Details

#[](name) ⇒ Object



73
74
75
76
# File 'lib/faye/protocol/channel.rb', line 73

def [](name)
  subtree = traverse(name)
  subtree ? subtree.value : nil
end

#[]=(name, value) ⇒ Object



78
79
80
81
# File 'lib/faye/protocol/channel.rb', line 78

def []=(name, value)
  subtree = traverse(name, true)
  subtree.value = value unless subtree.nil?
end

#distribute_message(message) ⇒ Object



138
139
140
141
142
# File 'lib/faye/protocol/channel.rb', line 138

def distribute_message(message)
  glob(message['channel']).each do |channel|
    channel.publish_event(:message, message['data'])
  end
end

#each(prefix = []) {|prefix, @value| ... } ⇒ Object

Yields:



64
65
66
67
# File 'lib/faye/protocol/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_childObject



60
61
62
# File 'lib/faye/protocol/channel.rb', line 60

def each_child
  @children.each { |key, subtree| yield(key, subtree) }
end

#glob(path = []) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/faye/protocol/channel.rb', line 96

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 unless @value.nil?
    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

#keysObject



69
70
71
# File 'lib/faye/protocol/channel.rb', line 69

def keys
  map { |key, value| '/' + key * '/' }
end

#subscribe(names, callback) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/faye/protocol/channel.rb', line 123

def subscribe(names, callback)
  return unless callback
  names.each do |name|
    channel = self[name] ||= Channel.new(name)
    channel.add_subscriber(:message, callback)
  end
end

#traverse(path, create_if_absent = false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/faye/protocol/channel.rb', line 83

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

#unsubscribe(name, callback) ⇒ Object



131
132
133
134
135
136
# File 'lib/faye/protocol/channel.rb', line 131

def unsubscribe(name, callback)
  channel = self[name]
  return false unless channel
  channel.remove_subscriber(:message, callback)
  channel.count_subscribers(:message).zero?
end