Class: Version::File
- Inherits:
-
Object
- Object
- Version::File
- Defined in:
- lib/versus/file.rb,
lib/versus/file/plain_format.rb,
lib/versus/file/jeweler_format.rb
Overview
Version::File class
Defined Under Namespace
Modules: JewelerFormat, PlainFormat
Constant Summary collapse
- NAMES =
Possible names for a version file to look for in automatic look-up.
%w{ VERSION VERSION.yml VERSION.yaml var/version }
Class Method Summary collapse
-
.current(path = nil) ⇒ Version::Number
Get current version by look-up of version file.
-
.lookup(path = nil) ⇒ Version::File
Look-up and return version file.
-
.supported_formats ⇒ Object
Supported version file parse formats.
-
.version_file(path) ⇒ Object
Attempts to detect the version file for the passed
filename
.
Instance Method Summary collapse
-
#change(number, file = nil) ⇒ Object
Change the number in the the file.
-
#format ⇒ Object
Get the verison file format.
-
#initialize(path) ⇒ File
constructor
New Version::File instance.
-
#number ⇒ Object
(also: #version)
Get a Version::Number instance from parsed file.
-
#parse(read) ⇒ Version::Number
Parse file constents.
-
#read ⇒ Object
Read the version file.
-
#save(file = nil) ⇒ Object
Save the version file.
Constructor Details
#initialize(path) ⇒ File
New Version::File instance.
96 97 98 |
# File 'lib/versus/file.rb', line 96 def initialize(path) @path = path end |
Class Method Details
.current(path = nil) ⇒ Version::Number
39 40 41 42 |
# File 'lib/versus/file.rb', line 39 def self.current(path=nil) vfile = lookup(path || File.dirname(caller.first)) vfile.version if vfile end |
.lookup(path = nil) ⇒ Version::File
Look-up and return version file.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/versus/file.rb', line 49 def self.lookup(path=nil) # if path is nil, detect automatically; if path is a directory, detect # automatically in the directory; if path is a filename, use it directly file = if path if ::File.file?(path) ::File.(path) else version_file(path) end else version_file(Dir.pwd) end return nil unless file && ::File.file?(file) File.new(file) end |
.supported_formats ⇒ Object
Supported version file parse formats.
22 23 24 |
# File 'lib/versus/file.rb', line 22 def self.supported_formats [JewelerFormat, PlainFormat] end |
.version_file(path) ⇒ Object
Attempts to detect the version file for the passed filename
. Looks up the directory hierarchy for a file named VERSION or VERSION.yml. Returns a Pathname for the file if found, otherwise nil.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/versus/file.rb', line 72 def self.version_file(path) path = File.(path) path = File.dirname(path) unless File.directory?(path) return nil unless File.directory?(path) home = File.('~') done = nil until path == '/' or path == home NAMES.each do |name| full = File.join(dir, name) break(done = full) if File.file?(full) end break done if done path = File.dirname(path) end done end |
Instance Method Details
#change(number, file = nil) ⇒ Object
Change the number in the the file.
125 126 127 128 |
# File 'lib/versus/file.rb', line 125 def change(number, file=nil) @number = Number.parse(number) save end |
#format ⇒ Object
Get the verison file format.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/versus/file.rb', line 103 def format @format ||= ( if read fmt = self.class.supported_formats.find{ |fm| fm.match?(path, read) } raise IOError, "Version file matches no known format." else PlainFormat end ) end |
#number ⇒ Object Also known as: version
Get a Version::Number instance from parsed file.
117 118 119 |
# File 'lib/versus/file.rb', line 117 def number @number ||= parse(read) end |
#parse(read) ⇒ Version::Number
Parse file constents.
151 152 153 |
# File 'lib/versus/file.rb', line 151 def parse(read) format.parse(read) end |
#read ⇒ Object
Read the version file.
133 134 135 |
# File 'lib/versus/file.rb', line 133 def read @read ||= File.read(path) end |
#save(file = nil) ⇒ Object
Save the version file.
140 141 142 143 144 |
# File 'lib/versus/file.rb', line 140 def save(file=nil) file = file || path text = format.render(number) ::File.open(file, 'w'){ |f| f << text } end |