Class: IsolatedServer::Mongodb

Inherits:
Base
  • Object
show all
Defined in:
lib/isolated_server/mongodb.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#base, #params, #pid

Instance Method Summary collapse

Methods inherited from Base

#cleanup!, #down!, #exec_server, exec_wait, get_free_port, #grab_free_port, #kill!, #locate_executable

Constructor Details

#initialize(options = {}) ⇒ Mongodb

Returns a new instance of Mongodb.



8
9
10
11
# File 'lib/isolated_server/mongodb.rb', line 8

def initialize(options = {})
  super options
  @dbpath         = FileUtils.mkdir("#{@base}/data").first
end

Instance Attribute Details

#dbpathObject (readonly)

Returns the value of attribute dbpath.



6
7
8
# File 'lib/isolated_server/mongodb.rb', line 6

def dbpath
  @dbpath
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/isolated_server/mongodb.rb', line 6

def port
  @port
end

#repl_setObject (readonly)

Returns the value of attribute repl_set.



6
7
8
# File 'lib/isolated_server/mongodb.rb', line 6

def repl_set
  @repl_set
end

Instance Method Details

#boot!Object



13
14
15
16
17
# File 'lib/isolated_server/mongodb.rb', line 13

def boot!
  @port ||= grab_free_port

  up!
end

#connectionObject



43
44
45
# File 'lib/isolated_server/mongodb.rb', line 43

def connection
  @connection ||= connection_klass.new('localhost', @port)
end

#connection_klassObject



47
48
49
50
51
52
53
54
55
# File 'lib/isolated_server/mongodb.rb', line 47

def connection_klass
  if Kernel.const_defined?("Mongo::MongoClient")
    # 1.8.0+
    Mongo::MongoClient
  else
    # < 1.8.0
    Mongo::Connection
  end
end

#consoleObject



57
58
59
# File 'lib/isolated_server/mongodb.rb', line 57

def console
  system(['mongo', '--port', @port].shelljoin)
end

#up!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/isolated_server/mongodb.rb', line 19

def up!
  mongod = locate_executable("mongod")

  exec_server([
    mongod,
    '--dbpath', @dbpath,
    '--port', @port,
    *@params
  ].join(' '))

  until up?
    sleep(0.1)
  end
end

#up?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/isolated_server/mongodb.rb', line 34

def up?
  begin
    connection.ping
    true
  rescue Mongo::ConnectionFailure
    false
  end
end