Class: Ore::Versions::VersionConstant

Inherits:
Version
  • Object
show all
Includes:
Naming
Defined in:
lib/ore/versions/version_constant.rb

Overview

Represents a version loaded from a Ruby VERSION constant or Version module.

Constant Summary collapse

@@file_name =

Common file-name that the VERSION constant or Version module is defined within.

'version.rb'

Instance Attribute Summary

Attributes inherited from Version

#build, #major, #minor, #patch

Class Method Summary collapse

Methods included from Naming

#module_of, #modules_of, #names_in, #namespace_dirs_of, #namespace_of, #namespace_path_of, #underscore

Methods inherited from Version

#initialize, parse

Constructor Details

This class inherits a constructor from Ore::Versions::Version

Class Method Details

.extract_number(line) ⇒ Integer? (protected)

Extracts a version number from a MAJOR, MINOR, PATCH or BUILD constant declaration.

Parameters:

  • line (String)

    The line of Ruby code where the constant was defined.

Returns:

  • (Integer, nil)

    The extracted version number.


118
119
120
121
122
# File 'lib/ore/versions/version_constant.rb', line 118

def self.extract_number(line)
  if (match = line.match(/=\s*['"]?(\d+)['"]?/))
    match[1].to_i
  end
end

.extract_string(line) ⇒ String? (protected)

Extracts a number from a BUILD constant declaration.

Parameters:

  • line (String)

    The line of Ruby code where the constant was defined.

Returns:

  • (String, nil)

    The extracted string.


102
103
104
105
106
# File 'lib/ore/versions/version_constant.rb', line 102

def self.extract_string(line)
  if (match = line.match(/=\s*['"]?(\w+)['"]?/))
    match[1]
  end
end

.extract_version(line) ⇒ String? (protected)

Extracts the version string from a VERSION constant declaration.

Parameters:

  • line (String)

    The line of Ruby code where the VERSION constant was defined.

Returns:

  • (String, nil)

    The extracted version string.


87
88
89
90
91
# File 'lib/ore/versions/version_constant.rb', line 87

def self.extract_version(line)
  if (match = line.match(/=\s*['"](\d+\.\d+\.\d+(\.\w+)?)['"]/))
    match[1]
  end
end

.find(project) ⇒ VersionConstant?

Finds the version.rb file.

Parameters:

  • project (Project)

    The project.

Returns:


27
28
29
30
31
32
33
# File 'lib/ore/versions/version_constant.rb', line 27

def self.find(project)
  if project.namespace_dir
    path = File.join(project.namespace_dir,@@file_name)

    self.load(project.lib_path(path)) if project.lib_file?(path)
  end
end

.load(path) ⇒ VersionConstant?

Extracts the VERSION constant or the major / minor / patch / build version numbers from the Version module.

Parameters:

  • path (String)

    The path to the version.rb file.

Returns:


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ore/versions/version_constant.rb', line 45

def self.load(path)
  major = nil
  minor = nil
  patch = nil
  build = nil

  File.open(path) do |file|
    file.each_line do |line|
      unless line =~ /^\s*#/ # skip commented lines
        if line =~ /(VERSION|Version)\s*=\s*/
          version = extract_version(line)

          return self.parse(version) if version
        elsif line =~ /(MAJOR|Major)\s*=\s*/
          major ||= extract_number(line)
        elsif line =~ /(MINOR|Minor)\s*=\s*/
          minor ||= extract_number(line)
        elsif line =~ /(PATCH|Patch)\s*=\s*/
          patch ||= extract_number(line)
        elsif line =~ /(BUILD|Build)\s*=\s*/
          build ||= extract_string(line)
        end

        break if (major && minor && patch && build)
      end
    end
  end

  return self.new(major,minor,patch,build)
end