lib/dbi/dbrc.rb in dbi-dbrc-1.1.0 vs lib/dbi/dbrc.rb in dbi-dbrc-1.1.1
- old
+ new
@@ -1,21 +1,24 @@
-if PLATFORM.match("mswin")
- require "win32/file"
+if RUBY_PLATFORM.match('mswin')
+ require 'win32/file'
+ require 'win32/dir'
end
-require "sys/admin"
+require 'sys/admin'
include Sys
+# The DBI module serves as a namespace only.
module DBI
- # The base error class for dbi-dbrc. If anything goes wrong, this is
- # the error that is raised. A subclass of StandardError.
- class DBRCError < StandardError; end
-
- # The main class.
+ # The DBRC class encapsulates a database resource config file.
class DBRC
- VERSION = "1.1.0"
+
+ # This error is raised if anything fails trying to read the config file.
+ # the error that is raised.
+ class Error < StandardError; end
+
+ VERSION = '1.1.1'
attr_accessor :database, :user, :password, :driver, :dsn
attr_accessor :maximum_reconnects, :timeout, :interval, :dbrc_dir
# Returns a new DBRC object. The contents of the object depend on the
# arguments passed to the constructor. If only a database name is
@@ -26,67 +29,72 @@
# If a directory is passed as the third argument, then DBRC will look
# in that directory, instead of the default directory, for the .dbrc
# file.
#
# If an entry cannot be found for the database, or database plus user
- # combination, then a DBRCError is raised. If the .dbrc file cannot
+ # combination, then a Error is raised. If the .dbrc file cannot
# be found, or is setup improperly with regards to permissions or
- # properties, a DBRCError is raised.
- def initialize(database,user=nil,dbrc_dir=nil)
+ # properties, a Error is raised.
+ #
+ def initialize(database, user=nil, dbrc_dir=nil)
if dbrc_dir.nil?
- if PLATFORM.match("mswin")
- home = ENV["USERPROFILE"] || ENV["HOME"]
+ if RUBY_PLATFORM.match('mswin')
+ home = ENV['USERPROFILE'] || ENV['HOME']
file = nil
if home
- file = home + "\\.dbrc"
+ file = File.join(home, '.dbrc')
else
- file = "C:\\Documents and Settings\\"
- file += Admin.get_user(Admin.get_login).home_dir + "\\.dbrc"
+ file = File.join(File.basename(Dir::APPDATA), '.dbrc')
end
@dbrc_file = file
else
- @dbrc_file = Admin.get_user(Process.uid).dir + "/.dbrc"
+ @dbrc_file = File.join(Admin.get_user(Process.uid).dir, '.dbrc')
end
else
- @dbrc_file = dbrc_dir + "/.dbrc"
+ @dbrc_file = File.join(dbrc_dir, '.dbrc')
end
@database = database
@user = user
encrypted = false # Win32 only
+ @driver = nil
+ @interval = nil
+ @timeout = nil
+ @maximum_reconnects = nil
+
check_file()
# If on Win32 and the file is encrypted, decrypt it.
- if PLATFORM.match("mswin") && File.encrypted?(@dbrc_file)
+ if RUBY_PLATFORM.match("mswin") && File.encrypted?(@dbrc_file)
encrypted = true
File.decrypt(@dbrc_file)
end
parse_dbrc_config_file()
validate_data()
convert_numeric_strings()
create_dsn_string()
# If on Win32 and the file was encrypted, re-encrypt it
- if PLATFORM.match("mswin") && encrypted
+ if RUBY_PLATFORM.match("mswin") && encrypted
File.encrypt(@dbrc_file)
end
end
private
# Ensure that the user/password has been set
def validate_data
unless @user
- raise DBRCError, "no user found associated with #{@database}"
+ raise Error, "no user found associated with #{@database}"
end
unless @password
- raise DBRCError, "password not defined for #{@user}@#{@database}"
+ raise Error, "password not defined for #{@user}@#{@database}"
end
end
# Converts strings that should be numbers into actual numbers
def convert_numeric_strings
@@ -104,23 +112,23 @@
def check_file(file=@dbrc_file)
File.open(file){ |f|
# Permissions must be set to 600 or better on Unix systems.
# Must be hidden on Win32 systems.
- if PLATFORM.match("mswin")
+ if RUBY_PLATFORM.match("mswin")
unless File.hidden?(file)
- raise DBRCError, "The .dbrc file must be hidden"
+ raise Error, "The .dbrc file must be hidden"
end
else
unless (f.stat.mode & 077) == 0
- raise DBRCError, "Bad .dbrc file permissions"
+ raise Error, "Bad .dbrc file permissions"
end
end
# Only the owner may use it
unless f.stat.owned?
- raise DBRCError, "Not owner of .dbrc file"
+ raise Error, "Not owner of .dbrc file"
end
}
end
# Parse the text out of the .dbrc file. This is the only method you
@@ -149,11 +157,12 @@
if @user
err = "no record found for #{@user}@#{@database}"
else
err = "no record found for #{@database}"
end
- raise DBRCError, err
+
+ raise Error, err
end
alias_method(:db,:database)
alias_method(:db=,:database=)
alias_method(:passwd,:password)
@@ -185,11 +194,11 @@
end
}
return
}
# If we reach here it means the database and/or user wasn't found
- raise DBRCError, "No record found for #{@user}@#{@database}"
+ raise Error, "No record found for #{@user}@#{@database}"
end
end
# A subclass of DBRC designed to handle .dbrc files in YAML format. The
# public methods of this class are identical to DBRC.
@@ -212,9 +221,9 @@
@maximum_reconnects = info["max_reconn"]
return
}
}
# If we reach this point, it means the database wasn't found
- raise DBRCError, "No entry found for #{@user}@#{@database}"
+ raise Error, "No entry found for #{@user}@#{@database}"
end
end
end