Class: Gemirro::MirrorFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gemirro/mirror_file.rb

Overview

Similar to MirrorDirectory the MirrorFile class is used to make it easier to read and write data in a directory that mirrors data from an external source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MirrorFile



17
18
19
# File 'lib/gemirro/mirror_file.rb', line 17

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gemirro/mirror_file.rb', line 11

class MirrorFile
  attr_reader :path

  ##
  # @param [String] path
  #
  def initialize(path)
    @path = path
  end

  ##
  # Writes the specified content to the current file. Existing files are
  # overwritten.
  #
  # @param [String] content
  #
  def write(content)
    handle = File.open(@path, 'w')

    handle.write(content)
    handle.close
  end

  ##
  # Reads the content of the current file.
  #
  # @return [String]
  #
  def read
    handle  = File.open(@path, 'r')
    content = handle.read

    handle.close

    content
  end
end

Instance Method Details

#readString

Reads the content of the current file.



39
40
41
42
43
44
45
46
# File 'lib/gemirro/mirror_file.rb', line 39

def read
  handle  = File.open(@path, 'r')
  content = handle.read

  handle.close

  content
end

#write(content) ⇒ Object

Writes the specified content to the current file. Existing files are overwritten.



27
28
29
30
31
32
# File 'lib/gemirro/mirror_file.rb', line 27

def write(content)
  handle = File.open(@path, 'w')

  handle.write(content)
  handle.close
end