Class: Reissue::VersionUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/reissue/version_updater.rb

Constant Summary collapse

VERSION_MATCH =

Regular expression pattern for matching the version string.

Returns:

  • (Regexp)

    The version regex pattern.

/(?<major>\d+)\.(?<minor>[a-zA-Z\d]+)\.(?<patch>[a-zA-Z\d]+)(?<add>\.(?<pre>[a-zA-Z\d]+))?/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_file, version_redo_proc: nil) ⇒ VersionUpdater

Initializes a new instance of the VersionUpdater class.

Parameters:

  • version_file (String)

    The path to the version file.



59
60
61
62
63
64
65
# File 'lib/reissue/version_updater.rb', line 59

def initialize(version_file, version_redo_proc: nil)
  @version_file = version_file
  @original_version = nil
  @new_version = nil
  @updated_body = ""
  @version_redo_proc = version_redo_proc
end

Instance Attribute Details

#version_redo_procObject

A proc that can be used to redo the version string.



81
82
83
# File 'lib/reissue/version_updater.rb', line 81

def version_redo_proc
  @version_redo_proc
end

Instance Method Details

#call(segment, version_file: @version_file) ⇒ String

Updates the version segment and writes the updated version to the file.

This allows you to read from one file and write to another.

Parameters:

  • segment (Symbol)

    The segment to update (:major, :minor, or :patch).

  • version_file (String) (defaults to: @version_file)

    The version_file to the version file (optional, defaults to @version_file).

Returns:

  • (String)

    The updated version string.



74
75
76
77
78
# File 'lib/reissue/version_updater.rb', line 74

def call(segment, version_file: @version_file)
  update(segment)
  write(version_file)
  @new_version
end

#redo(version, segment) ⇒ Object

Creates a new version string based on the original version and the specified segment.



84
85
86
87
88
89
90
# File 'lib/reissue/version_updater.rb', line 84

def redo(version, segment)
  if version_redo_proc
    version_redo_proc.call(version, segment)
  else
    version.redo(segment)
  end
end

#update(segment) ⇒ String

Updates the specified segment of the version string.

Parameters:

  • segment (Symbol)

    The segment to update (:major, :minor, or :patch).

Returns:

  • (String)

    The updated version string.



96
97
98
99
100
101
102
103
# File 'lib/reissue/version_updater.rb', line 96

def update(segment)
  version_file = File.read(@version_file)
  @updated_body = version_file.gsub(version_regex) do |string|
    @original_version = ::Gem::Version.new(string)
    @new_version = self.redo(::Gem::Version.new(string), segment).to_s
  end
  @new_version
end

#version_regexObject



109
# File 'lib/reissue/version_updater.rb', line 109

def version_regex = VERSION_MATCH

#write(version_file = @version_file) ⇒ Object

Writes the updated version to the specified file.

Parameters:

  • version_file (String) (defaults to: @version_file)

    The version_file to the version file (optional, defaults to @version_file).



114
115
116
# File 'lib/reissue/version_updater.rb', line 114

def write(version_file = @version_file)
  File.write(version_file, @updated_body)
end