Class: Mongify::Database::NoSqlConnection

Inherits:
BaseConnection show all
Includes:
Mongo
Defined in:
lib/mongify/database/no_sql_connection.rb

Overview

No Sql Connection configuration

Basic format should look something like this:

no_sql_connection {options} do
  adapter   "mongodb"
  host      "localhost"
  database  "my_database"
end

Possible attributes:

adapter
host
database
username
password
port

Options:

:force => true       # This will force a database drop before processing

You’re also able to set attributes via the options

Constant Summary collapse

REQUIRED_FIELDS =

Required fields for a no sql connection

%w{host database}

Constants inherited from BaseConnection

BaseConnection::AVAILABLE_FIELDS

Instance Method Summary collapse

Methods inherited from BaseConnection

#method_missing, #respond_to?, #to_hash

Constructor Details

#initialize(options = {}) ⇒ NoSqlConnection

Returns a new instance of NoSqlConnection.



34
35
36
37
38
# File 'lib/mongify/database/no_sql_connection.rb', line 34

def initialize(options={})
  super options
  @options = options
  adapter 'mongodb' if adapter.nil? || adapter == 'mongo'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongify::Database::BaseConnection

Instance Method Details

#adapter(name = nil) ⇒ Object

Sets and/or returns a adapter It takes care of renaming adapter(‘mongo’) to ‘mongodb’



42
43
44
45
# File 'lib/mongify/database/no_sql_connection.rb', line 42

def adapter(name=nil)
  name = 'mongodb' if name && name.to_s.downcase == 'mongo'
  super(name)
end

#ask_to_drop_databaseObject

Asks user permission to drop the database

Returns:

  • true or false depending on user’s response



138
139
140
141
142
# File 'lib/mongify/database/no_sql_connection.rb', line 138

def ask_to_drop_database
  if UI.ask("Are you sure you want to drop #{database} database?")
    drop_database
  end
end

#connectionObject

Returns a mongo connection NOTE: If forced? is true, the first time a connection is made, it will ask to drop the database before continuing



75
76
77
78
79
# File 'lib/mongify/database/no_sql_connection.rb', line 75

def connection
  return @connection if @connection
  @connection = setup_connection_adapter
  @connection
end

#connection_stringObject

Returns a connection string that can be used to build a Mongo Connection (Currently this isn’t used due to some issue early on in development)



49
50
51
# File 'lib/mongify/database/no_sql_connection.rb', line 49

def connection_string
  "#{@adapter}://#{@host}#{":#{@port}" if @port}"
end

#create_pre_mongified_id_index(collection_name) ⇒ Object

Creates a pre_mongified_id index to ensure speedy lookup for collections via the pre_mongified_id



132
133
134
# File 'lib/mongify/database/no_sql_connection.rb', line 132

def create_pre_mongified_id_index(collection_name)
  db[collection_name].create_index([['pre_mongified_id', Mongo::ASCENDING]])
end

#dbObject

Returns the database from the connection



87
88
89
# File 'lib/mongify/database/no_sql_connection.rb', line 87

def db
  @db ||= connection[database]
end

#drop_mongified_index(collection_name) ⇒ Object

Removes pre_mongified_id from collection

Parameters:

  • collection_name (String)

    name of collection to remove the index from

Returns:

  • True if successful

Raises:

  • MongoDBError if index isn’t found



126
127
128
# File 'lib/mongify/database/no_sql_connection.rb', line 126

def drop_mongified_index(collection_name)
  db[collection_name].drop_index('pre_mongified_id_1') if db[collection_name].index_information.keys.include?("pre_mongified_id_1")
end

#find_one(collection_name, query) ⇒ Object

Finds one item from a collection with the given query



107
108
109
# File 'lib/mongify/database/no_sql_connection.rb', line 107

def find_one(collection_name, query)
  db[collection_name].find_one(query)
end

#forced?Boolean

Returns true if :force was set to true This will force a drop of the database upon connection

Returns:

  • (Boolean)


61
62
63
# File 'lib/mongify/database/no_sql_connection.rb', line 61

def forced?
  !!@options['force']
end

#get_id_using_pre_mongified_id(colleciton_name, pre_mongified_id) ⇒ Object

Returns a row of a item from a given collection with a given pre_mongified_id



112
113
114
# File 'lib/mongify/database/no_sql_connection.rb', line 112

def get_id_using_pre_mongified_id(colleciton_name, pre_mongified_id)
  db[colleciton_name].find_one('pre_mongified_id' => pre_mongified_id).try(:[], '_id')
end

#has_connection?Boolean

Returns true or false depending if we have a connection to a mongo server

Returns:

  • (Boolean)


82
83
84
# File 'lib/mongify/database/no_sql_connection.rb', line 82

def has_connection?
  connection.connected?
end

#insert_into(colleciton_name, row) ⇒ Object

Inserts into the collection a given row



97
98
99
# File 'lib/mongify/database/no_sql_connection.rb', line 97

def insert_into(colleciton_name, row)
  db[colleciton_name].insert(row, :safe => true)
end

#remove_pre_mongified_ids(collection_name) ⇒ Object

Removes pre_mongified_id from all records in a given collection



117
118
119
120
# File 'lib/mongify/database/no_sql_connection.rb', line 117

def remove_pre_mongified_ids(collection_name)
  drop_mongified_index(collection_name)
  db[collection_name].update({}, { '$unset' => { 'pre_mongified_id' => 1} }, :multi => true)
end

#select_rows(collection) ⇒ Object

Returns a hash of all the rows from the database of a given collection



92
93
94
# File 'lib/mongify/database/no_sql_connection.rb', line 92

def select_rows(collection)
  db[collection].find
end

#setup_connection_adapterObject

Sets up a connection to the database



66
67
68
69
70
# File 'lib/mongify/database/no_sql_connection.rb', line 66

def setup_connection_adapter
  connection = Connection.new(host, port)
  connection.add_auth(database, username, password) if username && password
  connection
end

#update(colleciton_name, id, attributes) ⇒ Object

Updates a collection item with a given ID with the given attributes



102
103
104
# File 'lib/mongify/database/no_sql_connection.rb', line 102

def update(colleciton_name, id, attributes)
  db[colleciton_name].update({"_id" => id}, attributes)
end

#valid?Boolean

Returns true or false depending if the given attributes are present and valid to make up a connection to a mongo server

Returns:

  • (Boolean)


55
56
57
# File 'lib/mongify/database/no_sql_connection.rb', line 55

def valid?
  super && @database.present?
end