class Semverve::Formats::ModuleConstants
Handles version files with MAJOR, MINOR, and PATCH constants.
Constants
- CONSTANTS
-
Mapping of semantic-version parts to Ruby constant names.
@return [Hash<Symbol, String>]
Public Instance Methods
# File lib/semverve/formats/module_constants.rb, line 64 def generate(version, module_name:) <<~RUBY # frozen_string_literal: true ## # Namespace for #{module_name}. module #{module_name} ## # Semantic version information for #{module_name}. module Version ## # Major version. # # @return [Integer] MAJOR = #{version.major} ## # Minor version. # # @return [Integer] MINOR = #{version.minor} ## # Patch version. # # @return [Integer] PATCH = #{version.patch} module_function ## # Version as +[MAJOR, MINOR, PATCH]+ # # @return [Array<Integer>] def to_a [MAJOR, MINOR, PATCH] end ## # Version as +MAJOR.MINOR.PATCH+ # # @return [String] def to_s to_a.join(".") end end ## # Full gem version string. # # @return [String] VERSION = #{module_name}::Version.to_s end RUBY end
Generates a module-constant version file.
@param [Semverve::SemanticVersion] version @param [String] module_name
@return [String]
Source
# File lib/semverve/formats/module_constants.rb, line 28 def parse(content, path:) SemanticVersion.new( major: constant_value(content, path, :major), minor: constant_value(content, path, :minor), patch: constant_value(content, path, :patch) ) end
Parses a semantic version from module-constant content.
@param [String] content @param [String] path
@return [Semverve::SemanticVersion]
# File lib/semverve/formats/module_constants.rb, line 44 def replace(content, version, path:) CONSTANTS.reduce(content) do |updated, (part, constant)| pattern = /^(\s*#{constant}\s*=\s*)\d+/ value = version.public_send(part) unless updated.match?(pattern) raise Error, "Could not find #{constant} in #{path}." end updated.sub(pattern) { "#{$1}#{value}" } end end
Replaces MAJOR, MINOR, and PATCH values in module-constant content.
@param [String] content @param [Semverve::SemanticVersion] version @param [String] path
@return [String]