Class: Pluto::Connecter
- Inherits:
-
Object
- Object
- Pluto::Connecter
- Includes:
- LogUtils::Logging
- Defined in:
- lib/pluto/connecter.rb
Overview
DB Connecter / Connection Manager lets you establish connection
Instance Method Summary collapse
- #connect!(config = nil) ⇒ Object
- #debug=(value) ⇒ Object
- #debug? ⇒ Boolean
-
#initialize ⇒ Connecter
constructor
A new instance of Connecter.
Constructor Details
#initialize ⇒ Connecter
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pluto/connecter.rb', line 25 def connect!( config = nil ) if config.nil? # use/try DATABASE_URL from environment 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 # assume sqlite3 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 dbbrowser and other tools add to ActiveRecord if ActiveRecord::Base.configurations.nil? # todo/check: can this ever happen? remove? puts "ActiveRecord configurations nil - set to empty hash" ActiveRecord::Base.configurations = {} # make it an empty hash end if debug? puts 'ar configurations (before):' pp ActiveRecord::Base.configurations end # note: for now always use pluto key for config storage ActiveRecord::Base.configurations['pluto'] = config if debug? puts 'ar configurations (after):' pp ActiveRecord::Base.configurations end # 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 ActivityDb::Models::Activity.table_exists? ActivityDb::CreateDb.new.up end 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
20 21 22 |
# File 'lib/pluto/connecter.rb', line 20 def debug? @debug || false end |