Class: Pluto::Connecter

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/pluto/connecter.rb

Overview

DB Connecter / Connection Manager

lets you establish connection

Instance Method Summary collapse

Constructor Details

#initializeConnecter

Returns a new instance of Connecter.



11
12
13
# File 'lib/pluto/connecter.rb', line 11

def initialize
  # do nothing for now
end

Instance Method Details

#connect!(config = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pluto/connecter.rb', line 25

def connect!( config = nil )

  if config.nil?   # use DATABASE_URL

    logger.debug "ENV['DATBASE_URL'] - >#{ENV['DATABASE_URL']}<"

    db = URI.parse( ENV['DATABASE_URL'] || 'sqlite3:///pluto.db' )

    if db.scheme == 'postgres'
      config = {
        adapter:  'postgresql',
        host:     db.host,
        port:     db.port,
        username: db.user,
        password: db.password,
        database: db.path[1..-1],
        encoding: 'utf8'
      }
    else 
      config = {
        adapter:  db.scheme,       # sqlite3
        database: db.path[1..-1]   # pluto.db (NB: cut off leading /, thus 1..-1)
      }
    end
  end  # if config.nil?

  puts 'db settings:'
  pp config

  # for debugging - disable for production use
  if debug?
    ActiveRecord::Base.logger = Logger.new( STDOUT ) 
  end

  ActiveRecord::Base.establish_connection( config )
  
  # first time? - auto-run db migratation, that is, create db tables
  unless Models::Feed.table_exists?
     CreateDb.new.up  
  end
end

#debug=(value) ⇒ Object



16
17
18
# File 'lib/pluto/connecter.rb', line 16

def debug=(value)
  @debug = value
end

#debug?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/pluto/connecter.rb', line 20

def debug?
  @debug || false
end