Class: Believer::Environment::BaseEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/believer/environment/base_env.rb

Direct Known Subclasses

MerbEnv, RailsEnv

Constant Summary collapse

DEFAULT_CONFIG =
{
    :pool => {
        :size => 1,
        :timeout => 10
    },
    :believer => {
        :logger => {
            :use_environment => true,
            :level => ::Logger::DEBUG
        },
        :test => {
            :life_cycle => {
                :counters => :destroy
            }
        }
    }
}

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ BaseEnv

Creates a new environment using the provided configuration it’s a YAML file and load it as a Hash

Parameters:

  • config (Object) (defaults to: nil)

    the configuration. If it’s a hash use it as the configuration. If it’s a String, assume



25
26
27
28
29
30
31
32
33
# 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

Instance Method Details

#believer_configurationObject

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



76
77
78
# File 'lib/believer/environment/base_env.rb', line 76

def believer_configuration
  configuration[:believer]
end

#configurationObject

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.



50
51
52
53
54
55
56
57
# 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) ⇒ Object

Sets the configuration



60
61
62
# File 'lib/believer/environment/base_env.rb', line 60

def configuration=(config)
  @configuration = config
end

#connection_configurationObject



64
65
66
# File 'lib/believer/environment/base_env.rb', line 64

def connection_configuration
  configuration.reject { |k, v| k == :pool || k == :believer }
end

#connection_pool_configurationObject

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



69
70
71
72
73
# 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 = {}) ⇒ Object

Creates a new connection



81
82
83
84
85
86
87
88
89
90
91
# 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

#loggerObject



35
36
37
38
39
40
41
42
43
44
45
46
# 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