Class: App::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/app_version/app_version.rb

Overview

Application Version main class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Version

Creates a new instance of the Version class using information in the passed Hash to construct the version number.

Version.new(:major => 1, :minor => 0) #=> "1.0"


40
41
42
43
44
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
75
76
77
78
79
80
81
# File 'lib/app_version/app_version.rb', line 40

def initialize(args = nil)
  if args && args.is_a?(Hash)
    args.keys.reject { |key| key.is_a?(Symbol) }.each { |key| args[key.to_sym] = args.delete(key) }

    [:major, :minor].each do |param|
      fail ArgumentError.new("The #{param} parameter is required") if args[param].blank?
    end

    @major      = args[:major].to_s
    @minor      = args[:minor].to_s
    @patch      = args[:patch].to_s     unless args[:patch].blank?
    @meta       = args[:meta].to_s      unless args[:meta].blank?
    @milestone  = args[:milestone].to_s unless args[:milestone].blank?
    @build      = args[:build].to_s     unless args[:build].blank?
    @branch     = args[:branch].to_s    unless args[:branch].blank?
    @committer  = args[:committer].to_s unless args[:committer].blank?
    @format     = args[:format].to_s    unless args[:format].blank?

    unless args[:build_date].blank?
      b_date = case args[:build_date]
               when 'git-revdate'
                 get_revdate_from_git
               else
                 args[:build_date].to_s
               end
      @build_date = Date.parse(b_date)
    end

    @build = case args[:build]
             when 'svn'
               get_build_from_subversion
             when 'git-revcount'
               get_revcount_from_git
             when 'git-hash'
               get_hash_from_git
             when nil, ''
               nil
             else
               args[:build].to_s
             end
  end
end

Instance Attribute Details

#branchString

Git Branch information

Returns:

  • (String)

    the current value of branch



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def branch
  @branch
end

#buildString

Build number

Returns:

  • (String)

    the current value of build



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def build
  @build
end

#build_dateString

Build Date

Returns:

  • (String)

    the current value of build_date



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def build_date
  @build_date
end

#commiterString

Git Commiter information

Returns:

  • (String)

    the current value of commiter



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def commiter
  @commiter
end

#committerObject

Returns the value of attribute committer.



27
28
29
# File 'lib/app_version/app_version.rb', line 27

def committer
  @committer
end

#formatString

Format information

Returns:

  • (String)

    the current value of format



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def format
  @format
end

#majorString

Major Version number

Returns:

  • (String)

    the current value of major



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def major
  @major
end

#metaString

Semantic version meta info

Returns:

  • (String)

    the current value of meta



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def meta
  @meta
end

#milestoneString

Milestone information

Returns:

  • (String)

    the current value of milestone



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def milestone
  @milestone
end

#minorString

Minor Version number

Returns:

  • (String)

    the current value of minor



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def minor
  @minor
end

#patchString

Patch number

Returns:

  • (String)

    the current value of patch



23
24
25
# File 'lib/app_version/app_version.rb', line 23

def patch
  @patch
end

Class Method Details

.load(path) ⇒ Object

Loads the version information from a YAML file.

Parameters:

  • path (String)

    Yaml file name to load



113
114
115
# File 'lib/app_version/app_version.rb', line 113

def self.load(path)
  App::Version.new YAML.load(File.open(path))
end

.parse(version) ⇒ App:Version

Parses a version string to create an instance of the Version class.

Parameters:

  • version (String)

    Version Number String to parse and initialize object with

Returns:

  • (App:Version)

    Returns populated Version object

Raises:

  • (ArguementError)

    In the event string not parsable



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/app_version/app_version.rb', line 88

def self.parse(version)
  m = version.match(/(\d+)\.(\d+)(?:\.(\d+))?(?:-([\w.\d]+))?(?:\sM(\d+))?(?:\s\((\d+)\))?(?:\sof\s(\w+))?(?:\sby\s(\w+))?(?:\son\s(\S+))?/)

  fail ArgumentError.new("The version '#{version}' is unparsable") if m.nil?

  version = App::Version.new major: m[1],
                             minor: m[2],
                             patch: m[3],
                             meta: m[4],
                             milestone: m[5],
                             build: m[6],
                             branch: m[7],
                             committer: m[8]

  if m[9] && m[9] != ''
    date = Date.parse(m[9])
    version.build_date = date
  end

  version
end

Instance Method Details

#<=>(other) ⇒ Integer

Combined Compare operator for version

Parameters:

  • other (App::Version)

    Version object to compare against

Returns:

  • (Integer)

    returns -1 if (a <=> b)



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/app_version/app_version.rb', line 123

def <=>(other)
  # if !self.build.nil? && !other.build.nil?
  #   return self.build <=> other.build
  # end

  %w(build major minor patch milestone branch meta committer build_date).each do |meth|
    rhs = send(meth) || -1
    lhs = other.send(meth) || -1

    ret = lhs <=> rhs
    return ret unless ret == 0
  end

  0
end

#sem_ver_formatObject



139
140
141
142
143
144
145
# File 'lib/app_version/app_version.rb', line 139

def sem_ver_format
  @@sem_ver_format =  "#{major}.#{minor}"
  @@sem_ver_format << ".#{patch}" unless patch.blank?
  @@sem_ver_format << "-#{meta}" unless meta.blank?
  @@sem_ver_format << "+#{build}" unless build.blank?
  @@sem_ver_format
end

#to_sString

Generate version string

Returns:

  • (String)

    Returns App Version string



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/app_version/app_version.rb', line 152

def to_s
  if defined? @format
    str = eval(@format.to_s.inspect)
  else
    str = "#{major}.#{minor}"
    str << ".#{patch}" unless patch.blank?
    str << "-#{meta}" unless meta.blank?
    str << " M#{milestone}" unless milestone.blank?
    str << " (#{build})" unless build.blank?
    str << " of #{branch}" unless branch.blank?
    str << " by #{committer}" unless committer.blank?
    str << " on #{build_date}" unless build_date.blank?
  end
  str
end