Class: MongoCollection::DB

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject

Returns the value of attribute client.



12
13
14
# File 'lib/mongo_collection.rb', line 12

def client
  @client
end

.dbObject

Returns the value of attribute db.



12
13
14
# File 'lib/mongo_collection.rb', line 12

def db
  @db
end

Class Method Details

.[](key) ⇒ Object



32
33
34
# File 'lib/mongo_collection.rb', line 32

def self.[](key)
    return DB.client[key]
end

.close_connectionObject



42
43
44
45
# File 'lib/mongo_collection.rb', line 42

def self.close_connection 
    DB.client.close
    DB.db = nil
end

.collectionsObject



46
47
48
# File 'lib/mongo_collection.rb', line 46

def self.collections
    DB.db.collections
end

.configure(opts = {}) ⇒ Object

Configure through hash



19
20
21
# File 'lib/mongo_collection.rb', line 19

def self.configure(opts = {})
  opts.each { |k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym }
end

.configure_with(path_to_yaml_file) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/mongo_collection.rb', line 23

def self.configure_with(path_to_yaml_file)
    begin
      config = YAML::load(IO.read(path_to_yaml_file))
    rescue => e
      raise "YAML configuration file couldn't be found: #{e}"
    end
    configure(config)
end

.establish_connectionObject



36
37
38
39
40
# File 'lib/mongo_collection.rb', line 36

def self.establish_connection                  
    Mongo::Logger.logger.level = ::Logger::FATAL
    DB.client = Mongo::Client.new([@config[:addr]], :database => @config[:database])
    DB.db = @client.database
end

.hashObject



53
54
55
56
57
# File 'lib/mongo_collection.rb', line 53

def self.hash 
    md5 = Digest::MD5.new
    md5 << DB.collections.map{|x| x.count}.pack("C*")
    md5.hexdigest
end

.infoObject



49
50
51
# File 'lib/mongo_collection.rb', line 49

def self.info
    self.db.command(dbstats: 1).first
end