Class: Cassandra::Mocks::Table

Inherits:
Table
  • Object
show all
Defined in:
lib/cassandra_mocks/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(keyspace, name, partition_key, clustering_key, fields) ⇒ Table



4
5
6
7
8
9
# File 'lib/cassandra_mocks/table.rb', line 4

def initialize(keyspace, name, partition_key, clustering_key, fields)
  compaction = Cassandra::Table::Compaction.new('mock', {})
  options = Cassandra::Table::Options.new({}, compaction, {}, false, 'mock')
  column_map = column_map(partition_key, clustering_key, fields)
  super(keyspace, name, partition_key, clustering_key, column_map, options, [])
end

Instance Method Details

#clustering_columnsObject

make #clustering_columns public



67
68
69
# File 'lib/cassandra_mocks/table.rb', line 67

def clustering_columns
  super
end

#delete(filter) ⇒ Object



52
53
54
55
# File 'lib/cassandra_mocks/table.rb', line 52

def delete(filter)
  rows_to_remove = select('*', restriction: filter)
  @rows.reject! { |row| rows_to_remove.include?(row) }
end

#insert(attributes, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cassandra_mocks/table.rb', line 11

def insert(attributes, options = {})
  validate_columns!(attributes)
  validate_primary_key_presence!(attributes)

  prev_row_index = rows.find_index do |row|
    row.slice(*primary_key_names) == attributes.slice(*primary_key_names)
  end

  if prev_row_index
    return false if options[:check_exists]
    rows[prev_row_index] = attributes
  else
    rows << attributes
  end
  true
end

#partition_keyObject

make #partition_key public



62
63
64
# File 'lib/cassandra_mocks/table.rb', line 62

def partition_key
  super
end

#rowsObject



57
58
59
# File 'lib/cassandra_mocks/table.rb', line 57

def rows
  @rows ||= []
end

#select(*columns) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cassandra_mocks/table.rb', line 28

def select(*columns)
  filter = select_filter(columns)
  limit = filter.delete(:limit)
  order = select_order(filter)
  filter = filter.fetch(:restriction) { {} }
  unless filter.empty?
    validate_partion_key_filter!(filter)
    validate_clustering_column_filter!(filter)
    raise_if_fields_restricted!(filter)
  end

  filtered_rows = filtered_rows(filter)
  sorted_rows = filtered_rows.sort do |lhs, rhs|
    compare_rows(0, lhs, rhs, order)
  end

  sorted_rows = sorted_rows[0...limit] if limit

  result_rows = sorted_rows.map do |row|
    (columns.first == '*') ? row : row.slice(*columns)
  end
  ResultPage.new(result_rows)
end