Class: Cequel::Metal::Incrementer

Inherits:
Writer
  • Object
show all
Defined in:
lib/cequel/metal/incrementer.rb

Overview

Encapsulates a counter ‘UPDATE` operation comprising multiple increment or decrement operations

See Also:

Since:

  • 1.0.0

Instance Attribute Summary

Attributes inherited from Writer

#type_hints

Instance Method Summary collapse

Methods inherited from Writer

#execute, #initialize

Methods included from Util::Forwardable

#delegate

Constructor Details

This class inherits a constructor from Cequel::Metal::Writer

Instance Method Details

#decrement(data) ⇒ void

This method returns an undefined value.

Decrement one or more columns by given deltas

Parameters:

  • data (Hash<Symbol,Integer>)

    map of column names to deltas

Since:

  • 1.0.0



32
33
34
# File 'lib/cequel/metal/incrementer.rb', line 32

def decrement(data)
  increment(Hash[data.map { |column, count| [column, -count] }])
end

#increment(data) ⇒ void

This method returns an undefined value.

Increment one or more columns by given deltas

Parameters:

  • data (Hash<Symbol,Integer>)

    map of column names to deltas

Since:

  • 1.0.0



18
19
20
21
22
23
24
# File 'lib/cequel/metal/incrementer.rb', line 18

def increment(data)
  data.each_pair do |column_name, delta|
    operator = delta < 0 ? '-' : '+'
    statements << "#{column_name} = #{column_name} #{operator} ?"
    bind_vars << delta.abs
  end
end