Module: Adapter::Cassanity

Extended by:
Forwardable
Defined in:
lib/adapter/cassanity.rb,
lib/adapter/cassanity/version.rb

Constant Summary collapse

VERSION =
"0.5.0"

Instance Method Summary collapse

Instance Method Details

#clear(options = nil) ⇒ Object

Public



38
39
40
# File 'lib/adapter/cassanity.rb', line 38

def clear(options = nil)
  @client.truncate
end

#delete(key, options = nil) ⇒ Object

Public



29
30
31
32
33
34
35
# File 'lib/adapter/cassanity.rb', line 29

def delete(key, options = nil)
  operation_options = {where: where(key)}
  adapter_options = with_default_consistency(@options[:delete])
  arguments = update_arguments(operation_options, adapter_options, options)

  @client.delete(arguments)
end

#read(key, options = nil) ⇒ Object

Public



10
11
12
13
14
15
16
17
# File 'lib/adapter/cassanity.rb', line 10

def read(key, options = nil)
  operation_options = {where: where(key)}
  adapter_options = with_default_consistency(@options[:read])
  arguments = update_arguments(operation_options, adapter_options, options)

  rows = @client.select(arguments)
  rows.empty? ? nil : rows.first
end

#update_arguments(operation_options, adapter_options, method_options) ⇒ Object

Private



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/adapter/cassanity.rb', line 49

def update_arguments(operation_options, adapter_options, method_options)
  keys = operation_options.keys

  if !adapter_options.nil? && !adapter_options.empty?
    filtered_options = adapter_options.reject { |key| keys.include?(key) }
    operation_options.update(filtered_options)
  end

  if !method_options.nil? && !method_options.empty?
    filtered_options = method_options.reject { |key| keys.include?(key) }
    operation_options.update(filtered_options)
  end

  operation_options
end

#where(key) ⇒ Object

Private



43
44
45
46
# File 'lib/adapter/cassanity.rb', line 43

def where(key)
  primary_key = @options.fetch(:primary_key)
  {primary_key => key}
end

#with_default_consistency(options) ⇒ Object

Private



66
67
68
69
70
71
# File 'lib/adapter/cassanity.rb', line 66

def with_default_consistency(options)
  options ||= {}
  options[:using] ||= {}
  options[:using][:consistency] ||= :quorum
  options
end

#write(key, attributes, options = nil) ⇒ Object

Public



20
21
22
23
24
25
26
# File 'lib/adapter/cassanity.rb', line 20

def write(key, attributes, options = nil)
  operation_options = {set: attributes, where: where(key)}
  adapter_options = with_default_consistency(@options[:write])
  arguments = update_arguments(operation_options, adapter_options, options)

  @client.update(arguments)
end