Class: Spoonerize::Log

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

Overview

Class that handles reading/writing logs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Spoonerize::Log

Constructor for Log.

Parameters:

  • file (String)


26
27
28
29
30
31
# File 'lib/spoonerize/log.rb', line 26

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)


18
19
20
# File 'lib/spoonerize/log.rb', line 18

def directory
  @directory
end

#fileString (readonly)

The file name to use.

Returns:

  • (String)


12
13
14
# File 'lib/spoonerize/log.rb', line 12

def file
  @file
end

Instance Method Details

#contentsArray

The contents of the log file.

Returns:

  • (Array)


37
38
39
# File 'lib/spoonerize/log.rb', line 37

def contents
  ::CSV.read(file)
end

#eachArray

Iterate through each line of the file.

Returns:

  • (Array)


55
56
57
# File 'lib/spoonerize/log.rb', line 55

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

#sizeInteger

Number of entries in the file.

Returns:

  • (Integer)


63
64
65
# File 'lib/spoonerize/log.rb', line 63

def size
  contents.size
end

#write(row) ⇒ Array

Writes a line to the log.

Parameters:

  • row (Array)

Returns:

  • (Array)


47
48
49
# File 'lib/spoonerize/log.rb', line 47

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