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].freeze
AVAILABLE_FIELDS =

List of all the available fields to make up a connection

%w[
  adapter
  host
  username
  password
  database
  socket
  port
  encoding
  batch_size
].freeze
STRING_FIELDS =

List of all fields that should be forced to a string

%w[adapter].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ BaseConnection

Returns a new instance of BaseConnection.



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

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. STRING_FIELDS are forced into string. Example:

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


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mongify/database/base_connection.rb', line 84

def method_missing(method, *args)
  method_name = method.to_s
  if AVAILABLE_FIELDS.include?(method_name.to_s)

    if STRING_FIELDS.include?(method_name.to_s)
      assignment = "@#{method_name} = value.to_s"
    else
      assignment = "@#{method_name} = value"
    end

    class_eval <<-EOF
                    def #{method_name}(value=nil)
                      #{assignment} 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:



64
65
66
# File 'lib/mongify/database/base_connection.rb', line 64

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)


70
71
72
73
# File 'lib/mongify/database/base_connection.rb', line 70

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



58
59
60
# File 'lib/mongify/database/base_connection.rb', line 58

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



36
37
38
39
40
41
42
43
# File 'lib/mongify/database/base_connection.rb', line 36

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

#valid?Boolean

Ensures the required fields are filled

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
# File 'lib/mongify/database/base_connection.rb', line 46

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