Class: Puggernaut::Server::Channel
- Inherits:
-
EM::Channel
- Object
- EM::Channel
- Puggernaut::Server::Channel
- Extended by:
- Logger
- Defined in:
- lib/puggernaut/server/channel.rb
Class Attribute Summary collapse
-
.channels ⇒ Object
Returns the value of attribute channels.
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Class Method Summary collapse
- .all_messages_after_id(channel, identifier) ⇒ Object
- .all_messages_after_time(channel, after_time) ⇒ Object
- .create(channels, user_id) ⇒ Object
- .inhabitants(channel_name) ⇒ Object
- .say(messages, exclude_user_id = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(channels, user_id) ⇒ Channel
constructor
A new instance of Channel.
Methods included from Logger
Constructor Details
#initialize(channels, user_id) ⇒ Channel
Returns a new instance of Channel.
7 8 9 10 11 |
# File 'lib/puggernaut/server/channel.rb', line 7 def initialize(channels, user_id) @channels = channels @user_id = user_id super() end |
Class Attribute Details
.channels ⇒ Object
Returns the value of attribute channels.
17 18 19 |
# File 'lib/puggernaut/server/channel.rb', line 17 def channels @channels end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
5 6 7 |
# File 'lib/puggernaut/server/channel.rb', line 5 def channels @channels end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
5 6 7 |
# File 'lib/puggernaut/server/channel.rb', line 5 def user_id @user_id end |
Class Method Details
.all_messages_after_id(channel, identifier) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/puggernaut/server/channel.rb', line 26 def (channel, identifier) if @messages && @messages[channel] found = false ( @messages[channel].select { |(id, , time)| found = true if id == identifier found }[1..-1] || [] ).collect { || "#{channel}|#{.join '|'}" } else [] end end |
.all_messages_after_time(channel, after_time) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/puggernaut/server/channel.rb', line 43 def (channel, after_time) if @messages && @messages[channel] ( @messages[channel].select { |(id, , time)| after_time < time } || [] ).collect { || "#{channel}|#{.join '|'}" } else [] end end |
.create(channels, user_id) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/puggernaut/server/channel.rb', line 19 def create(channels, user_id) channel = self.new(channels, user_id) @channels ||= [] @channels << channel channel end |
.inhabitants(channel_name) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/puggernaut/server/channel.rb', line 58 def inhabitants(channel_name) user_ids = @channels.collect do |channel| channel.user_id if channel.channels.include?(channel_name) end user_ids.compact end |
.say(messages, exclude_user_id = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/puggernaut/server/channel.rb', line 65 def say(, exclude_user_id=nil) @messages ||= {} = .inject({}) do |hash, (channel_name, )| = .collect do || [ rand.to_s[2..-1], , Time.now ] end @messages[channel_name] ||= [] @messages[channel_name] += @messages[channel_name] = @messages[channel_name].select do || [2] >= Time.now - 2 * 60 * 60 end hash[channel_name] = hash end @channels.each do |channel| next if exclude_user_id && channel.user_id == exclude_user_id push = channel.channels.collect do |channel_name| if [channel_name] [channel_name].collect { || "#{channel_name}|#{.join('|')}" }.join("\n") end end push = push.compact unless push.empty? channel.push push.join("\n") end end end |