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

Returns a new instance of MirrorFile.

Parameters:

  • path (String)


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

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

Returns:

  • (String)


10
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
# File 'lib/gemirro/mirror_file.rb', line 10

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.

Returns:

  • (String)


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

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.

Parameters:

  • content (String)


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

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

  handle.write(content)
  handle.close
end