Class: Config

Inherits:
Object
  • Object
show all
Defined in:
lib/config.rb

Overview

Read configuration file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/config.rb', line 8

def initialize
  @env = ENV['env']
  @env = 'dev' if @env.nil?
  config = Pyer::Properties.new("config/#{@env}")
  @host      = config.host
  @port      = config.port
  @username  = config.username
  @password  = config.password
  @database  = config.database
  @version0  = config.version0
  @upgrade   = config.upgrade
  @downgrade = config.downgrade
  @verbose   = ENV['verbose'] == 'true'
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



6
7
8
# File 'lib/config.rb', line 6

def database
  @database
end

#downgradeObject (readonly)

Returns the value of attribute downgrade.



6
7
8
# File 'lib/config.rb', line 6

def downgrade
  @downgrade
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/config.rb', line 6

def env
  @env
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/config.rb', line 6

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



6
7
8
# File 'lib/config.rb', line 6

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/config.rb', line 6

def port
  @port
end

#upgradeObject (readonly)

Returns the value of attribute upgrade.



6
7
8
# File 'lib/config.rb', line 6

def upgrade
  @upgrade
end

#usernameObject (readonly)

Returns the value of attribute username.



6
7
8
# File 'lib/config.rb', line 6

def username
  @username
end

#verboseObject (readonly)

Returns the value of attribute verbose.



6
7
8
# File 'lib/config.rb', line 6

def verbose
  @verbose
end

#version0Object (readonly)

Returns the value of attribute version0.



6
7
8
# File 'lib/config.rb', line 6

def version0
  @version0
end

Instance Method Details

#createObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/config.rb', line 23

def create
  Dir.mkdir 'config' unless File.exist?('config')
  File.open("config/#{@env}", 'w') do |f|
    f.write("database  = \n")
    f.write("host      = localhost\n")
    f.write("port      = 5432\n")
    f.write("username  = \n")
    f.write("password  = \n")
    f.write("encoding  = utf8\n")
    f.write("version0  = 0.00\n")
    f.write("upgrade   = migrations/*_up_*.sql\n")
    f.write("downgrade = migrations/*_down_*.sql\n")
  end unless File.exist?("config/#{@env}")
end

#version(path) ⇒ Object

Extract version number from the SQL script path name Version is text between last ‘/’ and ‘_’ File path must end with ‘.sql’



41
42
43
# File 'lib/config.rb', line 41

def version(path)
  path.gsub(%r{.*/}, '').gsub(/_.*\.sql/, '')
end