Class: Spoonerize::Log

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

Overview

Class that handles reading/writing logs. Log file is stored as a simple CSV.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Spoonerize::Log

Constructor for Log.

Parameters:

  • file (String)


28
29
30
31
32
33
# File 'lib/spoonerize/log.rb', line 28

def initialize(file)
  @file = File.expand_path(file)
  @directory = File.dirname(file)
  FileUtils.mkdir_p(directory) unless File.directory?(directory)
  FileUtils.touch(file) unless File.file?(file)
end

Instance Attribute Details

#directoryString (readonly)

The directory the file is located.

Returns:

  • (String)


20
21
22
# File 'lib/spoonerize/log.rb', line 20

def directory
  @directory
end

#fileString (readonly)

The file name to use.

Returns:

  • (String)


14
15
16
# File 'lib/spoonerize/log.rb', line 14

def file
  @file
end

Instance Method Details

#contentsArray

The contents of the log file.

Returns:

  • (Array)


39
40
41
# File 'lib/spoonerize/log.rb', line 39

def contents
  ::CSV.read(file)
end

#eachEnumerable

Iterate through each line of the file.

Returns:

  • (Enumerable)


57
58
59
# File 'lib/spoonerize/log.rb', line 57

def each
  contents.each { |row| yield row }
end

#sizeInteger

Number of entries in the file.

Returns:

  • (Integer)


65
66
67
# File 'lib/spoonerize/log.rb', line 65

def size
  contents.size
end

#write(row) ⇒ Array

Writes a line to the log.

Parameters:

  • row (Array)

Returns:

  • (Array)


49
50
51
# File 'lib/spoonerize/log.rb', line 49

def write(row)
  ::CSV.open(file, "a") { |csv| csv << row }
end