Class: Sinatra::Monk::MBase

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra-monk/2.0.rb,
lib/sinatra-monk/monk.rb

Overview

Provides a base class for Mongo database management. This is the simplest way to connect to Mongo with Monk. It is intented to keep Sinatra’s philosofy of keeping it as simple as possible for use.

Direct Known Subclasses

MHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user = '', pass = '', database = 'local', host = 'localhost', port = 27017, opts = {:connect => false}) ⇒ MBase

(Any Ruby) Setup the Monk with the data that was sent. Autoconnect if :connect flag is true.

Parameters:

  • user (String) (defaults to: '')

    the database username,

  • pass (String) (defaults to: '')

    the password of the previous database user,

  • database (String) (defaults to: 'local')

    the name of the database,

  • host (String) (defaults to: 'localhost')

    the host of the database,

  • port (Fixnum) (defaults to: 27017)

    the port to connect to the database,

  • opts (Hash) (defaults to: {:connect => false})

    monk options (set :conect to autoconnect the Monk)



47
48
49
50
# File 'lib/sinatra-monk/2.0.rb', line 47

def initialize(user:'', pass:'', database:'local', host:'localhost',
               port:27017, opts:{:connect => false})
  _ol_initialize(user, pass, database, host, port, opts)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



34
35
36
# File 'lib/sinatra-monk/monk.rb', line 34

def client
  @client
end

#databaseObject (readonly)

Returns the value of attribute database.



34
35
36
# File 'lib/sinatra-monk/monk.rb', line 34

def database
  @database
end

#passwordObject (readonly)

Returns the value of attribute password.



34
35
36
# File 'lib/sinatra-monk/monk.rb', line 34

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



34
35
36
# File 'lib/sinatra-monk/monk.rb', line 34

def username
  @username
end

Instance Method Details

#_ol_initializeObject

Aliased initialize method, made to add Ruby 2.0 version.



37
# File 'lib/sinatra-monk/2.0.rb', line 37

alias _ol_initialize initialize

#closeObject

Closes the connection with the server, you MAY NOT TRY to alter the collection while in that state.



60
61
62
# File 'lib/sinatra-monk/monk.rb', line 60

def close
  @client.close
end

#connect(user = @username, pass = @password) ⇒ Object

Conects the Monk to Mongo.

Parameters:

  • user (String) (defaults to: @username)

    the database username,

  • pass (String) (defaults to: @password)

    the password of the previous database user.



72
73
74
75
76
77
78
# File 'lib/sinatra-monk/monk.rb', line 72

def connect user = @username, pass = @password
  return true if connected?
  @client.connect
  @username  = user
  @password  = pass
  return @database.authenticate @username, @password
end

#connected?Boolean

Returns the Monk connection state.

Returns:

  • (Boolean)


65
66
67
# File 'lib/sinatra-monk/monk.rb', line 65

def connected?
  return @client.connected?
end