Class: Todo::Src::File
- Inherits:
-
Object
- Object
- Todo::Src::File
- Defined in:
- lib/todo/src/file.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #exists? ⇒ Boolean
-
#initialize(path, opts = {}) ⇒ File
constructor
A new instance of File.
- #read ⇒ Object
- #write(lines) ⇒ Object
Constructor Details
#initialize(path, opts = {}) ⇒ File
Returns a new instance of File.
6 7 8 9 |
# File 'lib/todo/src/file.rb', line 6 def initialize(path, opts = {}) @path = path @mode = opts[:mode] || 'w+' end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
4 5 6 |
# File 'lib/todo/src/file.rb', line 4 def mode @mode end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/todo/src/file.rb', line 4 def path @path end |
Instance Method Details
#exists? ⇒ Boolean
11 12 13 |
# File 'lib/todo/src/file.rb', line 11 def exists? ::File.exists?(path) end |
#read ⇒ Object
15 16 17 |
# File 'lib/todo/src/file.rb', line 15 def read exists? ? ::File.readlines(path).map(&:rstrip) : [] end |
#write(lines) ⇒ Object
19 20 21 |
# File 'lib/todo/src/file.rb', line 19 def write(lines) ::File.open(path, mode) { |f| f.puts(lines.join("\n")) } end |