lib/tobox/configuration.rb in tobox-0.3.2 vs lib/tobox/configuration.rb in tobox-0.4.0
- old
+ new
@@ -5,11 +5,11 @@
module Tobox
class Configuration
extend Forwardable
- attr_reader :handlers, :lifecycle_events, :arguments_handler, :default_logger
+ attr_reader :handlers, :lifecycle_events, :arguments_handler, :default_logger, :database
def_delegator :@config, :[]
DEFAULT_CONFIGURATION = {
environment: ENV.fetch("APP_ENV", "development"),
@@ -58,20 +58,46 @@
env = @config[:environment]
@default_logger = @config[:logger] || Logger.new(STDERR, formatter: DEFAULT_LOG_FORMATTER) # rubocop:disable Style/GlobalStdStream
@default_logger.level = @config[:log_level] || (env == "production" ? Logger::INFO : Logger::DEBUG)
+ @database = if @config[:database_uri]
+ database_opts = {}
+ database_opts[:max_connections] = @config[:concurrency] if @config[:worker] == :thread
+ Sequel.connect(@config[:database_uri].to_s, database_opts)
+ else
+ Sequel::DATABASES.first
+ end
+ raise Error, "no database found" unless @database
+
+ if @database.frozen?
+ raise "#{@database} must have the :date_arithmetic extension loaded" unless Sequel.respond_to?(:date_add)
+ else
+ @database.extension :date_arithmetic
+ @database.loggers << @default_logger unless @config[:environment] == "production"
+ end
+
freeze
end
def on(*events, &callback)
events.each do |event|
(@handlers[event.to_sym] ||= []) << callback
end
self
end
+ def on_start(&callback)
+ (@lifecycle_events[:on_start] ||= []) << callback
+ self
+ end
+
+ def on_stop(&callback)
+ (@lifecycle_events[:on_stop] ||= []) << callback
+ self
+ end
+
def on_before_event(&callback)
(@lifecycle_events[:before_event] ||= []) << callback
self
end
@@ -114,9 +140,10 @@
@name.freeze
@config.each_value(&:freeze).freeze
@handlers.each_value(&:freeze).freeze
@lifecycle_events.each_value(&:freeze).freeze
@plugins.freeze
+ @database.freeze
super
end
private