Class: SequelDbConfig::Config
- Inherits:
-
Object
- Object
- SequelDbConfig::Config
- Includes:
- Singleton
- Defined in:
- lib/sequel_db_config.rb
Overview
Your code goes here…
Constant Summary collapse
- DEFAULT_CONFIG_FILE =
"dbconfig.yaml"
Instance Attribute Summary collapse
-
#connection_string ⇒ Object
readonly
Returns the value of attribute connection_string.
Instance Method Summary collapse
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #load(file) ⇒ Object
- #make_options(opts) ⇒ Object
-
#migrate(path, version = nil) ⇒ Object
Execute migration If version is nil, sequel will migrate to the latest version.
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
15 16 17 18 19 20 21 |
# File 'lib/sequel_db_config.rb', line 15 def initialize @connection_string = "" begin self.load(DEFAULT_CONFIG_FILE) rescue end end |
Instance Attribute Details
#connection_string ⇒ Object (readonly)
Returns the value of attribute connection_string.
13 14 15 |
# File 'lib/sequel_db_config.rb', line 13 def connection_string @connection_string end |
Instance Method Details
#load(file) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sequel_db_config.rb', line 23 def load(file) begin File.open(file) do |f| conf = YAML.load(f.read) db_driver = conf["db_driver"] db_host = conf["db_host"] db_name = conf["db_name"] db_user = conf["db_user"] db_password = conf["db_password"] = { max_connections: conf["max_connections"], pool_timeout: conf["pool_timeout"] } option_string = self.() @connection_string = "#{db_driver}://#{db_user}:#{db_password}@#{db_host}/#{db_name}#{option_string}" end rescue raise "Cannot open configuration file." end end |
#make_options(opts) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sequel_db_config.rb', line 45 def (opts) option_string = "" opts.each do |key, value| if value if option_string == "" option_string = "?" else option_string += "&" end option_string += "#{key}=#{value}" end end option_string end |
#migrate(path, version = nil) ⇒ Object
Execute migration If version is nil, sequel will migrate to the latest version.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sequel_db_config.rb', line 62 def migrate(path, version = nil) if @connection_string == "" raise "Configuration is not loaded." end unless Dir.exist?(path) raise "Migration dir not exists. (#{path})" end Sequel.extension :migration db = Sequel.connect(@connection_string) if version Sequel::Migrator.run(db, path, target: version.to_i) else Sequel::Migrator.run(db, path) end end |