Class: Spoonerize::Log
- Inherits:
-
Object
- Object
- Spoonerize::Log
- Defined in:
- lib/spoonerize/log.rb
Overview
Class that handles reading/writing logs.
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 ⇒ Array
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.
26 27 28 29 30 31 |
# File 'lib/spoonerize/log.rb', line 26 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.
18 19 20 |
# File 'lib/spoonerize/log.rb', line 18 def directory @directory end |
#file ⇒ String (readonly)
The file name to use.
12 13 14 |
# File 'lib/spoonerize/log.rb', line 12 def file @file end |
Instance Method Details
#contents ⇒ Array
The contents of the log file.
37 38 39 |
# File 'lib/spoonerize/log.rb', line 37 def contents ::CSV.read(file) end |
#each ⇒ Array
Iterate through each line of the file.
55 56 57 |
# File 'lib/spoonerize/log.rb', line 55 def each contents.each { |row| yield row } end |
#size ⇒ Integer
Number of entries in the file.
63 64 65 |
# File 'lib/spoonerize/log.rb', line 63 def size contents.size end |
#write(row) ⇒ Array
Writes a line to the log.
47 48 49 |
# File 'lib/spoonerize/log.rb', line 47 def write(row) ::CSV.open(file, "a") { |csv| csv << row } end |