module Ably::Modules::ChannelsCollection

ChannelsCollection module provides common functionality to the Rest and Realtime Channels objects such as get, [], fetch, and release

Public Class Methods

new(client, channel_klass) click to toggle source
# 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

Public Instance Methods

[](name, channel_options = {})
Alias for: get
count()
Alias for: length
each(&block) click to toggle source

Method to allow {ChannelsCollection} to be {ruby-doc.org/core-2.1.3/Enumerable.html Enumerable}

# File lib/submodules/ably-ruby/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, &missing_block) click to toggle source

Return a Channel for the given name if it exists, else the block will be called. This method is intentionally similar to {ruby-doc.org/core-2.1.3/Hash.html#method-i-fetch Hash#fetch} providing a simple way to check if a channel exists or not without creating one

@param name [String] The name of the channel

@yield [options] (optional) if a missing_block is passed to this method and no channel exists matching the name, this block is called @yieldparam [String] name of the missing channel

@return [Channel]

# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 48
def fetch(name, &missing_block)
  channels.fetch(name, &missing_block)
end
get(name, channel_options = {}) click to toggle source

Return a Channel for the given name

@param name [String] The name of the channel @param channel_options [Hash, Ably::Models::ChannelOptions] A hash of options or a {Ably::Models::ChannelOptions}

@return [Channel]

# File lib/submodules/ably-ruby/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
Also aliased as: []
length() click to toggle source

@!attribute [r] length @return [Integer] number of channels created

# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 67
def length
  channels.length
end
Also aliased as: count, size
release(name) click to toggle source

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

@param name [String] The name of the channel

@return [void]

# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 61
def release(name)
  channels.delete(name)
end
size()
Alias for: length

Private Instance Methods

channel_klass() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 97
def channel_klass
  @channel_klass
end
channels() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 101
def channels
  @channels
end
client() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 93
def client
  @client
end
logger() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 89
def logger
  client.logger
end
raise_implicit_options_update() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 81
def raise_implicit_options_update
  raise ArgumentError, "You are trying to indirectly update channel options which will trigger reattachment of the channel. Please use Channel#set_options directly if you wish to continue"
end
warn_implicit_options_update() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/channels_collection.rb, line 85
def warn_implicit_options_update
  logger.warn { "Channels#get: Using this method to update channel options is deprecated and may be removed in a future version of ably-ruby. Please use Channel#setOptions instead" }
end