Module: Ably::Modules::ChannelsCollection
- Includes:
- Enumerable
- Included in:
- Rest::Channels
- Defined in:
- lib/submodules/ably-ruby/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.
60 61 62 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 60 def length channels.length end |
Instance Method Details
#each(&block) ⇒ Object
Method to allow Ably::Modules::ChannelsCollection to be Enumerable
67 68 69 70 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 67 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
41 42 43 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 41 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 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 20 def get(name, = {}) if channels.has_key?(name) channels[name].tap do |channel| channel. if && !.empty? end else channels[name] ||= channel_klass.new(client, name, ) end end |
#initialize(client, channel_klass) ⇒ Object
7 8 9 10 11 |
# File 'lib/submodules/ably-ruby/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
54 55 56 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 54 def release(name) channels.delete(name) end |