Class: Semverve::Formats::SimpleString

Inherits:
Object
  • Object
show all
Defined in:
lib/semverve/formats/simple_string.rb

Overview

Handles version files that define a single top-level VERSION string.

Constant Summary collapse

PATTERN =

Pattern for locating a simple VERSION assignment.

Returns:

  • (Regexp)
/^(\s*VERSION\s*=\s*)(["'])(\d+\.\d+\.\d+)(\2)/

Instance Method Summary collapse

Instance Method Details

#generate(version, module_name:) ⇒ String

Generates a simple version file.

Parameters:

Returns:

  • (String)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/semverve/formats/simple_string.rb', line 55

def generate(version, module_name:)
  <<~RUBY
    # frozen_string_literal: true

    ##
    # Namespace for #{module_name}.
    module #{module_name}
      ##
      # Full gem version string.
      #
      # @return [String]
      VERSION = "#{version}"
    end
  RUBY
end

#parse(content, path:) ⇒ Semverve::SemanticVersion

Parses a semantic version from simple string content.

Parameters:

  • content (String)
  • path (String)

Returns:

Raises:



24
25
26
27
28
29
30
# File 'lib/semverve/formats/simple_string.rb', line 24

def parse(content, path:)
  match = content.match(PATTERN)

  return SemanticVersion.parse(match[3]) if match

  raise Error, "Could not parse #{path} as simple format. Expected VERSION = \"MAJOR.MINOR.PATCH\"."
end

#replace(content, version, path:) ⇒ String

Replaces the semantic version in simple string content.

Parameters:

Returns:

  • (String)


40
41
42
43
44
45
46
# File 'lib/semverve/formats/simple_string.rb', line 40

def replace(content, version, path:)
  unless content.match?(PATTERN)
    raise Error, "Could not parse #{path} as simple format. Expected VERSION = \"MAJOR.MINOR.PATCH\"."
  end

  content.sub(PATTERN) { "#{$1}#{$2}#{version}#{$4}" }
end