class FuckBot::Store

Constants

STATS_RESPONSE

Attributes

data[RW]

Public Class Methods

new(path) click to toggle source
# File lib/fuck_bot/store.rb, line 9
def initialize(path)
  path ||= File.join(Dir.home, '.fuck_bot.pstore')
  @ref = PStore.new(path)

  load_data
end

Public Instance Methods

increment(user_id) click to toggle source
# File lib/fuck_bot/store.rb, line 25
def increment(user_id)
  @data[user_id] += 1

  self
end
save() click to toggle source
# File lib/fuck_bot/store.rb, line 16
def save
  @ref.transaction do
    @ref[:data] = @data
    @ref.commit
  end

  self
end
to_stats() click to toggle source
# File lib/fuck_bot/store.rb, line 31
def to_stats
  @data.sort_by { |_, v| -v }.map do |user, count|
    format(STATS_RESPONSE, user, count)
  end.join("\n")
end

Private Instance Methods

load_data() click to toggle source
# File lib/fuck_bot/store.rb, line 39
def load_data
  @data = @ref.transaction { @ref[:data] } || Hash.new(0)
end