Sha256: f8452022e53b68d94bfed76ce1c80891f42d633903aee6ad75d194ba497a19e0

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Mysql2xxxx
  class Properties
    attr_reader :options
    def initialize(options = {})
      @options = options.dup
      @options.stringify_keys!
    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) || '127.0.0.1'
    end
    
    # MySQL connection charset
    #
    # If you change this, you also have to change :encoding
    #
    # Default: utf8
    def charset
      options['charset'] || 'utf8'
    end
    
    # Encoding
    #
    # If you change this, you also have to change :charset
    #
    # Default: UTF-8
    def encoding
      options['encoding'] || 'UTF-8'
    end
    
    def port
      options['port'] || active_record_config.try(:[], :port)
    end
    
    def socket
      options['socket'] || active_record_config.try(:[], :socket)
    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
    rescue
      # oh well
    end
    
    def active_record_config
      if active_record_connection
        active_record_connection.instance_variable_get :@config
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mysql2xxxx-0.1.0 lib/mysql2xxxx/properties.rb