version 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ $: << 'lib'
2
+
3
+ require 'version'
4
+
5
+ require 'rubygems'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'spec/rake/spectask'
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = 'version'
12
+ s.author = 'Stephen Touset'
13
+ s.email = 'stephen@touset.org'
14
+ s.summary = 'simple version-number encapsulation'
15
+ s.version = '0.5.0'
16
+ s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'spec/**/*']
17
+
18
+ s.add_development_dependency 'rspec'
19
+ end
20
+
21
+ Rake::GemPackageTask.new(spec) do |gem|
22
+ gem.need_tar = true
23
+ end
24
+
25
+ Rake::RDocTask.new do |doc|
26
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
27
+
28
+ doc.title = 'emcien-engine #{version}'
29
+ doc.rdoc_dir = 'doc'
30
+ doc.rdoc_files.include('README*')
31
+ doc.rdoc_files.include('lib/**/*.rb')
32
+ end
33
+
34
+ Spec::Rake::SpecTask.new(:spec) do |spec|
35
+ spec.spec_files = FileList['spec/**/*_spec.rb']
36
+ end
37
+
38
+ task :default => :spec
data/lib/ext/array.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Array
2
+ #
3
+ # Converts the Array into a version number.
4
+ #
5
+ def to_version
6
+ Version.new *self
7
+ end
8
+ end
data/lib/ext/hash.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Hash
2
+ #
3
+ # Converts the Hash into a version number.
4
+ #
5
+ def to_version
6
+ Version.new *self.values_at(:major, :minor, :revision, :rest)
7
+ end
8
+ end
data/lib/ext/string.rb ADDED
@@ -0,0 +1,8 @@
1
+ class String
2
+ #
3
+ # Converts the String into a version number.
4
+ #
5
+ def to_version
6
+ Version.new *self.split(%r{\.})
7
+ end
8
+ end
@@ -0,0 +1,2 @@
1
+ require 'rake'
2
+
data/lib/version.rb ADDED
@@ -0,0 +1,149 @@
1
+ require 'ext/array'
2
+ require 'ext/hash'
3
+ require 'ext/string'
4
+
5
+ #
6
+ # Encodes version-numbering logic into a convenient class.
7
+ #
8
+ class Version
9
+ include Comparable
10
+
11
+ #
12
+ # Creates a new version number, with a +major+ version number, +minor+
13
+ # revision number, +revision+ number, and optionally more (unnamed)
14
+ # version components.
15
+ #
16
+ def initialize(major, minor = 0, revision = nil, *rest)
17
+ self.components = []
18
+
19
+ self.major = major
20
+ self.minor = minor
21
+ self.revision = revision
22
+
23
+ rest.each.with_index {|v, i| self[3 + i] = v }
24
+ end
25
+
26
+ #
27
+ # For +major+, +minor+, and +revision+, make a helper method that gets and
28
+ # sets each based on accessing indexes.
29
+ #--
30
+ # TODO: make these rdoc-capable
31
+ #++
32
+ #
33
+ [ :major, :minor, :revision ].each.with_index do |component, i|
34
+ define_method(:"#{component}") { self[i] }
35
+ define_method(:"#{component}=") {|v| self[i] = v.to_s }
36
+ end
37
+
38
+ #
39
+ # Retrieves the component of the Version at +index+.
40
+ #
41
+ def [](index)
42
+ self.components[index]
43
+ end
44
+
45
+ #
46
+ # Set the component of the Version at +index+ to +value+. Zeroes out any
47
+ # trailing components.
48
+ #
49
+ # If +index+ is greater than the length of the version number, pads the
50
+ # version number with zeroes until +index+.
51
+ #--
52
+ # TODO: rewrite sanely
53
+ #++
54
+ def []=(index, value)
55
+ self.resize!(index) and return if value.nil? || value.empty?
56
+
57
+ if index < self.length
58
+ length = self.length - index
59
+ zeroes = Array.new(length, 0)
60
+
61
+ self.components[index, length] = zeroes
62
+ self.components[index] = value
63
+ else
64
+ length = index - self.length
65
+ zeroes = Array.new(length, 0)
66
+
67
+ self.components += zeroes
68
+ self.components << value
69
+ end
70
+ end
71
+
72
+ #
73
+ # Resizes the Version to +length+, removing any trailing components. Is a
74
+ # no-op if +length+ is greater than its current length.
75
+ #
76
+ def resize!(length)
77
+ self.components = self.components[0, length]
78
+ self
79
+ end
80
+
81
+ #
82
+ # Bumps the version number. Pass +index+ to bump a component other than the
83
+ # least-significant part. Set +trim+ to true if you want the version to be
84
+ # resized to only large enough to contain the index.
85
+ #
86
+ # "1.0.4a".bump! # => "1.0.4b"
87
+ # "1.0.4b".bump!(1, true) # => "1.2"
88
+ #
89
+ def bump!(index = self.length - 1, trim = false)
90
+ self.resize!(index + 1) if trim
91
+ self[index] = (self[index] || -1).succ
92
+ self
93
+ end
94
+
95
+ #
96
+ # Returns the current length of the version number.
97
+ #
98
+ def length
99
+ self.components.length
100
+ end
101
+
102
+ #
103
+ # Compares a Version against any +other+ object that responds to
104
+ # +to_version+.
105
+ #
106
+ def <=>(other)
107
+ self.to_a <=> other.to_version.to_a
108
+ end
109
+
110
+ #
111
+ # Converts the version number into an array of its components.
112
+ #
113
+ def to_a
114
+ self.components
115
+ end
116
+
117
+ #
118
+ # Converts the version number into a hash of its components.
119
+ #
120
+ def to_hash
121
+ { :major => self.major,
122
+ :minor => self.minor,
123
+ :revision => self.revision,
124
+ :rest => self.length > 3 ? self.components.drop(3) : nil }
125
+ end
126
+
127
+ #
128
+ # The canonical representation of a version number.
129
+ #
130
+ def to_s
131
+ self.components.join('.')
132
+ end
133
+
134
+ #
135
+ # Returns +self+.
136
+ #
137
+ def to_version
138
+ self
139
+ end
140
+
141
+ #
142
+ # Aliased from +to_s+.
143
+ #
144
+ alias inspect to_s
145
+
146
+ protected
147
+
148
+ attr_accessor :components
149
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'emcien-engine'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Touset
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-04 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: stephen@touset.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - Rakefile
35
+ - lib/ext/array.rb
36
+ - lib/ext/hash.rb
37
+ - lib/ext/string.rb
38
+ - lib/rake/version_task.rb
39
+ - lib/version.rb
40
+ - spec/spec.opts
41
+ - spec/spec_helper.rb
42
+ has_rdoc: true
43
+ homepage:
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: simple version-number encapsulation
70
+ test_files: []
71
+