Class: Makara::ConfigParser

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

Defined Under Namespace

Classes: ConnectionUrlResolver

Constant Summary collapse

DEFAULTS =
{
  master_ttl: 5,
  blacklist_duration: 30,
  sticky: true
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ConfigParser

Returns a new instance of ConfigParser.



144
145
146
147
148
149
# File 'lib/makara/config_parser.rb', line 144

def initialize(config)
  @config = config.symbolize_keys
  @makara_config = DEFAULTS.merge(@config[:makara] || {})
  @makara_config = @makara_config.symbolize_keys
  @id = sanitize_id(@makara_config[:id])
end

Instance Attribute Details

#makara_configObject (readonly)

Returns the value of attribute makara_config.



142
143
144
# File 'lib/makara/config_parser.rb', line 142

def makara_config
  @makara_config
end

Class Method Details

.merge_and_resolve_default_url_config(config) ⇒ Object

NOTE: url format must be, e.g. url: mysql2://… NOT url: mysql2_makara://… since the ‘_’ in the protocol (mysql2_makara) makes the URI invalid NOTE: Does not use ENV



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/makara/config_parser.rb', line 128

def self.merge_and_resolve_default_url_config(config)
  if ENV['DATABASE_URL']
    Makara::Logging::Logger.log "Please rename DATABASE_URL to use in the database.yml", :warn
  end
  return config unless config.key?(:url)

  url = config[:url]
  url_config = ConnectionUrlResolver.new(url).to_hash
  url_config = url_config.symbolize_keys
  url_config.delete(:adapter)
  config.delete(:url)
  config.update(url_config)
end

Instance Method Details

#idObject



151
152
153
154
155
156
# File 'lib/makara/config_parser.rb', line 151

def id
  @id ||= begin
    sorted = recursive_sort(@config)
    Digest::MD5.hexdigest(sorted.to_s)
  end
end

#master_configsObject



158
159
160
161
162
# File 'lib/makara/config_parser.rb', line 158

def master_configs
  all_configs
    .select { |config| config[:role] == 'master' }
    .map { |config| config.except(:role) }
end

#slave_configsObject



164
165
166
167
168
# File 'lib/makara/config_parser.rb', line 164

def slave_configs
  all_configs
    .reject { |config| config[:role] == 'master' }
    .map { |config| config.except(:role) }
end