Class: MongoODM::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_odm/config.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Config) initialize(opts = {})

Returns a new instance of Config



8
9
10
11
12
13
14
15
# File 'lib/mongo_odm/config.rb', line 8

def initialize(opts = {})
  from_hash opts
  @database ||= 'test'
  @host ||= 'localhost'
  @port || 27017
  @logger ||= Rails.logger  if defined?(Rails)
  @pool_size ||= 1
end

Instance Attribute Details

- (Object) database

Returns the value of attribute database



5
6
7
# File 'lib/mongo_odm/config.rb', line 5

def database
  @database
end

- (Object) host

Returns the value of attribute host



5
6
7
# File 'lib/mongo_odm/config.rb', line 5

def host
  @host
end

- (Object) logger

Returns the value of attribute logger



6
7
8
# File 'lib/mongo_odm/config.rb', line 6

def logger
  @logger
end

- (Object) password

Returns the value of attribute password



5
6
7
# File 'lib/mongo_odm/config.rb', line 5

def password
  @password
end

- (Object) pool_size

Returns the value of attribute pool_size



6
7
8
# File 'lib/mongo_odm/config.rb', line 6

def pool_size
  @pool_size
end

- (Object) port

Returns the value of attribute port



5
6
7
# File 'lib/mongo_odm/config.rb', line 5

def port
  @port
end

- (Object) username

Returns the value of attribute username



5
6
7
# File 'lib/mongo_odm/config.rb', line 5

def username
  @username
end

Instance Method Details

- (Object) connection



37
38
39
40
41
42
43
44
45
# File 'lib/mongo_odm/config.rb', line 37

def connection
  opts = { :logger => self.logger, :pool_size => self.pool_size }
  Mongo::Connection.new(self.host, self.port, opts).tap do |conn|
    if self.username || self.password
      conn.add_auth(self.database, self.username, self.password)
      conn.apply_saved_authentication
    end
  end
end

- (Object) from_hash(opts)



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongo_odm/config.rb', line 23

def from_hash(opts)
  opts = opts.dup.symbolize_keys!

  if opts[:uri].present?
    self.uri = opts[:uri]
  else
    @database, @host, @port, @username, @password = opts.values_at(:database, :host, :port, :username, :password)
    @port &&= Integer(@port)
  end

  @logger ||= opts[:logger]
  @pool_size ||= opts[:pool_size]
end

- (Object) uri=(uri)



17
18
19
20
21
# File 'lib/mongo_odm/config.rb', line 17

def uri=(uri)
  uri = URI.parse(uri)  unless URI === uri
  @database = uri.path.to_s.sub('/', '')
  @host, @port, @username, @password = uri.host, uri.port, uri.user, uri.password
end