Skip to content

Instantly share code, notes, and snippets.

@xdougx
Last active October 21, 2015 13:31
Show Gist options
  • Save xdougx/379c8092981b066bb950 to your computer and use it in GitHub Desktop.
Save xdougx/379c8092981b066bb950 to your computer and use it in GitHub Desktop.
build an database connection with PG
development:
encoding: utf8
port: 5432
database: development
username: username
password:
host: localhost
---
test:
encoding: utf8
port: 5432
database: test
username: username
password:
host: localhost
---
require "yaml"
module Importation
class DB
include Importation
property(db, env)
def initialize(env)
@env = env
end
def connection
@db ||= PG.connect(conection_string)
end
def conection_string
config = configuration
base_string = "postgresql://#{config["host"]}:#{config["port"]}/#{config["database"]}?"
# user=#{config["username"]}&password=#{config["password"]}&client_encoding=#{config["encoding"]}"
config_array = config.map do |key, value|
next if value.nil?
"#{key}=#{value}"
end.compact!
base_string + config_array.join("&")
end
def configuration
base = base_configuration
config = configuration_by_env
base.each { |key, value| config[key] = value unless config.keys.includes?(key) }
config
end
def base_configuration
{
"encoding" => "utf8",
"port" => "5432",
"database" => "default",
"username" => "root",
"password" => "",
"host" => "localhost"
}
end
def configuration_by_env
data[@env] as Hash
end
def data
YAML.load(File.read("#{root}/config/database.yml")) as Hash
end
end
end
Error in macro 'pp' /usr/local/Cellar/crystal-lang/0.9.0/src/macros.cr:52, line 2:
1.
2. ::puts "#{ "database.conection_string" } = #{ (database.conection_string).inspect }"
3.
::puts "#{ "database.conection_string" } = #{ (database.conection_string).inspect }"
^~~~~~~~~~~~~~~~
instantiating 'Importation::DB#conection_string()'
in ./app/importation/db.cr:24: instantiating 'Hash(YAML::Type, YAML::Type)#map()'
config_array = config.map do |key, value|
^~~
in /usr/local/Cellar/crystal-lang/0.9.0/src/hash.cr:444: instantiating 'each()'
each do |k, v|
^~~~
in /usr/local/Cellar/crystal-lang/0.9.0/src/hash.cr:444: instantiating 'each()'
each do |k, v|
^~~~
in /usr/local/Cellar/crystal-lang/0.9.0/src/hash.cr:445: instantiating 'Array(String)#push(String?)'
array.push yield k, v
^~~~
in /usr/local/Cellar/crystal-lang/0.9.0/src/array.cr:1251: instantiating '{String?}#each()'
values.each do |value|
^~~~
in /usr/local/Cellar/crystal-lang/0.9.0/src/array.cr:1251: instantiating '{String?}#each()'
values.each do |value|
^~~~
in /usr/local/Cellar/crystal-lang/0.9.0/src/array.cr:1252: no overload matches 'Array(String)#<<' with types String?
Overloads are:
- Array(String)#<<(value : String)
Couldn't find overloads for these types:
- Array(String)#<<(value : Nil)
self << value
^~
================================================================================
Nil trace:
/usr/local/Cellar/crystal-lang/0.9.0/src/array.cr:1251
values.each do |value|
^~~~~
macro macro_4427918288 (in /usr/local/Cellar/crystal-lang/0.9.0/src/tuple.cr:163):2
yield self[0]
require "./importation/*"
module Importation
def root
ROOT
end
def files_path
"#{root}/files"
end
end
h = {
"a" => "a",
"b" => "",
"c" => "a",
"d" => "",
"e" => "a",
}
e = h.map do |k, v|
next if v.nil? || v.empty?
"#{k}=#{v}"
end
pp e
ROOT = __DIR__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment