Class: GemMirror::MirrorFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_mirror/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)


18
19
20
# File 'lib/gem_mirror/mirror_file.rb', line 18

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

Returns:

  • (String)


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gem_mirror/mirror_file.rb', line 12

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

  ##
  # Reads the contents of a Gzip encoded file.
  #
  # @return [String]
  #
  def read_gzip
    content = nil

    Zlib::GzipReader.open(path) do |gz|
      content = gz.read
      gz.close
    end

    content
  end
end

Instance Method Details

#readString

Reads the content of the current file.

Returns:

  • (String)


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

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

  handle.close

  content
end

#read_gzipString

Reads the contents of a Gzip encoded file.

Returns:

  • (String)


54
55
56
57
58
59
60
61
62
63
# File 'lib/gem_mirror/mirror_file.rb', line 54

def read_gzip
  content = nil

  Zlib::GzipReader.open(path) do |gz|
    content = gz.read
    gz.close
  end

  content
end

#write(content) ⇒ Object

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

Parameters:

  • content (String)


28
29
30
31
32
33
# File 'lib/gem_mirror/mirror_file.rb', line 28

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

  handle.write(content)
  handle.close
end