class RocketJob::Batch::Statistics::Stats

Attributes

in_memory[R]
stats[R]

Public Class Methods

new(hash = nil) click to toggle source

hash [Hash]

Update an `in-memory` copy of the stats instead of gathering them inside `stats`.
# File lib/rocket_job/batch/statistics.rb, line 18
def initialize(hash = nil)
  @in_memory = hash
  @stats     = Hash.new(0) unless hash
end

Public Instance Methods

empty?() click to toggle source
# File lib/rocket_job/batch/statistics.rb, line 40
def empty?
  stats.nil? || stats.empty?
end
inc(hash) click to toggle source
# File lib/rocket_job/batch/statistics.rb, line 23
def inc(hash)
  hash.each_pair { |key, increment| inc_key(key, increment) }
  self
end
inc_key(key, increment = 1) click to toggle source
# File lib/rocket_job/batch/statistics.rb, line 28
def inc_key(key, increment = 1)
  return if increment.zero?

  if in_memory
    # For tests and in-process execution
    inc_in_memory(key, increment)
  elsif key && key != ""
    stats["statistics.#{key}"] += increment
  end
  self
end

Private Instance Methods

inc_in_memory(key, increment) click to toggle source

Navigates path and creates child hashes as needed at the end is reached

# File lib/rocket_job/batch/statistics.rb, line 47
def inc_in_memory(key, increment)
  paths = key.to_s.split(".")
  last  = paths.pop
  return unless last

  last_target = paths.inject(in_memory) do |target, sub_key|
    target.key?(sub_key) ? target[sub_key] : target[sub_key] = Hash.new(0)
  end
  last_target[last] += increment
end