Module: Sequelizer
- Defined in:
- lib/sequelizer.rb,
lib/sequelizer/cli.rb,
lib/sequelizer/options.rb,
lib/sequelizer/version.rb,
lib/sequelizer/env_config.rb,
lib/sequelizer/yaml_config.rb,
lib/sequelizer/options_hash.rb,
lib/sequelizer/connection_maker.rb,
lib/sequelizer/gemfile_modifier.rb
Overview
Sequelizer
Sequelizer is a Ruby gem that simplifies database connections using Sequel. It allows users to configure database connections via config/database.yml or .env files, providing an easy-to-use interface for establishing database connections without hardcoding sensitive information.
Usage
Include this module in any class where you’d like to quickly establish a Sequel connection to a database:
class MyClass
include Sequelizer
def some_method
db[:users].all # Uses cached connection
end
end
Configuration Sources
Configuration is loaded from multiple sources in order of precedence:
-
Passed options
-
.env file
-
Environment variables
-
config/database.yml
-
~/.config/sequelizer/database.yml
Examples
# Use cached connection
db[:users].all
# Create new connection with custom options
new_db(adapter: 'postgres', host: 'localhost')
Defined Under Namespace
Classes: CLI, ConnectionMaker, EnvConfig, GemfileModifier, Options, OptionsHash, YamlConfig
Constant Summary collapse
- VERSION =
Version for the gem
'0.2.0'.freeze
Class Method Summary collapse
-
.options ⇒ Hash
Returns the default options hash for database connections.
Instance Method Summary collapse
-
#db(options = {}) ⇒ Sequel::Database
Instantiates and memoizes a database connection.
-
#find_cached(options) ⇒ Sequel::Database?
Finds a cached connection for the given options.
-
#new_db(options = {}) ⇒ Sequel::Database
Instantiates and returns a new database connection on each call.
Class Method Details
Instance Method Details
#db(options = {}) ⇒ Sequel::Database
Instantiates and memoizes a database connection. The db method instantiates the connection on the first call and then memoizes itself so only a single connection is used on repeated calls.
65 66 67 |
# File 'lib/sequelizer.rb', line 65 def db( = {}) @_sequelizer_db ||= new_db() end |
#find_cached(options) ⇒ Sequel::Database?
Finds a cached connection for the given options.
91 92 93 94 |
# File 'lib/sequelizer.rb', line 91 def find_cached() @_sequelizer_cache ||= {} @_sequelizer_cache[] end |
#new_db(options = {}) ⇒ Sequel::Database
Instantiates and returns a new database connection on each call.
80 81 82 83 84 85 |
# File 'lib/sequelizer.rb', line 80 def new_db( = {}) cached = find_cached() return cached if cached && ![:force_new] @_sequelizer_cache[] = ConnectionMaker.new().connection end |