Class: Querrel::Querreller

Inherits:
Object
  • Object
show all
Includes:
MapReduce
Defined in:
lib/querrel/querreller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MapReduce

#map, #query, #reduce

Constructor Details

#initialize(dbs, options = {}) ⇒ Querreller

Returns a new instance of Querreller.



10
11
12
# File 'lib/querrel/querreller.rb', line 10

def initialize(dbs, options = {})
  @connection_resolver = ConnectionResolver.new(dbs, options[:db_names])
end

Instance Attribute Details

#connection_resolverObject

Returns the value of attribute connection_resolver.



8
9
10
# File 'lib/querrel/querreller.rb', line 8

def connection_resolver
  @connection_resolver
end

Instance Method Details

#retrieve_connection_spec(db, resolver) ⇒ Object



34
35
36
# File 'lib/querrel/querreller.rb', line 34

def retrieve_connection_spec(db, resolver)
  resolver.spec(db.to_sym)
end

#run(options = {}, &blk) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/querrel/querreller.rb', line 14

def run(options = {}, &blk)
  if options.key?(:on)
    resolver = ConnectionResolver.new(options[:on], !!options[:db_names])
    dbs = resolver.configurations.keys
  else
    resolver = @connection_resolver
    dbs = @connection_resolver.configurations.keys
  end

  threads = []
  dbs.each do |db|
    threads << Thread.new do
      con_spec = retrieve_connection_spec(db, resolver)
      Thread.current[:querrel_con_spec] = con_spec
      yield(ConnectedModelFactory)
    end
  end
  threads.each(&:join)
end

#while_connected_to(db, resolver, &b) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/querrel/querreller.rb', line 38

def while_connected_to(db, resolver, &b)
  conf = resolver.spec(db.to_sym)
  pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(conf)
  pool.with_connection(&b)
ensure
  pool.disconnect!
end