Module: Nightcrawler::Manager

Defined in:
lib/nightcrawler/manager.rb

Class Method Summary collapse

Class Method Details

.create_shard(from_class, key, config) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nightcrawler/manager.rb', line 34

def create_shard(from_class, key, config)
  raise "Could not find shard for key #{key}" unless config
  class_name = "#{from_class.prefix}#{key.to_s.capitalize}"
  klass = Class.new ActiveRecord::Base
  if class_name.split("::").length > 1
    _module = class_name.split("::").first
    _class = class_name.split("::").last
    eval(_module).module_eval{self.const_set _class, klass}
  else
    Object.const_set class_name, klass
  end
  klass.class_eval do
    establish_connection config
    include Nightcrawler::Shard
    class << self
      attr_accessor :shard_id
    end

    def self.sharded?
      false
    end
  end
  klass.shard_id = key
  klass
end

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nightcrawler/manager.rb', line 3

def self.included(klass)
  class << klass
    attr_reader :prefix
    def shards
      Nightcrawler::Manager.mutex.synchronize do
        @shards ||= Nightcrawler::Manager.shards_from_descriptions self, shard_descriptions
      end
    end

    def shard(key)
      shards[key]
    end

    def shard_prefix(prefix)
      @prefix = prefix
    end
  end
end

.mutexObject



24
25
26
# File 'lib/nightcrawler/manager.rb', line 24

def mutex
  @mutex ||= Mutex.new
end

.shards_from_descriptions(from_class, description_hash) ⇒ Object



28
29
30
31
32
# File 'lib/nightcrawler/manager.rb', line 28

def shards_from_descriptions(from_class, description_hash)
  Hash[description_hash.collect do |key, config|
    [key, create_shard(from_class, key, config)]
  end]
end