Sha256: fc1e83c1bdd11c2f28617039fc7b4752b1a40daa3f91c8e17847095dee593f20

Contents?: true

Size: 1.62 KB

Versions: 140

Compression:

Stored size: 1.62 KB

Contents

module Sprout
  
  # Used by the GitTask to load, parse and persist Version information
  # related to a project.
  # Expects a file with a 3 digit number, separated by periods like:
  #
  #   3.4.2
  #
  # Create with a path to the file like:
  # 
  #   version = VersionFile.new('path/Version.txt')
  #
  class VersionFile
    
    def initialize(file_path)
      @file_path = file_path
      read_value
    end
    
    def value=(value)
      @value = value
      write_value
    end
    
    def value
      @value
    end
    
    def major_version
      @value.split('.').shift.to_i
    end
    
    def minor_version
      @value.split('.')[1].to_i
    end
    
    def revision
      @value.split('.').pop.to_i
    end
    
    def increment_revision
      self.revision = self.revision + 1
    end
    
    def to_s
      @value
    end
    
    def to_str
      @value
    end
    
    def to_tag
      parts = value.split('.')
      parts[0] = add_leading_zeros(parts[0], 2)
      parts[1] = add_leading_zeros(parts[1], 2)
      parts[2] = add_leading_zeros(parts[2], 3)
      return parts.join('.')
    end
    
    private
    
    def add_leading_zeros(str, digits)
      (digits - str.size).times do
        str = "0#{str}"
      end
      str
    end
    
    def read_value
      File.open(@file_path, 'r') do |file|
        @value = file.read.strip
      end
    end
    
    def write_value
      File.open(@file_path, 'r+') do |file|
        file.write @value
      end
    end
    
    def revision=(revision)
      parts = @value.split('.')
      parts[2] = revision
      @value = parts.join('.')
      write_value
    end
  end
end

Version data entries

140 entries across 140 versions & 1 rubygems

Version Path
sprout-0.7.228 lib/sprout/version_file.rb
sprout-0.7.228-x86-linux lib/sprout/version_file.rb
sprout-0.7.228-x86-darwin-10 lib/sprout/version_file.rb
sprout-0.7.228-mswin32 lib/sprout/version_file.rb
sprout-0.7.228-darwin lib/sprout/version_file.rb
sprout-0.7.227 lib/sprout/version_file.rb
sprout-0.7.227-x86-linux lib/sprout/version_file.rb
sprout-0.7.227-x86-darwin-10 lib/sprout/version_file.rb
sprout-0.7.227-mswin32 lib/sprout/version_file.rb
sprout-0.7.227-darwin lib/sprout/version_file.rb
sprout-0.7.226 lib/sprout/version_file.rb
sprout-0.7.226-x86-linux lib/sprout/version_file.rb
sprout-0.7.226-x86-darwin-10 lib/sprout/version_file.rb
sprout-0.7.226-mswin32 lib/sprout/version_file.rb
sprout-0.7.226-darwin lib/sprout/version_file.rb
sprout-0.7.224 lib/sprout/version_file.rb
sprout-0.7.224-x86-linux lib/sprout/version_file.rb
sprout-0.7.224-x86-darwin-10 lib/sprout/version_file.rb
sprout-0.7.224-mswin32 lib/sprout/version_file.rb
sprout-0.7.224-darwin lib/sprout/version_file.rb