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.



89
90
91
92
93
94
95
# File 'lib/ardb.rb', line 89

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.



85
86
87
# File 'lib/ardb.rb', line 85

def logger
  @logger
end

#migrations_pathObject



97
98
99
# File 'lib/ardb.rb', line 97

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

#root_pathObject

Returns the value of attribute root_path.



85
86
87
# File 'lib/ardb.rb', line 85

def root_path
  @root_path
end

#schema_formatObject

Returns the value of attribute schema_format.



86
87
88
# File 'lib/ardb.rb', line 86

def schema_format
  @schema_format
end

#schema_pathObject



101
102
103
# File 'lib/ardb.rb', line 101

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

Instance Method Details

#==(other) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ardb.rb', line 130

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



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

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



120
121
122
123
124
125
126
127
128
# File 'lib/ardb.rb', line 120

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