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
96
# File 'lib/ardb.rb', line 89

def initialize
  @default_timezone = :utc
  @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

#default_timezoneObject

Returns the value of attribute default_timezone.



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

def default_timezone
  @default_timezone
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#migrations_pathObject



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

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



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

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

Instance Method Details

#==(other) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ardb.rb', line 134

def ==(other)
  if other.kind_of?(self.class)
    self.activerecord_connect_hash == other.activerecord_connect_hash &&
    self.default_timezone          == other.default_timezone          &&
    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



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

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



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

def validate!
  if self.adapter.to_s.empty? || self.database.to_s.empty?
    raise ConfigurationError, "an adapter and database must be provided"
  end

  if !VALID_SCHEMA_FORMATS.include?(self.schema_format)
    raise ConfigurationError, "schema format must be one of: " \
                              "#{VALID_SCHEMA_FORMATS.join(", ")}"
  end

  true
end