Module: ActiveRecord::Configurations
- Defined in:
- lib/active_record/configurations.rb,
lib/active_record/configurations/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#configurations ⇒ Object
readonly
Returns the value of attribute configurations.
-
#environments ⇒ Object
readonly
Returns the value of attribute environments.
Class Method Summary collapse
Instance Method Summary collapse
- #configure(name, parent: :default, &block) ⇒ Object
- #lookup_environment(name) ⇒ Object
- #resolve_database_dsn(string, environment) ⇒ Object
Instance Attribute Details
#configurations ⇒ Object (readonly)
Returns the value of attribute configurations.
55 56 57 |
# File 'lib/active_record/configurations.rb', line 55 def configurations @configurations end |
#environments ⇒ Object (readonly)
Returns the value of attribute environments.
54 55 56 |
# File 'lib/active_record/configurations.rb', line 54 def environments @environments end |
Class Method Details
.extended(child) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_record/configurations.rb', line 27 def self.extended(child) child.instance_variable_set(:@environments, {}) child.instance_variable_set(:@configurations, {}) # This is needed to explicitly override the behaviour provided by ActiveRecord::Core. def child.configurations instance_variable_get(:@configurations) end def child.configurations= value instance_variable_set(:@configurations, value) end # Remove the shitty "default" child.configurations.delete("default_env") # Add one that makes sense: child.configure(:default) do # The provided uri overrides other options. dsn ->{ENV[prefix.upcase + "_DSN"]} database ->{prefix + "_#{name}"} host 'localhost' prefix 'database' end end |
Instance Method Details
#configure(name, parent: :default, &block) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_record/configurations.rb', line 101 def configure(name, parent: :default, &block) parent = self.lookup_environment(parent) environment = Build::Environment.new(parent, {name: name.to_s}, &block) self.environments[name] = environment configuration = environment.to_hash if dsn = configuration[:dsn] resolve_database_dsn(dsn, configuration) end self.configurations[name.to_s] = configuration.stringify_keys end |
#lookup_environment(name) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/active_record/configurations.rb', line 91 def lookup_environment(name) return nil unless name if environment = self.environments[name] environment # elsif self.superclass # self.superclass.lookup_environment(name) end end |
#resolve_database_dsn(string, environment) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/active_record/configurations.rb', line 57 def resolve_database_dsn(string, environment) uri = URI.parse(string) case uri.scheme when 'postgres' environment[:adapter] = 'postgresql' when 'mysql' environment[:adapter] = 'mysql2' else environment[:adapter] = uri.scheme end if username = uri.user environment[:username] = username end if password = uri.password environment[:password] = password end if host = uri.host environment[:host] = host end if port = uri.port environment[:port] = port end if path = uri.path # The path should always be relative: environment[:database] = path.sub(/^\//, '') end end |