Sha256: 3857ac666381949358b2fb2ada6c0b295aa3aeca96ac954f28e699c518c290ab

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Mysql2xxxx
  class Properties
    attr_reader :options
    def initialize(options = {})
      @options = options.dup
      @options.stringify_keys!
    end
    
    def database_config
      {
        :username => user,
        :password => password,
        :host => host,
        :port => port,
        :database => database
      }
    end

    def user
      options['user'] || active_record_config.try(:[], :username)
    end
    
    def password
      options['password'] || active_record_config.try(:[], :password)
    end
    
    def host
      options['host'] || active_record_config.try(:[], :host)
    end
    
    def port
      options['port'] || active_record_config.try(:[], :port)
    end
    
    def database
      options['database'] || active_record_connection.try(:current_database)
    end
    
    def execute
      options['execute']
    end
        
    private
    
    def active_record_connection
      if defined? ::ActiveRecord
        ::ActiveRecord::Base.connection
      end
    end
    
    def active_record_config
      if active_record_connection
        active_record_connection.instance_variable_get :@config
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mysql2xxxx-0.0.2 lib/mysql2xxxx/properties.rb
mysql2xxxx-0.0.1 lib/mysql2xxxx/properties.rb