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



16
17
18
# File 'lib/gem-mirror/mirror_file.rb', line 16

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gem-mirror/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

    return 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

    return content
  end
end

Instance Method Details

#readString

Reads the content of the current file.



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

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

  handle.close

  return content
end

#read_gzipString

Reads the contents of a Gzip encoded file.



52
53
54
55
56
57
58
59
60
61
# File 'lib/gem-mirror/mirror_file.rb', line 52

def read_gzip
  content = nil

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

  return content
end

#write(content) ⇒ Object

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



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

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

  handle.write(content)
  handle.close
end