Class: Communard::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn_string = ENV["DATABASE_URL"], opts = Sequel::OPTS) {|_self| ... } ⇒ Configuration

Returns a new instance of Configuration.

Yields:

  • (_self)

Yield Parameters:



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.dump_after_migrating = false
  self.same_db = true

  yield self if block_given?
end

Instance Attribute Details

#db_pathObject

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_migratingObject

Returns the value of attribute dump_after_migrating.



43
44
45
# File 'lib/communard/configuration.rb', line 43

def dump_after_migrating
  @dump_after_migrating
end

#loggerObject

Returns the value of attribute logger.



39
40
41
# File 'lib/communard/configuration.rb', line 39

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/communard/configuration.rb', line 8

def options
  @options
end

#same_dbObject

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

#adapterObject



88
89
90
# File 'lib/communard/configuration.rb', line 88

def adapter
  options.fetch("adapter")
end

#connectionObject



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_nameObject



92
93
94
# File 'lib/communard/configuration.rb', line 92

def database_name
  options.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_connectionObject



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