Class: Communard::Configuration
- Inherits:
-
Object
- Object
- Communard::Configuration
- Defined in:
- lib/communard/configuration.rb
Instance Attribute Summary collapse
-
#db_path ⇒ Object
Returns the value of attribute db_path.
-
#dump_after_migrating ⇒ Object
Returns the value of attribute dump_after_migrating.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#same_db ⇒ Object
Returns the value of attribute same_db.
Instance Method Summary collapse
- #adapter ⇒ Object
- #connection ⇒ Object
- #database_name ⇒ Object
- #default_logger(out = $stdout) ⇒ Object
-
#initialize(conn_string = ENV["DATABASE_URL"], opts = Sequel::OPTS) {|_self| ... } ⇒ Configuration
constructor
A new instance of Configuration.
- #silent_connection ⇒ Object
Constructor Details
#initialize(conn_string = ENV["DATABASE_URL"], opts = Sequel::OPTS) {|_self| ... } ⇒ Configuration
Returns a new instance of Configuration.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/communard/configuration.rb', line 10 def initialize(conn_string = ENV["DATABASE_URL"], opts = Sequel::OPTS) @conn_string = conn_string @opts = opts case conn_string when String uri = URI.parse(conn_string) @options = { "adapter" => uri.scheme, "user" => uri.user, "password" => uri.password, "port" => uri.port, "host" => uri.hostname, "database" => (m = %r{/(.*)}.match(uri.path)) && (m[1]), } when Hash @options = conn_string.map { |k, v| [ k.to_s, v ] }.to_h else raise ArgumentError, "Sequel::Database.connect takes either a Hash or a String, given: #{conn_string.inspect}" end self.db_path = Pathname(Dir.pwd).join("db") self.logger = nil self. = false self.same_db = true yield self if block_given? end |
Instance Attribute Details
#db_path ⇒ Object
Returns the value of attribute db_path.
45 46 47 |
# File 'lib/communard/configuration.rb', line 45 def db_path @db_path end |
#dump_after_migrating ⇒ Object
Returns the value of attribute dump_after_migrating.
43 44 45 |
# File 'lib/communard/configuration.rb', line 43 def @dump_after_migrating end |
#logger ⇒ Object
Returns the value of attribute logger.
39 40 41 |
# File 'lib/communard/configuration.rb', line 39 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/communard/configuration.rb', line 8 def @options end |
#same_db ⇒ Object
Returns the value of attribute same_db.
41 42 43 |
# File 'lib/communard/configuration.rb', line 41 def same_db @same_db end |
Instance Method Details
#adapter ⇒ Object
88 89 90 |
# File 'lib/communard/configuration.rb', line 88 def adapter .fetch("adapter") end |
#connection ⇒ Object
51 52 53 54 55 56 |
# File 'lib/communard/configuration.rb', line 51 def connection Sequel.connect(@conn_string, @opts).tap { |c| c.loggers = [logger, default_logger].compact c.sql_log_level = :debug } end |
#database_name ⇒ Object
92 93 94 |
# File 'lib/communard/configuration.rb', line 92 def database_name .fetch("database") end |
#default_logger(out = $stdout) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/communard/configuration.rb', line 64 def default_logger(out = $stdout) ::Logger.new(out).tap { |l| alternate = 0 l.formatter = Proc.new { |sev, _, _, msg| alternate = ((alternate + 1) % 2) msg = if sev == "DEBUG" " #{msg}" else "[#{sev}] #{msg}" end if out.tty? color = case sev when "INFO" then 35 + alternate when "DEBUG" then 30 else 31 end "\e[#{color}m#{msg}\e[0m\n" else "#{msg}\n" end } } end |
#silent_connection ⇒ Object
58 59 60 61 62 |
# File 'lib/communard/configuration.rb', line 58 def silent_connection Sequel.connect(@conn_string, @opts).tap { |c| c.loggers = [logger].compact } end |