tag_comparer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/tag_comparer.rb +101 -0
  2. metadata +45 -0
@@ -0,0 +1,101 @@
1
+ # A comparison class that can be used to compare version numbers for software
2
+ # projects
3
+ module TagComparer
4
+ extend self
5
+
6
+ # These are the allowed version prefixes at this time
7
+ VERSION_PREFIXES = {
8
+ 'rc' => 100,
9
+ 'beta' => 99,
10
+ 'b' => 99,
11
+ 'alpha' => 98,
12
+ 'a' => 98,
13
+ 'v' => 1 # makes processing easier
14
+ }
15
+
16
+ # Compares a list of version numbers and returns the latest one based on
17
+ # logical version numbering systems. Arguments can be arrays of strings
18
+ # or just strings.
19
+ #
20
+ # ==== Examples
21
+ #
22
+ # get_latest('v1,'v2') # returns 'v2'
23
+ # get_latest('v1.3.beta1', 'v1.3.beta2') # returns 'v1.3.beta2'
24
+ # get_latest(['v1.3.beta1', 'v1.3.rc1', 'v1.3.alpha1']) # returns 'v1.3.rc1'
25
+ def get_latest(*args)
26
+ tags = []
27
+ args.each do |a|
28
+ if a.kind_of?(Array)
29
+ tags.push(a)
30
+ else
31
+ tags.push(a)
32
+ end
33
+ end
34
+
35
+ tags.flatten!
36
+
37
+ latest_tag = tags.first
38
+
39
+ tags.each do |tag|
40
+ latest_tag = compare_items(latest_tag, tag) >= 0 ? latest_tag : tag
41
+ end
42
+
43
+ latest_tag
44
+ end
45
+
46
+ private
47
+
48
+ def compare_items(first_tag, second_tag)
49
+
50
+ first = get_version_parts(first_tag)
51
+ second = get_version_parts(second_tag)
52
+
53
+ length = [first.size, second.size].max
54
+
55
+ for i in 0..length - 1
56
+
57
+ x = first[i]
58
+ y = second[i]
59
+
60
+ if x == nil
61
+ return -1
62
+ elsif y == nil
63
+ return 1
64
+ end
65
+
66
+ if x.match(/\d/) == nil
67
+ result = compare_text_versions(x, y)
68
+ return result unless result == 0
69
+ next
70
+ end
71
+
72
+ x = x.to_i
73
+ y = y.to_i
74
+
75
+ if x > y
76
+ return 1
77
+ elsif x < y
78
+ return -1
79
+ end
80
+ end
81
+
82
+ return 0
83
+ end
84
+
85
+ def compare_text_versions(a, b)
86
+ prefix_a = VERSION_PREFIXES[a] || -1
87
+ prefix_b = VERSION_PREFIXES[b] || -1
88
+
89
+ if prefix_a > prefix_b
90
+ 1
91
+ elsif prefix_a < prefix_b
92
+ -1
93
+ else
94
+ 0
95
+ end
96
+ end
97
+
98
+ def get_version_parts(version)
99
+ version.scan(/([a-zA-Z]+|\d+{1,1})/).flatten
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tag_comparer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Craig MacGregor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A class for comparing tag version numbers for software projects
15
+ email: craigerm@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/tag_comparer.rb
21
+ homepage: https://github.com/craigerm/tag_comparer
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Tag version comparison helper
45
+ test_files: []