Sha256: 4fcce2a048e1b6e260808007110dd697bdee788af3ab7ee2aa15a99aef15dea4

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module GemRelease
  class VersionFile
    include GemRelease::Helpers
    
    VERSION_PATTERN = /(VERSION\s*=\s*(?:"|'))(\d+\.\d+\.\d+)("|')/
    NUMBER_PATTERN  = /(\d+)\.(\d+)\.(\d+)/
    
    attr_reader :target
    
    def initialize(options = {})
      @target = options[:target] || :patch
    end
    
    def bump!
      File.open(filename, 'w+') { |f| f.write(bumped_content) }
    end
    
    def new_number
      @new_number ||= old_number.sub(NUMBER_PATTERN) do
        respond_to?(target) ? send(target, $1, $2, $3) : target
      end
    end
    
    def old_number
      @old_number ||= content =~ VERSION_PATTERN && $2
    end
    
    def filename
      File.expand_path("lib/#{gem_module_path}/version.rb")
    end
    
    protected
    
      def major(major, minor, patch)
        "#{major.to_i + 1}.0.0"
      end
    
      def minor(major, minor, patch)
        "#{major}.#{minor.to_i + 1}.0"
      end
    
      def patch(major, minor, patch)
        "#{major}.#{minor}.#{patch.to_i + 1}"
      end
    
      def content
        @content ||= File.read(filename)
      end
      
      def bumped_content
        content.sub(VERSION_PATTERN) { "#{$1}#{new_number}#{$3}"}
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gem-release-0.0.13 lib/gem_release/version_file.rb