Module: Cassandro

Defined in:
lib/cassandro/core.rb,
lib/cassandro/model.rb,
lib/cassandro/ext/migrator.rb,
lib/cassandro/ext/migration.rb,
lib/cassandro/ext/soft_delete.rb

Defined Under Namespace

Modules: SoftDelete Classes: Migration, Migrator, Model

Constant Summary collapse

@@cluster =
nil
@@session =
nil
@@tables =
[]

Class Method Summary collapse

Class Method Details

.clientObject



12
13
14
# File 'lib/cassandro/core.rb', line 12

def self.client
  @@session
end

.clusterObject



8
9
10
# File 'lib/cassandro/core.rb', line 8

def self.cluster
  @@cluster
end

.connect(options = {}) ⇒ Object



20
21
22
23
24
# File 'lib/cassandro/core.rb', line 20

def self.connect(options = {})
  keyspace = options.delete(:keyspace)
  @@cluster = Cassandra.cluster(options)
  @@session = @@cluster.connect(keyspace || nil)
end

.create_keyspace(name, options = { replication: { class: 'SimpleStrategy', replication_factor: 1}}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cassandro/core.rb', line 39

def self.create_keyspace(name, options = { replication: { class: 'SimpleStrategy', replication_factor: 1}} )
  with = "WITH " + options.map do |key, option|
    param_string = "#{key.to_s.upcase} = " +
    if option.is_a?(Hash)
      "{ #{option.map { |k,v| "'#{k}': #{v.is_a?(String) ? "'#{v}'": v.to_s}" }.join(", ")} }"
    else
      option.is_a?(String) ? "'#{option}'": option.to_s
    end
  end.join(" AND ")

  keyspace_definition = "    CREATE KEYSPACE IF NOT EXISTS \#{name}\n    \#{with}\n  KSDEF\n\n  @@session.execute(keyspace_definition)\nend\n"

.disconnectObject



30
31
32
33
# File 'lib/cassandro/core.rb', line 30

def self.disconnect
  @@cluster.close if @cluster
  @@session = nil
end

.execute(cql_command) ⇒ Object



35
36
37
# File 'lib/cassandro/core.rb', line 35

def self.execute(cql_command)
  @@session.execute(cql_command)
end

.load_tablesObject



65
66
67
68
69
70
71
72
# File 'lib/cassandro/core.rb', line 65

def self.load_tables
  @@tables.each do |table_definition|
    queries = table_definition.split(";").map(&:strip)
    queries.each do |query|
      @@session.execute(query) unless query.empty?
    end
  end
end

.register_table(table_def) ⇒ Object



61
62
63
# File 'lib/cassandro/core.rb', line 61

def self.register_table(table_def)
  @@tables << table_def
end

.tablesObject



16
17
18
# File 'lib/cassandro/core.rb', line 16

def self.tables
  @@tables
end

.truncate_table(table_name) ⇒ Object



57
58
59
# File 'lib/cassandro/core.rb', line 57

def self.truncate_table(table_name)
  @@session.execute("TRUNCATE #{table_name}")
end

.use(keyspace) ⇒ Object



26
27
28
# File 'lib/cassandro/core.rb', line 26

def self.use(keyspace)
  @@session.execute("USE #{keyspace}") if @@session
end