class TickingAway::JSONFileStorage
Class to store !timeat stats as a JSON file in local storage and return !timepopularity stats for a provided !timeat call
Attributes
filename[R]
stats[RW]
Public Class Methods
new(filename = 'ticking_away_stats.json')
click to toggle source
# File lib/ticking_away/json_file_storage.rb, line 10 def initialize(filename = 'ticking_away_stats.json') @filename = filename @stats = read_from_file end
Public Instance Methods
full_match?(stat_name, key)
click to toggle source
# File lib/ticking_away/json_file_storage.rb, line 37 def full_match?(stat_name, key) return true if key.length == stat_name.length || key[stat_name.length] == '/' false end
get_stat(stat_name)
click to toggle source
Get the number of times !timeat was called for a tz_info or prefix. Partial prefix matches do not count.
# File lib/ticking_away/json_file_storage.rb, line 28 def get_stat(stat_name) call_count = 0 stats.each do |key, value| call_count += value if key.start_with?(stat_name) && full_match?(stat_name, key) end call_count end
increment_stat(stat_name)
click to toggle source
Add 1 to a !timeat <tz_info> stat and save the hash as JSON to a file
# File lib/ticking_away/json_file_storage.rb, line 17 def increment_stat(stat_name) if stats[stat_name] stats[stat_name] += 1 else stats.merge!({ stat_name => 1 }) end save_stats end
read_file()
click to toggle source
# File lib/ticking_away/json_file_storage.rb, line 47 def read_file File.read(filename) end
read_from_file()
click to toggle source
Get saved stats on instantiation or return an empty hash
# File lib/ticking_away/json_file_storage.rb, line 52 def read_from_file return JSON.parse(read_file) if File.file?(filename) {} end
save_stats()
click to toggle source
# File lib/ticking_away/json_file_storage.rb, line 43 def save_stats File.write(filename, JSON.dump(stats)) end