Class: Version

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

Defined Under Namespace

Modules: ShortCut Classes: Tag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, tag = nil, tag_offset = nil, category = nil, name = nil, release_name = nil) ⇒ Version

protected :major=, :minor=, :tag=,

:category=, :name=, :release_name=


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/version.rb', line 15

def initialize ( major, minor, tag=nil, tag_offset=nil,
                 category=nil, name=nil, release_name=nil )
  @major    = major
  @minor    = minor
  @tag      = Tag[tag || :off, tag_offset || 0]
  if @tag.offset.zero?
    case @tag.name
    when :p then raise ArgumentError, 'The patch level tag (p) needs an offset'
    when :r then raise ArgumentError, 'The revision tag (r) needs an offset'
    end
  elsif @tag.name == :off
    raise ArgumentError, 'The official release tag (off) cannot have an offset'
  end
  @category = category || ''
  @name     = name || ''
  @release_name = release_name || ''
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



11
12
13
# File 'lib/version.rb', line 11

def category
  @category
end

#majorObject

Returns the value of attribute major.



11
12
13
# File 'lib/version.rb', line 11

def major
  @major
end

#minorObject

Returns the value of attribute minor.



11
12
13
# File 'lib/version.rb', line 11

def minor
  @minor
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/version.rb', line 11

def name
  @name
end

#release_nameObject

Returns the value of attribute release_name.



11
12
13
# File 'lib/version.rb', line 11

def release_name
  @release_name
end

#tagObject

Returns the value of attribute tag.



11
12
13
# File 'lib/version.rb', line 11

def tag
  @tag
end

Class Method Details

.parse(str) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/version.rb', line 33

def self.parse ( str )
  re_cat_name = /([\w-]+\/)?([\w-]+)/
  re_tag = /_(r|alpha|beta|pre|rc|off|p)(\d*)/
  unless str =~ /^(#{re_cat_name}-)?([\d.]+)(#{re_tag})?(#.*)?$/
    raise ArgumentError, "Cannot parse your version: #{str}"
  end
  category, name, head, _, tag, offset, release_name = Regexp.last_match[2..8]
  parts = head.split('.').map { |x| x.to_i }
  raise ArgumentError, "Too many parts: #{head}" if parts.size > 2
  major, minor = parts

  # Remove now useless chars (/, #)
  category[-1]    = '' if category
  release_name[0] = '' if release_name

  tag = (tag.nil?)? :off : tag.to_sym
  new(major || 0, minor || 0, tag, offset.to_i,
      category || '', name || '', release_name || '')
end

.yaml_new(klass, tag, val) ⇒ Object



53
54
55
# File 'lib/version.rb', line 53

def self.yaml_new( klass, tag, val )
  parse val
end

Instance Method Details

#<=>(other) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/version.rb', line 57

def <=>(other)
  category_cmp = @category <=> other.category
  return category_cmp unless category_cmp.zero?

  name_cmp = @name <=> other.name
  return name_cmp unless name_cmp.zero?

  major_cmp = @major <=> other.major
  return major_cmp unless major_cmp.zero?
  
  minor_cmp = @minor <=> other.minor
  return minor_cmp unless minor_cmp.zero?
  
  @tag <=> other.tag
end

#alpha_releaseObject



198
199
200
# File 'lib/version.rb', line 198

def alpha_release
  tag_release :alpha
end

#beta_releaseObject



194
195
196
# File 'lib/version.rb', line 194

def beta_release
  tag_release :beta
end

#category_and_nameObject



85
86
87
# File 'lib/version.rb', line 85

def category_and_name
  (@name.empty?)? '' : ((@category.empty?)? "#@name-" : "#@category/#@name-")
end

#import!Object



208
209
210
# File 'lib/version.rb', line 208

def import!
  true
end

#inspectObject



101
102
103
# File 'lib/version.rb', line 101

def inspect
  "#<Version: #{to_s}>"
end

#major_minorObject



77
78
79
# File 'lib/version.rb', line 77

def major_minor
  "#@major.#@minor"
end

#major_releaseObject



129
130
131
132
133
134
135
# File 'lib/version.rb', line 129

def major_release
  release do |r|
    r.major += 1
    r.minor = 0
    r.tag = Tag[:alpha, 0]
  end
end

#minor_releaseObject



137
138
139
140
141
142
# File 'lib/version.rb', line 137

def minor_release
  release do |r|
    r.minor += 1
    r.tag = Tag[:alpha, 0]
  end
end

#official?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/version.rb', line 73

def official?
  @tag.name == :off
end

#official_releaseObject



182
183
184
# File 'lib/version.rb', line 182

def official_release
  tag_release :off
end

#patch_level_releaseObject



178
179
180
# File 'lib/version.rb', line 178

def patch_level_release
  tag_release :p
end

#pre_releaseObject



190
191
192
# File 'lib/version.rb', line 190

def pre_release
  tag_release :pre
end

#pretty_versionObject



115
116
117
118
119
# File 'lib/version.rb', line 115

def pretty_version
  more = ''
  more << " #@release_name" unless @release_name.empty?
  "#{@tag.pretty_string}: #{major_minor}#{more}"
end

#release(&block) ⇒ Object



202
203
204
205
206
# File 'lib/version.rb', line 202

def release ( &block )
  x = dup
  x.instance_eval { block[self] }
  x
end

#release_candidateObject



186
187
188
# File 'lib/version.rb', line 186

def release_candidate
  tag_release :rc
end

#short_stringObject



95
96
97
98
99
# File 'lib/version.rb', line 95

def short_string
  s = ''
  s << @name << '-' unless @name.empty?
  s << major_minor << tag_string
end

#tag_release(tag) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/version.rb', line 159

def tag_release ( tag )
  return incr_tag_release if @tag.name == tag

  dest = new_tag_release tag
  off = new_tag_release :off
  if self < dest and dest <= off
    dest
  elsif off <= self and self < dest
    dest
  elsif self < off and off < dest
    raise ArgumentError,
      "You need to be at least on an official release (not #{self}) to go on #{dest}"
  elsif dest < self
    raise ArgumentError, "You cannot downgrade (#{self} to #{dest})"
  else
    raise ArgumentError, "Unhandled case: self=#{self}, dest=#{dest}, off=#{off}"
  end
end

#tag_stringObject



81
82
83
# File 'lib/version.rb', line 81

def tag_string
  (official?)? '' : "_#{@tag.name}#{(@tag.offset.zero?)? '' : @tag.offset}"
end

#to_aObject



121
122
123
# File 'lib/version.rb', line 121

def to_a
  [ @major, @minor, @tag.name, @tag.offset ]
end

#to_positive_integersObject



125
126
127
# File 'lib/version.rb', line 125

def to_positive_integers
  [@major, @minor, @tag.rank + 5, @tag.offset]
end

#to_rubygems_version_stringObject



105
106
107
# File 'lib/version.rb', line 105

def to_rubygems_version_string
  to_positive_integers.join '.'
end

#to_sObject



89
90
91
92
93
# File 'lib/version.rb', line 89

def to_s
  s = category_and_name + major_minor + tag_string
  s << '#' << @release_name unless @release_name.empty?
  s
end

#to_yaml(opts = {}) ⇒ Object



109
110
111
112
113
# File 'lib/version.rb', line 109

def to_yaml ( opts={} )
  YAML::quick_emit nil, opts do |out|
    out.scalar taguri, to_s, :plain
  end
end