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

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


60
61
62
# File 'lib/ably/modules/channels_collection.rb', line 60

def length
  channels.length
end

Instance Method Details

#each(&block) ⇒ Object


67
68
69
70
# File '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

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)

41
42
43
# File '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

Parameters:

  • name (String)

    The name of the channel

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

    Channel options including the encryption options

Returns:

  • (Channel)

20
21
22
23
24
25
26
27
28
# File 'lib/ably/modules/channels_collection.rb', line 20

def get(name, channel_options = {})
  if channels.has_key?(name)
    channels[name].tap do |channel|
      channel.update_options channel_options if channel_options && !channel_options.empty?
    end
  else
    channels[name] ||= channel_klass.new(client, name, channel_options)
  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

Parameters:

  • name (String)

    The name of the channel


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

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