class AppConfig::Storage::Mongo

Mongo storage method.

Constants

DEFAULTS

Public Class Methods

new(options) click to toggle source
# File lib/app_config/storage/mongo.rb, line 18
def initialize(options)
  # Allows passing `true` as an option.
  if options.is_a?(Hash)
    @options = DEFAULTS.merge(options)
  else
    @options = DEFAULTS
  end

  setup_connection!
  fetch_data!
end

Public Instance Methods

save!() click to toggle source

Saves the data back to Mongo. Returns `true`/`false`.

# File lib/app_config/storage/mongo.rb, line 31
def save!
  if @_id
    retval = collection.update({ '_id' => @_id}, @data.to_hash)
  else
    retval = collection.save(@data.to_hash)
  end

  !!retval
end

Private Instance Methods

authenticate_connection!() click to toggle source
# File lib/app_config/storage/mongo.rb, line 43
def authenticate_connection!
  database.authenticate(@options[:username], @options[:password])
end
collection() click to toggle source
# File lib/app_config/storage/mongo.rb, line 51
def collection
  @collection ||= database.collection(@options[:collection])
end
connected?() click to toggle source
# File lib/app_config/storage/mongo.rb, line 47
def connected?
  @connection && @connection.connected?
end
database() click to toggle source
# File lib/app_config/storage/mongo.rb, line 55
def database
  @database ||= @connection.db(@options[:database])
end
fetch_data!() click to toggle source
# File lib/app_config/storage/mongo.rb, line 59
def fetch_data!
  raise 'Not connected to MongoDB' unless connected?

  @data = Storage::ConfigData.new(collection.find_one)
  @_id = @data._id
end
setup_connection!() click to toggle source
# File lib/app_config/storage/mongo.rb, line 66
def setup_connection!
  @connection = ::Mongo::Connection.new(@options[:host], @options[:port].to_i)
  authenticate_connection! if @options[:username] && @options[:password]
end