Class: Spoonerize::Log
- Inherits:
-
Object
- Object
- Spoonerize::Log
- 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
-
#directory ⇒ String
readonly
The directory the file is located.
-
#file ⇒ String
readonly
The file name to use.
Instance Method Summary collapse
-
#contents ⇒ Array
The contents of the log file.
-
#each ⇒ Enumerable
Iterate through each line of the file.
-
#initialize(file) ⇒ Spoonerize::Log
constructor
Constructor for Log.
-
#size ⇒ Integer
Number of entries in the file.
-
#write(row) ⇒ Array
Writes a line to the log.
Constructor Details
#initialize(file) ⇒ Spoonerize::Log
Constructor for Log.
28 29 30 31 32 33 |
# File 'lib/spoonerize/log.rb', line 28 def initialize(file) @file = File.(file) @directory = File.dirname(file) FileUtils.mkdir_p(directory) unless File.directory?(directory) FileUtils.touch(file) unless File.file?(file) end |
Instance Attribute Details
#directory ⇒ String (readonly)
The directory the file is located.
20 21 22 |
# File 'lib/spoonerize/log.rb', line 20 def directory @directory end |
#file ⇒ String (readonly)
The file name to use.
14 15 16 |
# File 'lib/spoonerize/log.rb', line 14 def file @file end |
Instance Method Details
#contents ⇒ Array
The contents of the log file.
39 40 41 |
# File 'lib/spoonerize/log.rb', line 39 def contents ::CSV.read(file) end |
#each ⇒ Enumerable
Iterate through each line of the file.
57 58 59 |
# File 'lib/spoonerize/log.rb', line 57 def each contents.each { |row| yield row } end |
#size ⇒ Integer
Number of entries in the file.
65 66 67 |
# File 'lib/spoonerize/log.rb', line 65 def size contents.size end |
#write(row) ⇒ Array
Writes a line to the log.
49 50 51 |
# File 'lib/spoonerize/log.rb', line 49 def write(row) ::CSV.open(file, "a") { |csv| csv << row } end |