Module: ActiveRecord::Configurations

Defined in:
lib/active_record/configurations.rb,
lib/active_record/configurations/version.rb

Constant Summary collapse

VERSION =
"0.4.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configurationsObject (readonly)

Returns the value of attribute configurations.



73
74
75
# File 'lib/active_record/configurations.rb', line 73

def configurations
  @configurations
end

#environmentsObject (readonly)

Returns the value of attribute environments.



72
73
74
# File 'lib/active_record/configurations.rb', line 72

def environments
  @environments
end

Class Method Details

.extended(child) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_record/configurations.rb', line 45

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_record/configurations.rb', line 119

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



109
110
111
112
113
114
115
116
117
# File 'lib/active_record/configurations.rb', line 109

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_record/configurations.rb', line 75

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