Module: Ably::Modules::ChannelsCollection
- Includes:
- Enumerable
- Included in:
- Realtime::Channels, Rest::Channels
- Defined in:
- lib/ably/modules/channels_collection.rb
Overview
ChannelsCollection module provides common functionality to the Rest and Realtime Channels objects such as #get, #[], #fetch, and #release
Instance Attribute Summary collapse
-
#length ⇒ Integer
(also: #count, #size)
readonly
Number of channels created.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Method to allow ChannelsCollection to be Enumerable.
-
#fetch(name) {|options| ... } ⇒ Channel
Return a Channel for the given name if it exists, else the block will be called.
-
#get(name, channel_options = {}) ⇒ Channel
(also: #[])
Return a Channel for the given name.
- #initialize(client, channel_klass) ⇒ Object
-
#release(name) ⇒ void
Destroy the Channel and releases the associated resources.
Instance Attribute Details
#length ⇒ Integer (readonly) Also known as: count, size
Returns number of channels created.
67 68 69 |
# File 'lib/ably/modules/channels_collection.rb', line 67 def length channels.length end |
Instance Method Details
#each(&block) ⇒ Object
Method to allow Ably::Modules::ChannelsCollection to be Enumerable
74 75 76 77 |
# File 'lib/ably/modules/channels_collection.rb', line 74 def each(&block) return to_enum(:each) unless block_given? channels.values.each(&block) end |
#fetch(name) {|options| ... } ⇒ Channel
Return a Channel for the given name if it exists, else the block will be called. This method is intentionally similar to Hash#fetch providing a simple way to check if a channel exists or not without creating one
48 49 50 |
# File 'lib/ably/modules/channels_collection.rb', line 48 def fetch(name, &missing_block) channels.fetch(name, &missing_block) end |
#get(name, channel_options = {}) ⇒ Channel Also known as: []
Return a Channel for the given name
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ably/modules/channels_collection.rb', line 20 def get(name, = {}) if channels.has_key?(name) channels[name].tap do |channel| if && !.empty? if channel.respond_to?(:need_reattach?) && channel.need_reattach? else channel. = end end end else channels[name] ||= channel_klass.new(client, name, ) end end |
#initialize(client, channel_klass) ⇒ Object
7 8 9 10 11 |
# File 'lib/ably/modules/channels_collection.rb', line 7 def initialize(client, channel_klass) @client = client @channel_klass = channel_klass @channels = {} end |
#release(name) ⇒ void
This method returns an undefined value.
Destroy the Channel and releases the associated resources.
Releasing a Channel is not typically necessary as a channel consumes no resources other than the memory footprint of the Channel object. Explicitly release channels to free up resources if required
61 62 63 |
# File 'lib/ably/modules/channels_collection.rb', line 61 def release(name) channels.delete(name) end |