Class: GtfsReader::Version::Bumper

Inherits:
Object
  • Object
show all
Defined in:
lib/gtfs_reader/version.rb

Overview

A helper class which bumps the version number stored in this file

Constant Summary collapse

PARTS =
%i[major minor patch].freeze
PATTERN =
/(\s+)MAJOR = \d+\s+MINOR = \d+\s+PATCH = \d+\s+BUILD = .+/

Instance Method Summary collapse

Constructor Details

#initialize(part, filename = __FILE__) ⇒ Bumper

Returns a new instance of Bumper.

Parameters:

  • part (String)

    the part of the version to bump. one of major, minor, or patch

  • filename (String) (defaults to: __FILE__)

    the file to edit



24
25
26
27
28
# File 'lib/gtfs_reader/version.rb', line 24

def initialize(part, filename = __FILE__)
  raise "#{part} not one of #{PARTS}" unless PARTS.include? part
  @filename = filename
  @part = part
end

Instance Method Details

#bumpObject

Increase the version number and write it to this file



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gtfs_reader/version.rb', line 31

def bump
  parts = new_version
  # \1 holds a newline and the indentation from the source
  text = '\1' + ["MAJOR = #{parts[:major]}",
                 "MINOR = #{parts[:minor]}",
                 "PATCH = #{parts[:patch]}",
                 "BUILD = #{parts[:build] || 'nil'}"].join('\1')

  out_data = File.read(@filename).gsub(PATTERN, text)
  # puts out_data
  File.open(@filename, 'w') { |out| out << out_data }
  puts "Bumped version to #{self}"
end

#to_sString

Returns What the new version string will be.

Returns:

  • (String)

    What the new version string will be.



46
47
48
49
# File 'lib/gtfs_reader/version.rb', line 46

def to_s
  p = new_version
  [p[:major], p[:minor], p[:patch], p[:build]].compact.join('.')
end