Class: GemMirror::MirrorFile
- Inherits:
-
Object
- Object
- GemMirror::MirrorFile
- 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
- #path ⇒ String readonly
Instance Method Summary collapse
-
#initialize(path) ⇒ MirrorFile
constructor
A new instance of MirrorFile.
-
#read ⇒ String
Reads the content of the current file.
-
#read_gzip ⇒ String
Reads the contents of a Gzip encoded file.
-
#write(content) ⇒ Object
Writes the specified content to the current file.
Constructor Details
#initialize(path) ⇒ MirrorFile
Returns a new instance of MirrorFile.
18 19 20 |
# File 'lib/gem_mirror/mirror_file.rb', line 18 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ String (readonly)
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
#read ⇒ String
Reads the content of the current file.
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_gzip ⇒ String
Reads the contents of a Gzip encoded file.
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.
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 |