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"
DEFAULT_SCHEMA_PATH =
"db/schema"
RUBY_SCHEMA_FORMAT =
:ruby
SQL_SCHEMA_FORMAT =
:sql
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.



100
101
102
103
104
105
106
107
# File 'lib/ardb.rb', line 100

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.



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

def default_timezone
  @default_timezone
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#migrations_pathObject



109
110
111
# File 'lib/ardb.rb', line 109

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

#root_pathObject

Returns the value of attribute root_path.



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

def root_path
  @root_path
end

#schema_formatObject

Returns the value of attribute schema_format.



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

def schema_format
  @schema_format
end

#schema_pathObject



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

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

Instance Method Details

#==(other) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ardb.rb', line 146

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

#activerecord_connect_hashObject



126
127
128
129
130
131
# File 'lib/ardb.rb', line 126

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

#validate!Object



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

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

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

  true
end