Sha256: 88960c7e50e2396cd8c656184be733302f08174974d0b6c575fd91c579dd499d

Contents?: true

Size: 869 Bytes

Versions: 6

Compression:

Stored size: 869 Bytes

Contents

module ConnectionManager
  module Shards  
    @shard_class_names = []
    
    def shard_class_names(*shard_class_names)
      @shard_class_names = shard_class_names
    end 
    
    # Takes a block that is call on all available shards.
    def shards(*opts,&shards_block)
      opts = {:include_self => true}.merge(opts.extract_options!)
      raise ArgumentError, "shard_class_names have not been defined for #{self.class.name}" if @shard_class_names.length == 0
      if block_given?   
        results = []
        @shard_class_names.each do |s|
          results << shards_block.call(s.constantize)
        end
        results << shards_block.call(self) if opts[:include_self]
        return results.flatten
      else
        raise ArgumentError, 'shards method requires a block.'
      end
    end
  end
end
ActiveRecord::Base.extend(ConnectionManager::Shards)

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
connection_manager-1.1.5 lib/connection_manager/shards.rb
connection_manager-1.1.4 lib/connection_manager/shards.rb
connection_manager-1.1.3 lib/connection_manager/shards.rb
connection_manager-1.1.2 lib/connection_manager/shards.rb
connection_manager-1.1.1 lib/connection_manager/shards.rb
connection_manager-1.1.0 lib/connection_manager/shards.rb