Class: Perpetuity::Configuration

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



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

def initialize
  @logger = Logger.new(STDOUT)
  @logger.progname = 'Perpetuity'
end

Class Method Details

.adaptersObject



61
62
63
# File 'lib/perpetuity/config.rb', line 61

def self.adapters
  @adapters ||= {}
end

Instance Method Details

#adapter(name) ⇒ Object



65
66
67
# File 'lib/perpetuity/config.rb', line 65

def adapter name
  self.class.adapters[name.to_sym]
end

#data_source(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/perpetuity/config.rb', line 11

def data_source *args
  if args.any?
    db = args.first

    case db
    when String
      args[0] = URI(args[0])
      @db = data_source_from_url(*args)
    when Symbol
      adapter = args.shift
      db_name = args.shift
      options = args.shift || {}
      adapter_class = adapter(adapter)

      @db = adapter_class.new(options.merge(db: db_name))
    end
  end

  @db
end

#data_source_from_url(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/perpetuity/config.rb', line 41

def data_source_from_url *args
  uri = args.shift
  options = args.shift || {}

  protocol = uri.scheme
  klass = adapter(protocol)
  db_options = {
    db: uri.path[1..-1],
    username: uri.user,
    password: uri.password,
    host: uri.host,
    port: uri.port,
  }
  if options.key? :pool_size
    db_options[:pool_size] = options[:pool_size]
  end

  klass.new(db_options)
end

#logger(*args) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/perpetuity/config.rb', line 32

def logger *args
  if args.any?
    raise ArgumentError, 'Perpetuity::Configuration#logger takes 0..1 arguments'
    @logger = args.first
  end

  @logger
end