Class: Mongify::Database::BaseConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/database/base_connection.rb

Overview

This is a Basic configuration for any sql or non sql database

Direct Known Subclasses

NoSqlConnection, SqlConnection

Constant Summary collapse

REQUIRED_FIELDS =

List of required fields to make a valid base connection

%w{host}
AVAILABLE_FIELDS =

List of all the available fields to make up a connection

%w{adapter host username password database socket port encoding}

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ BaseConnection

Returns a new instance of BaseConnection.



13
14
15
16
17
18
19
20
# File 'lib/mongify/database/base_connection.rb', line 13

def initialize(options=nil)
  if options
    options.stringify_keys!
    options.each do |key, value|
       instance_variable_set "@#{key.downcase}", value if AVAILABLE_FIELDS.include?(key.downcase)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Building set and/or return functions for AVAILABLE_FIELDS Example:

def host(value=nil)
  @host = value.to_s unless value.nil?
  @host
end


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mongify/database/base_connection.rb', line 67

def method_missing(method, *args)
  method_name = method.to_s
  if AVAILABLE_FIELDS.include?(method_name.to_s)
    class_eval <<-EOF
                    def #{method_name}(value=nil)
                      @#{method_name} = value.to_s unless value.nil?
                      @#{method_name}
                    end
                  EOF
    value = args.first if args.size > 0
    send(method,value)
  else
    super(method, args)
  end

end

Instance Method Details

#has_connection?Boolean

Used to test connection, Raises NotImplementedError because it needs to be setup in BaseConnection’s children

Returns:

  • (Boolean)

Raises:



48
49
50
# File 'lib/mongify/database/base_connection.rb', line 48

def has_connection?
  raise NotImplementedMongifyError
end

#respond_to?(method, *args) ⇒ Boolean

Returns true if we are trying to respond_to AVAILABLE_FIELDS functions

Returns:

  • (Boolean)


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

def respond_to?(method, *args) 
  return true if AVAILABLE_FIELDS.include?(method.to_s)
  super(method)
end

#setup_connection_adapterObject

Used to setup connection, Raises NotImplementedError because it needs to be setup in BaseConnection’s children



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

def setup_connection_adapter
  raise NotImplementedMongifyError
end

#to_hashObject

Returns all settings as a hash, this is used mainly in building ActiveRecord::Base.establish_connection



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

def to_hash
  hash = {}
  instance_variables.each do |variable|
    value = self.instance_variable_get variable
    hash[variable.to_s.gsub('@','').to_sym] = value unless value.nil?
  end
  hash
end

#valid?Boolean

Ensures the required fields are filled

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
# File 'lib/mongify/database/base_connection.rb', line 33

def valid?
  #TODO: Improve this to create an errors array with detailed errors (or maybe just use activemodel)
  REQUIRED_FIELDS.each do |require_field|
    return false unless instance_variables.map(&:to_s).include?("@#{require_field}") and
                        !instance_variable_get("@#{require_field}").to_s.empty?
  end
  true
end