Class: App::Version

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

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"


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/app_version/app_version.rb', line 21

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|
      raise ArgumentError.new("The #{param.to_s} 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

#branchObject

Returns the value of attribute branch.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def branch
  @branch
end

#buildObject

Returns the value of attribute build.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def build
  @build
end

#build_dateObject

Returns the value of attribute build_date.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def build_date
  @build_date
end

#committerObject

Returns the value of attribute committer.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def committer
  @committer
end

#formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def format
  @format
end

#majorObject

Returns the value of attribute major.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def major
  @major
end

#metaObject

Returns the value of attribute meta.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def meta
  @meta
end

#milestoneObject

Returns the value of attribute milestone.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def milestone
  @milestone
end

#minorObject

Returns the value of attribute minor.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



9
10
11
# File 'lib/app_version/app_version.rb', line 9

def patch
  @patch
end

Class Method Details

.load(path) ⇒ Object

Loads the version information from a YAML file.



88
89
90
# File 'lib/app_version/app_version.rb', line 88

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

.parse(version) ⇒ Object

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

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/app_version/app_version.rb', line 65

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+))?/)

  raise 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

  return version
end

Instance Method Details

#<=>(other) ⇒ Object



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

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 = self.send(meth) || -1
    lhs = other.send(meth) || -1

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

  return 0
end

#to_sObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/app_version/app_version.rb', line 108

def to_s
  if @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