Class: Connector::MongoConnector

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

Overview

Defines MongoDB connection properties

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017') ⇒ MongoConnector

Returns a new instance of MongoConnector.



11
12
13
14
15
16
17
# File 'lib/mylookup/connector.rb', line 11

def initialize(coll_name, db_name: 'ccsdm', host: 'localhost', port: '27017')
    @coll_name, @db_name, @host, @port = coll_name, db_name, host, port
    Mongo::Logger.logger.level = Logger::WARN
    @conn_str = @host + ':' + @port
    @client = Mongo::Client.new([@conn_str], :database => @db_name)
    @coll = @client[@coll_name]
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/mylookup/connector.rb', line 9

def client
  @client
end

#collObject

Returns the value of attribute coll.



9
10
11
# File 'lib/mylookup/connector.rb', line 9

def coll
  @coll
end

#coll_nameObject

Returns the value of attribute coll_name.



9
10
11
# File 'lib/mylookup/connector.rb', line 9

def coll_name
  @coll_name
end

#db_nameObject

Returns the value of attribute db_name.



9
10
11
# File 'lib/mylookup/connector.rb', line 9

def db_name
  @db_name
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/mylookup/connector.rb', line 9

def host
  @host
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/mylookup/connector.rb', line 9

def port
  @port
end

Instance Method Details

#collection_exists?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/mylookup/connector.rb', line 24

def collection_exists?
    recs({}) > 0
end

#field_exists?(field) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/mylookup/connector.rb', line 28

def field_exists? field
    @coll.find({}).projection({ '_id' => 0 }).limit(1).collect { |doc| doc }[0].keys.include? field
end

#recs(qry) ⇒ Object

Queries and returns number of documents in the collection



20
21
22
# File 'lib/mylookup/connector.rb', line 20

def recs qry
    @coll.find(qry).count
end