Class: VMLib::File
- Inherits:
-
Object
- Object
- VMLib::File
- Defined in:
- lib/vmlib/file.rb
Overview
This is the file class for handling VMLib version files
Constant Summary collapse
- FILE_NAME =
Default file name of ‘Version’
'Version'
Instance Method Summary collapse
-
#create(name = '', dir = nil) ⇒ Object
Create a new version file for the current (or specified) folder.
-
#find_file(dir = nil) ⇒ Object
Search the current and all parent folders for a version file Raises an error if it cannot find any up to the root.
-
#initialize(filename = nil) ⇒ File
constructor
Create a new file object with the specified filename.
-
#read(dir = nil) ⇒ Object
Read the version file and return the parsed version data.
-
#write(version, dir = nil) ⇒ Object
Write the specfied version into the version file.
Constructor Details
Instance Method Details
#create(name = '', dir = nil) ⇒ Object
Create a new version file for the current (or specified) folder. If it already exists, then throw an error
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vmlib/file.rb', line 81 def create(name = '', dir = nil) # Make sure that the directory isn't versioned already begin path = find_file(dir) rescue Errors::NoVersionError # We are good to go else # Raise error that it's already versioned raise Errors::VersionFileError, "version already exists at #{path}" end dir ||= ::Dir.pwd raise Errors::PathError unless ::File.directory?(dir) path = ::File.join(dir, @filename) ::File.open(path, "w") do |file| version = Version.new(name) file.write version.to_s + "\n" end end |
#find_file(dir = nil) ⇒ Object
Search the current and all parent folders for a version file Raises an error if it cannot find any up to the root.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vmlib/file.rb', line 27 def find_file(dir = nil) dir ||= ::Dir.pwd raise Errors::PathError unless ::File.directory?(dir) path = ::File.join(dir, @filename) ::Dir.chdir(dir) do while !::File.exists?(path) do if (::File.dirname(path).match(/^(\w:\/|\/)$/i)) raise Errors::NoVersionError, "#{dir} is not versioned" end path = ::File.join(::File.dirname(path), '..') path = ::File.(path) #puts "vmlib: looking at path #{path}" path = ::File.join(path, @filename) end @dir = path return path end end |
#read(dir = nil) ⇒ Object
Read the version file and return the parsed version data
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/vmlib/file.rb', line 49 def read(dir = nil) path = find_file(dir) if ::File.readable?(path) # Read verdata = Version.new verdata.parse ::File.read(path) else raise Errors::VersionFileError, "unable to read #{path}" end return verdata end |
#write(version, dir = nil) ⇒ Object
Write the specfied version into the version file
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/vmlib/file.rb', line 64 def write(version, dir = nil) path = find_file(dir) unless version.is_a? Version raise Errors::ParameterError, "invalid version #{version}" end if ::File.writable?(path) # Write ::File.write(path, version.to_s + "\n") else raise Errors::VersionFileError, "unable to write #{path}" end end |