Class: Ardb::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ardb.rb

Constant Summary collapse

ACTIVERECORD_ATTRS =
[
  :adapter,
  :database,
  :encoding,
  :host,
  :port,
  :username,
  :password,
  :pool,
  :checkout_timeout,
  :min_messages
].freeze
DEFAULT_MIGRATIONS_PATH =
'db/migrations'.freeze
DEFAULT_SCHEMA_PATH =
'db/schema'.freeze
RUBY_SCHEMA_FORMAT =
:ruby.freeze
SQL_SCHEMA_FORMAT =
:sql.freeze
VALID_SCHEMA_FORMATS =
[RUBY_SCHEMA_FORMAT, SQL_SCHEMA_FORMAT].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



81
82
83
84
85
86
87
# File 'lib/ardb.rb', line 81

def initialize
  @logger          = Logger.new(STDOUT)
  @root_path       = ENV['PWD']
  @migrations_path = DEFAULT_MIGRATIONS_PATH
  @schema_path     = DEFAULT_SCHEMA_PATH
  @schema_format   = RUBY_SCHEMA_FORMAT
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



77
78
79
# File 'lib/ardb.rb', line 77

def logger
  @logger
end

#migrations_pathObject



89
90
91
# File 'lib/ardb.rb', line 89

def migrations_path
  File.expand_path(@migrations_path.to_s, @root_path.to_s)
end

#root_pathObject

Returns the value of attribute root_path.



77
78
79
# File 'lib/ardb.rb', line 77

def root_path
  @root_path
end

#schema_formatObject

Returns the value of attribute schema_format.



78
79
80
# File 'lib/ardb.rb', line 78

def schema_format
  @schema_format
end

#schema_pathObject



93
94
95
# File 'lib/ardb.rb', line 93

def schema_path
  File.expand_path(@schema_path.to_s, @root_path.to_s)
end

Instance Method Details

#==(other) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ardb.rb', line 122

def ==(other)
  if other.kind_of?(self.class)
    self.activerecord_connect_hash == other.activerecord_connect_hash &&
    self.logger                    == other.logger           &&
    self.root_path                 == other.root_path        &&
    self.schema_format             == other.schema_format    &&
    self.migrations_path           == other.migrations_path  &&
    self.schema_path               == other.schema_path
  else
    super
  end
end

#activerecord_connect_hashObject



105
106
107
108
109
110
# File 'lib/ardb.rb', line 105

def activerecord_connect_hash
  ACTIVERECORD_ATTRS.inject({}) do |h, attr_name|
    value = self.send(attr_name)
    !value.nil? ? h.merge!(attr_name.to_s => value) : h
  end
end

#validate!Object



112
113
114
115
116
117
118
119
120
# File 'lib/ardb.rb', line 112

def validate!
  if self.adapter.to_s.empty? || self.database.to_s.empty?
    raise ConfigurationError, "an adapter and database must be provided"
  elsif !VALID_SCHEMA_FORMATS.include?(self.schema_format)
    raise ConfigurationError, "schema format must be one of: " \
                              "#{VALID_SCHEMA_FORMATS.join(', ')}"
  end
  true
end