class Spinna::History

Takes care of tracking the picks that have been recently made so that they are not picked again too soon. The log is persisted in ~/.spinna/history.log by default.

Attributes

config[R]

The configuration of the application.

log[R]

An array that represents the log entries. The oldest entries are first.

Public Class Methods

new(config) click to toggle source
# File lib/spinna/history.rb, line 15
def initialize(config)
  @config = config
  read_history_log
end

Public Instance Methods

append(album) click to toggle source

Add album at the end of the history log unless it’s already in there.

# File lib/spinna/history.rb, line 22
def append(album)
  @log << album unless @log.include?(album)
  trim if full?
end
history_path() click to toggle source

Returns the path to the history log file on the filesystem.

# File lib/spinna/history.rb, line 39
def history_path
  File.join(config.data_dir, 'history.log')
end
include?(album) click to toggle source

Is the given album in the history already?

# File lib/spinna/history.rb, line 28
def include?(album)
  @log.include?(album)
end
save() click to toggle source

Saves the history to file after removing old picks from the log if needed.

# File lib/spinna/history.rb, line 34
def save
  write_history_to_disk
end

Private Instance Methods

full?() click to toggle source

Are there more albums in the log than there should be?

# File lib/spinna/history.rb, line 46
def full?
  @log.size > config.history_size
end
history_file_exists?() click to toggle source
# File lib/spinna/history.rb, line 72
def history_file_exists?
  File.exists?(history_path)
end
read_history_log() click to toggle source

Reads the history.log file and stores all the lines in an array.

# File lib/spinna/history.rb, line 64
def read_history_log
  if !history_file_exists?
    @log = [] and return
  end

  @log = File.read(history_path).split("\n")
end
trim() click to toggle source

Deletes the old albums that are now outdated depending on the history_size.

# File lib/spinna/history.rb, line 52
def trim
  @log = @log.last(config.history_size)
end
write_history_to_disk() click to toggle source
# File lib/spinna/history.rb, line 56
def write_history_to_disk
  File.open(history_path, 'w+') do |f|
    @log.each { |album| f.puts album }
  end
end