Class: Believer::Test::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/believer/test/database.rb

Class Method Summary collapse

Class Method Details

.setup(options = {}) ⇒ Object

Drop, and create the keyspace. If options contains classes, creates all tables

Parameters:

  • options (Hash) (defaults to: {})

    the options

Options Hash (options):

  • :environment (Object)

    the environment to use. Must be a subclass of Believer::Environment::BaseEnv

  • :classes (Object)

    an array of classes to create tables for. Items can be strings or class constants



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/believer/test/database.rb', line 9

def self.setup(options = {})
  env = options[:environment] || Believer::Base.environment

  connection = env.create_connection(:connect_to_keyspace => false)

  keyspace = ::Believer::KeySpace.new(env)
  # First delete the existing keyspace
  begin
    #env.logger.debug "Dropping keyspace"
    keyspace.drop
  rescue Cql::QueryError
  end

  keyspace.create({})
  connection.use(env.connection_configuration[:keyspace])

  ::Believer::Base.environment = env

      classes = options[:classes]
  classes.each do |cl|
    if cl.is_a?(String)
      clazz = cl.split('::').inject(Kernel) { |scope, const_name| scope.const_get(const_name) }
    elsif cl.is_a?(Class)
      clazz = cl
    end
    if clazz.ancestors.include?(Believer::Base)
      #puts "Creating table #{clazz.table_name}"
      clazz.create_table()
    end

  end unless classes.nil? || classes.empty?

end