version_guard 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e466bebc6133be19c8093f5b9aba44d13443131f
4
+ data.tar.gz: 7298f61f14158299c951971150d66273910b9e99
5
+ SHA512:
6
+ metadata.gz: d482518642a866fb3c4fd1a45265d494c02e3a2236f5575816cd80bffd9b36d1ac29bba1b21c99a9080eac2d3934c91ec97ba4054cd455f0bbfa50fef36aa075
7
+ data.tar.gz: 3849c7bdbc3387a1fe4e1d93fabdf65319cf5fda826d5a52bfc99c90ccec9f9763c73febf50026f537e9e9187eb67c216e8acaf41646b866405f8e1503cbfbf9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ - ruby-head
8
+ - jruby-head
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kenn Ejima
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # VersionGuard
2
+
3
+ [![Build Status](https://travis-ci.org/kenn/version_guard.png)](https://travis-ci.org/kenn/version_guard)
4
+
5
+ A gem-style version checker for Ruby.
6
+
7
+ ```ruby
8
+ VersionGuard.check '2.13.0', '>= 2.2' # => true
9
+ ```
10
+
11
+ VersionGuard has two methods: `abort` and `check`. While `abort` terminates the process if the version requirement is not satisfied, `check` just returns true / false so that you can choose what to do with the failed version.
12
+
13
+ For instance, suppose you have the following lines in `config/initializers/patches.rb` for a Rails app:
14
+
15
+ ```ruby
16
+ VersionGuard.abort 'ActiveRecord::VERSION::STRING', '3.2.9'
17
+ # Monkey patches for ActiveRecord 3.2.9...
18
+ ```
19
+
20
+ The ruby process terminates immediately with a warning message if the version is not 3.2.9. That way, you can prevent the monkey patch from conflicting with the newer versions of ActiveRecord.
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'version_guard'
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ Since VersionGuard uses `Gem::Version` and `Gem::Version::Requirement` internally, any kind of the funky version comparisons that you use with `Gemfile` are supported.
33
+
34
+ The first argument can be either a version string like `"1.2.3"` or a name that can be converted to the constant that holds the version string. For most gems, it should be something like `GemName::VERSION`.
35
+
36
+ ```ruby
37
+ # As a simple version comparator
38
+ VersionGuard.check '2.13.0', '~> 2.2'
39
+
40
+ # For mobile app backend
41
+ if VersionGuard.check request.headers['x-ios-app-version'], '< 1.2.1'
42
+ # Send message "Your mobile app is too old, upgrade now!"
43
+ end
44
+
45
+ # For capistrano with RVM, in deploy.rb
46
+ require 'version_guard'
47
+ VersionGuard.abort ENV['rvm_version'].split(/\s/).first, '>= 1.18.5'
48
+
49
+ # Multiple version requirements are supported
50
+ VersionGuard.abort 'MyGem::VERSION', ['> 0.9.0', '< 1.1.0']
51
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = 'test/test*.rb'
6
+ end
7
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module VersionGuard
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'version_guard/version'
2
+
3
+ module VersionGuard
4
+ class << self
5
+ def abort(target, requirement, options = {})
6
+ Checker.new(target, requirement, options).abort_if_fail
7
+ end
8
+
9
+ def check(target, requirement, options = {})
10
+ Checker.new(target, requirement, options).check
11
+ end
12
+ end
13
+
14
+ class Checker
15
+ def initialize(target, requirement, options = {})
16
+ if target.nil? or requirement.nil? or target.empty? or requirement.empty?
17
+ raise Error, 'target and requirement cannot be nil'
18
+ end
19
+
20
+ @requirement = requirement
21
+ @options = options
22
+
23
+ @version = begin
24
+ Gem::Version.new(target) # raise ArgumentError unless target is like '1.2.3'
25
+ target
26
+ rescue ArgumentError
27
+ # constantize string like 'ActiveRecord::VERSION::STRING'
28
+ names = target.split('::')
29
+ names.shift if names.empty? || names.first.empty? # Normalize ::TOPLEVEL
30
+ @options[:name] ||= names.first
31
+ constant = Object
32
+ names.each do |name|
33
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
34
+ end
35
+ constant
36
+ end
37
+ end
38
+
39
+ def check
40
+ requirements = @requirement.is_a?(Array) ? @requirement : [@requirement]
41
+ Gem::Version::Requirement.new(requirements).satisfied_by?(Gem::Version.new(@version))
42
+ end
43
+
44
+ def abort_if_fail
45
+ if check
46
+ puts "#{message_base} passed" if @options[:verbose]
47
+ else
48
+ trace = caller.find{|i| i !~ /version_checker\.rb/ }
49
+ filename, lineno = trace.split(':')[0..1]
50
+ abort "#{message_base} failed - check line #{lineno} in #{filename}"
51
+ end
52
+ end
53
+
54
+ def message_base
55
+ if @options[:name]
56
+ "Version check for #{@options[:name]} (#{@version.inspect} with #{@requirement.inspect})"
57
+ else
58
+ "Version check #{@version.inspect} with #{@requirement.inspect}"
59
+ end
60
+ end
61
+ end
62
+
63
+ Error = Class.new(StandardError)
64
+ end
@@ -0,0 +1,51 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'version_guard'
4
+
5
+ class TestVersionGuard < MiniTest::Unit::TestCase
6
+ VERSION = '1.2.3'
7
+
8
+ def test_true
9
+ assert_equal true, VersionGuard.check('1', '1')
10
+ assert_equal true, VersionGuard.check('1', '= 1')
11
+ assert_equal true, VersionGuard.check('1', '>= 1')
12
+ assert_equal true, VersionGuard.check('1', '~> 1')
13
+ assert_equal true, VersionGuard.check('2', '> 1')
14
+ assert_equal true, VersionGuard.check('1.0', '>= 1.0')
15
+ assert_equal true, VersionGuard.check('1.0.0', '>= 0')
16
+ assert_equal true, VersionGuard.check('1.0.1', '~> 1.0.0')
17
+ assert_equal true, VersionGuard.check('1.0.0', '< 2.0.0')
18
+ assert_equal true, VersionGuard.check('1.0.0', ['> 0.9.0', '< 1.1.0'])
19
+ end
20
+
21
+ def test_false
22
+ assert_equal false, VersionGuard.check('1', '2')
23
+ assert_equal false, VersionGuard.check('1', '= 2')
24
+ assert_equal false, VersionGuard.check('1', '>= 2')
25
+ assert_equal false, VersionGuard.check('1', '~> 2')
26
+ assert_equal false, VersionGuard.check('2', '> 2')
27
+ assert_equal false, VersionGuard.check('1.0', '>= 1.1')
28
+ assert_equal false, VersionGuard.check('1.0.0', '>= 1.0.1')
29
+ assert_equal false, VersionGuard.check('1.0.1', '~> 1.1')
30
+ assert_equal false, VersionGuard.check('1.0.0', '> 2.0.0')
31
+ assert_equal false, VersionGuard.check('1.0.0', ['> 1.0.1', '< 1.1.0'])
32
+ end
33
+
34
+ def test_constants
35
+ assert_equal true, VersionGuard.check('TestVersionGuard::VERSION', '> 1.0.0')
36
+ assert_equal false, VersionGuard.check('TestVersionGuard::VERSION', '> 2.0.0')
37
+ assert_raises(NameError) { VersionGuard.check('a', '> 1.0.0') }
38
+ end
39
+
40
+ def test_nil
41
+ assert_raises(VersionGuard::Error) { VersionGuard.check(nil, '1') }
42
+ assert_raises(VersionGuard::Error) { VersionGuard.check('1', nil) }
43
+ assert_raises(VersionGuard::Error) { VersionGuard.check(nil, nil) }
44
+ end
45
+
46
+ def test_empty_string
47
+ assert_raises(VersionGuard::Error) { VersionGuard.check('', '') }
48
+ assert_raises(VersionGuard::Error) { VersionGuard.check('', '1') }
49
+ assert_raises(VersionGuard::Error) { VersionGuard.check('1', '') }
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'version_guard/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'version_guard'
8
+ spec.version = VersionGuard::VERSION
9
+ spec.authors = ['Kenn Ejima']
10
+ spec.email = ['kenn.ejima@gmail.com']
11
+ spec.description = %q{A gem-style version checker}
12
+ spec.summary = %q{A gem-style version checker}
13
+ spec.homepage = 'https://github.com/kenn/version_guard'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: version_guard
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kenn Ejima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A gem-style version checker
42
+ email:
43
+ - kenn.ejima@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .travis.yml
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/version_guard.rb
55
+ - lib/version_guard/version.rb
56
+ - test/test_version_guard.rb
57
+ - version_guard.gemspec
58
+ homepage: https://github.com/kenn/version_guard
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A gem-style version checker
82
+ test_files:
83
+ - test/test_version_guard.rb