Class: InMemoryBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/counter_server.rb

Overview

In-memory backend useful for testing.

Instance Method Summary collapse

Constructor Details

#initializeInMemoryBackend

Returns a new instance of InMemoryBackend.



122
123
124
# File 'lib/counter_server.rb', line 122

def initialize
  @in_memory_hash ||= Hash.new { |h, group_name| h[group_name] = Hash.new { |h, key| h[key] = 0 } }
end

Instance Method Details

#flush {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



136
137
138
# File 'lib/counter_server.rb', line 136

def flush
  yield self
end

#increment_by(group_name, key, increment_by) ⇒ Object



140
141
142
# File 'lib/counter_server.rb', line 140

def increment_by(group_name, key, increment_by)
  @in_memory_hash[group_name][key] += increment_by
end

#retrieve_counts(group_name) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/counter_server.rb', line 126

def retrieve_counts(group_name)
  ret = ActiveSupport::OrderedHash.new

  hash = @in_memory_hash[group_name]
  hash.keys.sort.each do |key|
    ret[key] = hash[key]
  end
  ret
end