Class: Bcdatabase::DatabaseConfigurations

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/bcdatabase.rb', line 157

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.

Returns:

  • (String)

    a YAMLized view of a configuration entry.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/bcdatabase.rb', line 187

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.message })
    end
  end
end

Class Method Details

.automatic_transformsObject



144
145
146
147
148
149
150
# File 'lib/bcdatabase.rb', line 144

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_’)‘.

Returns:

  • (#call)

    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_’)‘.



123
124
125
126
127
128
129
130
131
# File 'lib/bcdatabase.rb', line 123

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.

Returns:

  • (Hash)

    the entry for the given group and name after all transformation is complete.



178
179
180
# File 'lib/bcdatabase.rb', line 178

def [](groupname, dbname)
  create_entry(groupname.to_s, dbname.to_s)
end