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

Instance Method Summary collapse

Instance Attribute Details

#lengthInteger (readonly) Also known as: count, size

Returns number of channels created.

Returns:

  • (Integer)

    number of channels created



54
55
56
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 54

def length
  channels.length
end

Instance Method Details

#each(&block) ⇒ Object



61
62
63
64
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 61

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

Parameters:

  • name (String)

    The name of the channel

Yields:

  • (options)

    (optional) if a missing_block is passed to this method and no channel exists matching the name, this block is called

Yield Parameters:

  • name (String)

    of the missing channel

Returns:

  • (Channel)


35
36
37
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 35

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

Parameters:

  • name (String)

    The name of the channel

  • channel_options (Hash) (defaults to: {})

    Channel options including the encryption options

Returns:

  • (Channel)


20
21
22
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 20

def get(name, channel_options = {})
  channels[name] ||= channel_klass.new(client, name, channel_options)
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

Parameters:

  • name (String)

    The name of the channel



48
49
50
# File 'lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb', line 48

def release(name)
  channels.delete(name)
end