Class: Todo::Src::File

Inherits:
Object
  • Object
show all
Defined in:
lib/todo/src/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#modeObject (readonly)

Returns the value of attribute mode.



4
5
6
# File 'lib/todo/src/file.rb', line 4

def mode
  @mode
end

#pathObject (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

Returns:

  • (Boolean)


11
12
13
# File 'lib/todo/src/file.rb', line 11

def exists?
  ::File.exists?(path)
end

#readObject



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