Class: Bcdatabase::DatabaseConfigurations
- Inherits:
-
Object
- Object
- Bcdatabase::DatabaseConfigurations
- Defined in:
- lib/bcdatabase.rb
Overview
The set of groups and entries returned by one call to load.
Constant Summary collapse
- BUILT_IN_TRANSFORMS =
{ :key_defaults => lambda { |entry, name, group| { 'username' => name, 'database' => name }.merge(entry) }, :decrypt => lambda { |entry, name, group| entry.merge({ 'password' => Bcdatabase.decrypt(entry['epassword']) }) if entry['epassword'] }, :datamapper => prefix_remove_copy_transform('datamapper_'), :jruby => prefix_remove_copy_transform('jruby_') }
Class Method Summary collapse
- .automatic_transforms ⇒ Object
-
.prefix_remove_copy_transform(prefix) ⇒ #call
A transform that copies a prefixed key’s value to the name without the prefix.
Instance Method Summary collapse
-
#[](groupname, dbname) ⇒ Hash
The entry for the given group and name after all transformation is complete.
-
#initialize(files, transforms = []) ⇒ DatabaseConfigurations
constructor
Creates a configuration from a set of YAML files.
-
#method_missing(name, *args) ⇒ String
This method implements the Rails database.yml integration described in full in the README.
Constructor Details
#initialize(files, transforms = []) ⇒ DatabaseConfigurations
Creates a configuration from a set of YAML files.
General use of the library should not use this method, but instead should use Bcdatabase.load.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/bcdatabase.rb', line 162 def initialize(files, transforms=[]) @transforms = (self.class.automatic_transforms + transforms).collect do |t| case t when Symbol BUILT_IN_TRANSFORMS[t] or fail "No built-in transform named #{t.inspect}" else fail 'Transforms must by callable' unless t.respond_to?(:call) t end end @files = files @map = { } files.each do |filename| name = File.basename(filename).gsub(/\.ya?ml/, '') @map[name] = YAML.load(File.open(filename)) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ String
This method implements the Rails database.yml integration described in full in the README.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/bcdatabase.rb', line 192 def method_missing(name, *args) groupname = (args[0] or raise "Database configuration group not specified for #{name}") dbname = (args[1] or raise "Database entry name not specified for #{name}") n = name.to_s begin unseparated_yaml(n => self[groupname, dbname]) rescue Bcdatabase::Error => e if ENV['RAILS_ENV'] == n raise e else # Not using that configuration right now, so return a dummy instead # of throwing an exception unseparated_yaml(n => { 'error' => e. }) end end end |
Class Method Details
.automatic_transforms ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/bcdatabase.rb', line 149 def self.automatic_transforms @automatic_transforms ||= [ :key_defaults, :decrypt, (:jruby if RUBY_PLATFORM =~ /java/) ].compact end |
.prefix_remove_copy_transform(prefix) ⇒ #call
Returns a transform that copies a prefixed key’s value to the name without the prefix. E.g., the built-in ‘:datamapper` transform is `prefix_remove_copy_transform(’datamapper_’)‘.
128 129 130 131 132 133 134 135 136 |
# File 'lib/bcdatabase.rb', line 128 def self.prefix_remove_copy_transform(prefix) lambda { |entry, name, group| entry.merge( entry.keys.select { |k| k =~ /^#{prefix}/ }.inject({}) { |additions, k| additions[k.sub(/^#{prefix}/, '')] = entry[k]; additions } ) } end |
Instance Method Details
#[](groupname, dbname) ⇒ Hash
Returns the entry for the given group and name after all transformation is complete.
183 184 185 |
# File 'lib/bcdatabase.rb', line 183 def [](groupname, dbname) create_entry(groupname.to_s, dbname.to_s) end |