fun_with_version_strings 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.3.5"
12
+ gem "jeweler", "~> 1.8.4"
13
+ end
14
+
15
+ gem "fun_with_files"
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Bryce Anderson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = fun_with_version_strings
2
+
3
+ Declare any Ruby object to have a version:
4
+
5
+ class MyGem; end
6
+
7
+ FunWith::VersionStrings.versionize( MyGem, "0.0.3" )
8
+
9
+ Then the following is true:
10
+
11
+ MyGem.version # => "0.0.3"
12
+ MyGem.version.major # => 0
13
+ MyGem.version.minor # => 0
14
+ MyGem.version.patch # => 3
15
+
16
+ You can also compare versions. Observe:
17
+
18
+ version_a = "9.1.4"
19
+ version_b = "10.1.0"
20
+ version_c = "9.2.0"
21
+
22
+ version_c > version_a # => true
23
+ version_c == version_a # => false
24
+ version_b > version_a # => true
25
+ version_b < version_a # => false
26
+
27
+ But comparisons are a bit tricky. It will try to compare standard strings as version strings, if the standard string is of the proper format. Otherwise
28
+ it will compare the two as regular strings. Note that this means that the
29
+ comparison behaves differently depending on whether the left-hand side is
30
+ a regular string.
31
+
32
+
33
+ == Contributing to fun_with_version_strings
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
37
+ * Fork the project.
38
+ * Start a feature/bugfix branch.
39
+ * Commit and push until you are happy with your contribution.
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2013 Bryce Anderson. See LICENSE.txt for
46
+ further details.
47
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "fun_with_version_strings"
18
+ gem.homepage = "http://github.com/darthschmoo/fun_with_version_strings"
19
+ gem.license = "MIT"
20
+ gem.summary = "Add a version string (with :major,:minor, and :patch methods) to a Ruby object."
21
+ gem.description = "A simple little gem for adding a version string to any Ruby object."
22
+ gem.email = "keeputahweird@gmail.com"
23
+ gem.authors = ["Bryce Anderson"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+
36
+ task :default => :test
37
+
38
+ require 'rdoc/task'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "fun_with_version_strings #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,16 @@
1
+ require 'fun_with_files'
2
+
3
+ module FunWith
4
+ module VersionStrings
5
+ end
6
+ end
7
+
8
+ root = __FILE__.fwf_filepath.dirname.up
9
+
10
+ root.join( "lib", "version_strings" ).requir
11
+
12
+ FunWith::Files::RootPath.rootify( FunWith::VersionStrings, root )
13
+ FunWith::VersionStrings::VersionString.send( :include, FunWith::VersionStrings::Comparator )
14
+ FunWith::VersionStrings.extend( FunWith::VersionStrings::API )
15
+ FunWith::VersionStrings.versionize( FunWith::VersionStrings )
16
+
@@ -0,0 +1,26 @@
1
+ module FunWith
2
+ module VersionStrings
3
+ # methods that can be called directly upon the module FunWith::VersionStrings
4
+ module API
5
+ # if no string is given, the given object is assumed to have a root path and
6
+ # to have a file "VERSION" immediately underneath that root. That's standard for
7
+ # my gems.
8
+ def versionize( obj, version_string = nil )
9
+ if obj.respond_to?(:version)
10
+ warn( "FunWith::VersionStrings (warn): #{obj.to_s[0..100]} already responds to version()")
11
+ end
12
+
13
+ if version_string.nil?
14
+ if obj.respond_to?(:root) && obj.root("VERSION").file?
15
+ version_string = obj.root("VERSION").read
16
+ else
17
+ raise "No version string specified, and cannot infer version from VERSION file."
18
+ end
19
+ end
20
+
21
+ obj.extend( VersionizeExtender )
22
+ obj.version( version_string )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ module FunWith
2
+ module VersionStrings
3
+ module Comparator
4
+
5
+ # Unless both strings are valid version formats, pass comparison off to super
6
+ def <=>( rhs )
7
+
8
+ if rhs.is_a?(VersionString) || rhs =~ VersionString::FORMAT_VALIDATOR
9
+ rhs = rhs.fwvs_version_string
10
+
11
+ for level in [:major, :minor, :patch]
12
+ unless self.send(level) == rhs.send(level)
13
+ return self.send(level) <=> rhs.send(level)
14
+ end
15
+ end
16
+
17
+ return 0
18
+ else
19
+ super( rhs )
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def fwvs_version_string
3
+ if self.is_a?(FunWith::VersionStrings::VersionString)
4
+ self.dup
5
+ else
6
+ FunWith::VersionStrings::VersionString.new( self )
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ module FunWith
2
+ module VersionStrings
3
+ class VersionString < String
4
+ FORMAT_VALIDATOR = /^(?:(?<major>\d+)\.?)?(?:(?<minor>\d+)\.?)?(?:(?<patch>\d+))?$/
5
+ attr_accessor :major, :minor, :patch
6
+ alias :tiny :patch
7
+
8
+ # version must be of format n.nn or n.nn.nn
9
+ def initialize( src )
10
+ @match_data = src.match( FORMAT_VALIDATOR )
11
+ raise ArgumentError.new( "Not a valid version format for a VersionString." ) if @match_data.nil?
12
+
13
+ @major = (@match_data[:major] || 0).to_i
14
+ @minor = (@match_data[:minor] || 0).to_i
15
+ @patch = (@match_data[:patch] || 0).to_i
16
+
17
+ super( "#{@major}.#{@minor}.#{@patch}" )
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+
24
+
@@ -0,0 +1,10 @@
1
+ module FunWith
2
+ module VersionStrings
3
+ module VersionizeExtender
4
+ def version( str = nil )
5
+ @fwvs_version_string ||= str.fwvs_version_string
6
+ @fwvs_version_string
7
+ end
8
+ end
9
+ end
10
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'fun_with_version_strings'
16
+
17
+ include FunWith::VersionStrings
18
+
19
+ class Test::Unit::TestCase
20
+ end
21
+
22
+ class VersionStringsTestCase < Test::Unit::TestCase
23
+ def assert_valid_version_string( str, opts = {} )
24
+ str = str.fwvs_version_string
25
+ for level in [:major, :minor, :patch]
26
+ if opts[level]
27
+ assert_equal opts[level], str.send(level)
28
+ end
29
+ end
30
+ end
31
+
32
+ def versioned_module( &block )
33
+ @module = Module.new
34
+
35
+ assert !( @module.respond_to?( :version ) ), "New module shouldn't respond to :version method."
36
+
37
+ FunWith::VersionStrings.versionize( @module, "3.1.2" )
38
+ assert @module.respond_to?( :version ), "New module should respond to :version method."
39
+
40
+ assert @module.version.is_a?(String), "@module.version should yield a string"
41
+ assert @module.version.is_a?(FunWith::VersionStrings::VersionString), "@module.version should yield a VersionString"
42
+ yield
43
+ end
44
+ end
@@ -0,0 +1,88 @@
1
+ require 'helper'
2
+
3
+ class TestFunWithVersionStrings < VersionStringsTestCase
4
+ context "setup" do
5
+ should "respond to all the things" do
6
+ assert "".respond_to?(:fwvs_version_string), "String not responding"
7
+ assert FunWith::VersionStrings.respond_to?(:root), "FunWith::VersionStrings not responding to root"
8
+ assert FunWith::VersionStrings.respond_to?(:versionize), "FunWith::VersionStrings not responding to versionize"
9
+ end
10
+ end
11
+
12
+
13
+ should "accept the following strings as version formats" do
14
+ assert_valid_version_string( "", {:major => 0, :minor => 0, :patch => 0})
15
+ assert_valid_version_string( "0.", {:major => 0, :minor => 0, :patch => 0})
16
+ assert_valid_version_string( "0.0", {:major => 0, :minor => 0, :patch => 0})
17
+ assert_valid_version_string( "0.0.", {:major => 0, :minor => 0, :patch => 0})
18
+ assert_valid_version_string( "0.0.0", {:major => 0, :minor => 0, :patch => 0})
19
+ end
20
+
21
+ should "compare things correctly" do
22
+ # larger always comes first
23
+ gt_comparisons = [
24
+ ["1.7","1.4"],
25
+ ["0.9","0.8"],
26
+ ["0.0.1",""],
27
+ ["0.0.1","0.0."],
28
+ ["3.14159","3.141"],
29
+ ["1",""]
30
+ ]
31
+
32
+ eq_comparisons = [
33
+ ["0.0.0",""],
34
+ ["1.0.0","1.0"],
35
+ ["1.0.","1"],
36
+ ["3.1.1","3.1.1"],
37
+ ["3","3.0."],
38
+ ["2.4","2.4.0"]
39
+ ]
40
+
41
+ for c in gt_comparisons
42
+ assert( c.first.fwvs_version_string > c.last.fwvs_version_string, "#{c.first.inspect} should be greater than #{c.last.inspect}" )
43
+ end
44
+
45
+ for c in eq_comparisons
46
+ assert_equal( c.first.fwvs_version_string, c.last.fwvs_version_string )
47
+ end
48
+ end
49
+
50
+ context "trying it out" do
51
+ should "be able to call .version() on a class" do
52
+ versioned_module do
53
+ assert @module.version.length == 5, "@module.version.length == 5"
54
+ end
55
+ end
56
+ end
57
+
58
+ context "trying out default approach to finding a module version" do
59
+ should "raise error when no string given" do
60
+ mod = Module.new
61
+
62
+ assert_raises( RuntimeError ) do
63
+ FunWith::VersionStrings.versionize( @module )
64
+ end
65
+ end
66
+
67
+ should "raise error when root has no VERSION file" do
68
+ mod = Module.new
69
+ FunWith::Files::RootPath.rootify(mod, FunWith::VersionStrings.root("test"))
70
+ assert_raises( RuntimeError ) do
71
+ FunWith::VersionStrings.versionize( @module )
72
+ end
73
+ end
74
+
75
+ should "set version by VERSION file when module has a root" do
76
+ mod = Module.new
77
+ FunWith::Files::RootPath.rootify( mod, FunWith::VersionStrings.root )
78
+ FunWith::VersionStrings.versionize( mod )
79
+ assert FunWith::VersionStrings.respond_to?(:version)
80
+ assert mod.respond_to?(:version)
81
+ assert_equal FunWith::VersionStrings.version, mod.version
82
+ end
83
+ end
84
+
85
+ should "compare incorrectly when dealing with regular strings" do
86
+ flunk "not written"
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fun_with_version_strings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryce Anderson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fun_with_files
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.5
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.3.5
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.4
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.4
94
+ description: A simple little gem for adding a version string to any Ruby object.
95
+ email: keeputahweird@gmail.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - LICENSE.txt
100
+ - README.rdoc
101
+ files:
102
+ - .document
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ - Rakefile
107
+ - VERSION
108
+ - lib/fun_with_version_strings.rb
109
+ - lib/version_strings/api.rb
110
+ - lib/version_strings/comparator.rb
111
+ - lib/version_strings/string.rb
112
+ - lib/version_strings/version_string.rb
113
+ - lib/version_strings/versionize_extender.rb
114
+ - test/helper.rb
115
+ - test/test_fun_with_version_strings.rb
116
+ homepage: http://github.com/darthschmoo/fun_with_version_strings
117
+ licenses:
118
+ - MIT
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ segments:
130
+ - 0
131
+ hash: 3139587629202339878
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.25
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Add a version string (with :major,:minor, and :patch methods) to a Ruby object.
144
+ test_files: []