Class: FuckBot::Store

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

Constant Summary collapse

STATS_RESPONSE =
'<@%s>: %s'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Store

Returns a new instance of Store.



9
10
11
12
13
14
# 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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/fuck_bot/store.rb', line 7

def data
  @data
end

Instance Method Details

#increment(user_id) ⇒ Object



25
26
27
28
29
# File 'lib/fuck_bot/store.rb', line 25

def increment(user_id)
  @data[user_id] += 1

  self
end

#saveObject



16
17
18
19
20
21
22
23
# File 'lib/fuck_bot/store.rb', line 16

def save
  @ref.transaction do
    @ref[:data] = @data
    @ref.commit
  end

  self
end

#to_statsObject



31
32
33
34
35
# 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