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



67
68
69
# File 'lib/ably/modules/channels_collection.rb', line 67

def length
  channels.length
end

Instance Method Details

#each(&block) ⇒ Object



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

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)


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

Parameters:

Returns:

  • (Channel)


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, channel_options = {})
  if channels.has_key?(name)
    channels[name].tap do |channel|
      if channel_options && !channel_options.empty?
        if channel.respond_to?(:need_reattach?) && channel.need_reattach?
          raise_implicit_options_update
        else
          warn_implicit_options_update
          channel.options = channel_options
        end
      end
    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



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

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