Sha256: 7fff723346130e5c055d44f688b5683ab12c21afb864192e224cfc46d0d864da

Contents?: true

Size: 1011 Bytes

Versions: 2

Compression:

Stored size: 1011 Bytes

Contents

require 'faye_shard/shard'
require 'faye_shard/user/faye'

class FayeShards

  class << self

    #  Returns configuration for current Rails environment.
    #
    #  * <tt>env</tt>:: Rails environment, defaults to +Rails.env+.
    #
    def config
      @config ||= YAML.load(File.read(Rails.root.to_s + "/config/faye.yml"))
      @config[Rails.env]
    end

    #  Returns the object which represents a connection to a specific shard. The object is effectively
    #  an instance of +FayeShards::Shard+ class which can be found in this gem.
    #
    #  * <tt>id</tt>:: Id (an integer, string of whatever) to perform sharding based on.
    #
    def shard(id)
      @shards ||= {}

      shard_configs = config["shards"]

      @shards[id % shard_configs.size] ||= FayeShard::Shard.new(shard_configs[id % shard_configs.size])
    end

    #  Returns all configured shards
    #
    def all_shards
      @shards ||= config["shards"].map{ |shard_config| FayeShard::Shard.new(shard_config) }
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faye_shards-0.1.1 lib/faye_shards.rb
faye_shards-0.1.0 lib/faye_shards.rb