class Believer::Environment::BaseEnv

Constants

DEFAULT_CONFIG

Public Class Methods

new(config = nil) click to toggle source

Creates a new environment using the provided configuration @param config [Object] the configuration. If it’s a hash use it as the configuration. If it’s a String, assume it’s a YAML file and load it as a Hash

# File lib/believer/environment/base_env.rb, line 25
def initialize(config = nil)
  unless config.nil?
    if config.is_a?(Hash)
      @configuration = config.dup
    elsif config.is_a?(String)
      @configuration = load_config_from_file(config)
    end
  end
end

Public Instance Methods

believer_configuration() click to toggle source

The connection_pool configuration, which should be a :pool node in the configuration.

# File lib/believer/environment/base_env.rb, line 76
def believer_configuration
  configuration[:believer]
end
configuration() click to toggle source

Returns the configuration. This configuration hash should contain the cql-rb client connection parameters. Optionally the connection_pool configuraton can be included in a :pool node.

# File lib/believer/environment/base_env.rb, line 50
def configuration
  unless @configuration
    loaded = load_configuration
    config = HashWithIndifferentAccess.new(DEFAULT_CONFIG.merge(loaded))
    @configuration = config
  end
  @configuration
end
configuration=(config) click to toggle source

Sets the configuration

# File lib/believer/environment/base_env.rb, line 60
def configuration=(config)
  @configuration = config
end
connection_configuration() click to toggle source
# File lib/believer/environment/base_env.rb, line 64
def connection_configuration
  configuration.reject { |k, v| k == :pool || k == :believer }
end
connection_pool_configuration() click to toggle source

The connection_pool configuration, which should be a :pool node in the configuration.

# File lib/believer/environment/base_env.rb, line 69
def connection_pool_configuration
  pc = configuration[:pool]
  return DEFAULT_CONFIG[:pool] unless pc
  pc
end
create_connection(options = {}) click to toggle source

Creates a new connection

# File lib/believer/environment/base_env.rb, line 81
def create_connection(options = {})
  cc = connection_configuration
  if options[:connect_to_keyspace] && cc[:keyspace]
    connection = Cql::Client.connect(cc)
    connection.use(cc[:keyspace])
  else
    cc_no_keyspace = cc.delete_if { |k, v| k.to_s == 'keyspace' }
    connection = Cql::Client.connect(cc_no_keyspace)
  end
  connection
end
logger() click to toggle source
# File lib/believer/environment/base_env.rb, line 35
def logger
  return nil unless believer_configuration[:logger]
  return environment_logger if believer_configuration[:logger][:use_environment] && respond_to?(:environment_logger)
  unless @std_logger
    @std_logger = ::Logger.new(STDOUT)
    if believer_configuration[:logger][:level] && believer_configuration[:logger][:level].is_a?(Numeric)
      @std_logger.level = believer_configuration[:logger][:level].to_i
    end

  end
  @std_logger
end

Protected Instance Methods

load_config_from_file(config_file) click to toggle source
# File lib/believer/environment/base_env.rb, line 94
def load_config_from_file(config_file)
  return nil if config_file.nil?
  cfg = HashWithIndifferentAccess.new(YAML::load(File.open(config_file.to_s)))
  #puts "Loaded config from file #{config_file.to_s}: #{cfg}"
  cfg
end