class Librarian::Config::Database

Attributes

adapter_name[RW]
assigned_specfile_name[RW]
root[RW]
underlying_env[RW]
underlying_home[RW]
underlying_pwd[RW]

Public Class Methods

library() click to toggle source
# File lib/librarian/config/database.rb, line 11
def library
  name.split("::").first.downcase
end
new(adapter_name, options = { }) click to toggle source
# File lib/librarian/config/database.rb, line 25
def initialize(adapter_name, options = { })
  self.adapter_name = adapter_name or raise ArgumentError, "must provide adapter_name"

  options[:project_path] || options[:pwd] or raise ArgumentError, "must provide project_path or pwd"

  self.root = options[:project_path] && Pathname(options[:project_path])
  self.assigned_specfile_name = options[:specfile_name]
  self.underlying_env = options[:env] or raise ArgumentError, "must provide env"
  self.underlying_pwd = options[:pwd] && Pathname(options[:pwd])
  self.underlying_home = options[:home] && Pathname(options[:home])
end

Public Instance Methods

[](key, scope = nil) click to toggle source
# File lib/librarian/config/database.rb, line 49
def [](key, scope = nil)
  case scope
  when "local", :local then local[key]
  when "env", :env then env[key]
  when "global", :global then global[key]
  when nil then local[key] || env[key] || global[key]
  else raise Error, "bad scope"
  end
end
[]=(key, scope, value) click to toggle source
# File lib/librarian/config/database.rb, line 59
def []=(key, scope, value)
  case scope
  when "local", :local then local[key] = value
  when "global", :global then global[key] = value
  else raise Error, "bad scope"
  end
end
env() click to toggle source
# File lib/librarian/config/database.rb, line 41
def env
  memo(__method__) { HashSource.new(adapter_name, :name => "environment", :raw => env_source_data) }
end
global() click to toggle source
# File lib/librarian/config/database.rb, line 37
def global
  memo(__method__) { new_file_source(global_config_path) }
end
keys() click to toggle source
# File lib/librarian/config/database.rb, line 67
def keys
  [local, env, global].inject([]){|a, e| a.concat(e.keys) ; a}.sort.uniq
end
local() click to toggle source
# File lib/librarian/config/database.rb, line 45
def local
  memo(__method__) { new_file_source(local_config_path) }
end
lockfile_name() click to toggle source
# File lib/librarian/config/database.rb, line 91
def lockfile_name
  "#{specfile_name}.lock"
end
lockfile_path() click to toggle source
# File lib/librarian/config/database.rb, line 87
def lockfile_path
  project_path + lockfile_name
end
project_path() click to toggle source
# File lib/librarian/config/database.rb, line 71
def project_path
  root || specfile_path.dirname
end
specfile_name() click to toggle source
# File lib/librarian/config/database.rb, line 83
def specfile_name
  specfile_path.basename.to_s
end
specfile_path() click to toggle source
# File lib/librarian/config/database.rb, line 75
def specfile_path
  if root
    root + (assigned_specfile_name || default_specfile_name)
  else
    env_specfile_path || default_specfile_path
  end
end

Private Instance Methods

config_key() click to toggle source
# File lib/librarian/config/database.rb, line 161
def config_key
  "config"
end
config_name() click to toggle source
# File lib/librarian/config/database.rb, line 181
def config_name
  File.join(*[config_name_prefix, adapter_name, "config"])
end
config_name_prefix() click to toggle source
# File lib/librarian/config/database.rb, line 177
def config_name_prefix
  ".#{library}"
end
default_global_config_path() click to toggle source
# File lib/librarian/config/database.rb, line 114
def default_global_config_path
  underlying_home && underlying_home + config_name
end
default_local_config_path() click to toggle source
# File lib/librarian/config/database.rb, line 143
def default_local_config_path
  default_project_root_path + config_name
end
default_project_root_path() click to toggle source
# File lib/librarian/config/database.rb, line 147
def default_project_root_path
  if root
    root
  else
    path = underlying_pwd
    path = path.dirname until project_root_path?(path) || path.dirname == path
    project_root_path?(path) ? path : underlying_pwd
  end
end
default_specfile_name() click to toggle source
# File lib/librarian/config/database.rb, line 169
def default_specfile_name
  "#{adapter_name.capitalize}file"
end
default_specfile_path() click to toggle source
# File lib/librarian/config/database.rb, line 133
def default_specfile_path
  default_project_root_path + (assigned_specfile_name || default_specfile_name)
end
env_global_config_path() click to toggle source
# File lib/librarian/config/database.rb, line 110
def env_global_config_path
  memo(__method__) { env[config_key] }
end
env_local_config_path() click to toggle source
# File lib/librarian/config/database.rb, line 137
def env_local_config_path
  return unless env_specfile_path

  env_specfile_path.dirname + config_name
end
env_source_data() click to toggle source
# File lib/librarian/config/database.rb, line 189
def env_source_data
  prefix = raw_key_prefix

  data = underlying_env.dup
  data.reject!{|k, _| !k.start_with?(prefix) || k.size <= prefix.size}
  data
end
env_specfile_path() click to toggle source
# File lib/librarian/config/database.rb, line 126
def env_specfile_path
  memo(__method__) do
    path = env[specfile_key]
    path && Pathname(path)
  end
end
global_config_path() click to toggle source
# File lib/librarian/config/database.rb, line 106
def global_config_path
  env_global_config_path || default_global_config_path
end
library() click to toggle source
# File lib/librarian/config/database.rb, line 173
def library
  self.class.library
end
local_config_path() click to toggle source
# File lib/librarian/config/database.rb, line 118
def local_config_path
  root_local_config_path || env_local_config_path || default_local_config_path
end
memo(key) { || ... } click to toggle source
# File lib/librarian/config/database.rb, line 197
def memo(key)
  key = "@#{key}"
  instance_variable_set(key, yield) unless instance_variable_defined?(key)
  instance_variable_get(key)
end
new_file_source(config_path) click to toggle source
# File lib/librarian/config/database.rb, line 97
def new_file_source(config_path)
  return unless config_path

  FileSource.new(adapter_name,
    :config_path => config_path,
    :forbidden_keys => [config_key, specfile_key]
  )
end
project_root_path?(path) click to toggle source
# File lib/librarian/config/database.rb, line 157
def project_root_path?(path)
  File.file?(path + default_specfile_name)
end
raw_key_prefix() click to toggle source
# File lib/librarian/config/database.rb, line 185
def raw_key_prefix
  "#{library.upcase}_#{adapter_name.upcase}_"
end
root_local_config_path() click to toggle source
# File lib/librarian/config/database.rb, line 122
def root_local_config_path
  root && root + config_name
end
specfile_key() click to toggle source
# File lib/librarian/config/database.rb, line 165
def specfile_key
  "#{adapter_name}file"
end