ruby_version_reader 0.1.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: 78c1f3b88afaaf0f0db9269eeb7cb732f203b412
4
+ data.tar.gz: 5e0249a48e804eea1bec2193faa345e12b5326ec
5
+ SHA512:
6
+ metadata.gz: 79b94c053902dbe9fbf7327c02b50e4b91f41e94a968ee508f698ba1fd95452343418592942217994379fba14a2526e3de2fa8aceb1ff467f9c786a07ed1c005
7
+ data.tar.gz: 303d4ac392a8c34ea5058398bdc15f5c90c24b259ed754aa7884953b88d6ac42a0eb296415e8028783abf5b2ec870df75a785a84f0bcfe43eb8a316f977c7b61
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ #- ruby-head (travis error)
5
+ - 2.1.0
6
+ - 2.0.0
7
+ - 1.9.3
8
+ - 1.9.2
9
+ - 1.8.7
10
+ - ree
11
+ - jruby
12
+ - jruby-19mode
13
+ - jruby-18mode
14
+ - rbx
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Change Log
2
+
3
+ ## 0.1.0 - 2014-12-03
4
+ ### Added
5
+ - Initial Release with tested rvm support
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ platform :rbx do
6
+ gem 'rubysl-date'
7
+ gem 'rubysl-singleton'
8
+ end
9
+
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_version_reader (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ json (1.8.1)
11
+ rake (10.4.1)
12
+ rdoc (3.12.2)
13
+ json (~> 1.4)
14
+ rspec (2.99.0)
15
+ rspec-core (~> 2.99.0)
16
+ rspec-expectations (~> 2.99.0)
17
+ rspec-mocks (~> 2.99.0)
18
+ rspec-core (2.99.2)
19
+ rspec-expectations (2.99.2)
20
+ diff-lcs (>= 1.1.3, < 2.0)
21
+ rspec-mocks (2.99.2)
22
+ rubygems-tasks (0.2.4)
23
+ rubysl-date (2.0.8)
24
+ rubysl-singleton (2.0.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.0)
31
+ rake (~> 10.1)
32
+ rdoc (~> 3.0)
33
+ rspec (~> 2.4)
34
+ ruby_version_reader!
35
+ rubygems-tasks (~> 0.2)
36
+ rubysl-date
37
+ rubysl-singleton
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Marcos Wright-Kuhns
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,50 @@
1
+ = RubyVersionReader
2
+
3
+ Provides a +RubyVersionReader+ to simplify reading of +.ruby-version+ and +.ruby-gemset+ files in your programs.
4
+
5
+ == Setup
6
+
7
+ On your command-line:
8
+
9
+ $ gem install ruby_version_reader
10
+
11
+ In Ruby:
12
+
13
+ require 'ruby_version_reader'
14
+
15
+ == Usage
16
+
17
+ # Initialize your reader
18
+ reader = RubyVersionReader.new
19
+
20
+ # The current base path where .ruby-version files are being looked for
21
+ reader.path
22
+
23
+ # Access .ruby-version and .ruby-gemset data
24
+ reader.to_s #=> 'ruby-1.8.7-p249@rvr_gemset'
25
+ reader.full_version #=> '1.8.7-p249'
26
+ reader.gemset #=> 'rvr_gemset'
27
+
28
+ # Check for the main version with a Float
29
+ reader.is? 2.1
30
+
31
+ # Use strings for exacter checking
32
+ reader.is.above '1.9.2-p330'
33
+ reader.is.at_least '2.0.0' # or exactly, below, at_most
34
+
35
+ # You can use the common comparison operators
36
+ reader >= '1.8.7'
37
+ reader.between? '1.8.7', '1.9.2'
38
+
39
+ # Misc Accessors
40
+ reader.engine #=> 'ruby'
41
+ reader.major # => 1
42
+ reader.minor # => 8
43
+ reader.tiny # => 7
44
+ reader.patchlevel # => 249
45
+
46
+ == Credit
47
+
48
+ Basic RubyVersionReader comparison logic from https://github.com/janlelis/ruby_version which originated from the zucker gem.
49
+
50
+ Copyright (c) 2014 Marcos Wright-Kuhns. MIT License.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler'
7
+ rescue LoadError => e
8
+ warn e.message
9
+ warn "Run `gem install bundler` to install Bundler."
10
+ exit -1
11
+ end
12
+
13
+ begin
14
+ Bundler.setup(:development)
15
+ rescue Bundler::BundlerError => e
16
+ warn e.message
17
+ warn "Run `bundle install` to install missing gems."
18
+ exit e.status_code
19
+ end
20
+
21
+ require 'rake'
22
+
23
+ require 'rubygems/tasks'
24
+ Gem::Tasks.new
25
+
26
+ require 'rdoc/task'
27
+ RDoc::Task.new do |rdoc|
28
+ rdoc.title = "ruby_version"
29
+ end
30
+ task :doc => :rdoc
31
+
32
+ require 'rspec/core/rake_task'
33
+ RSpec::Core::RakeTask.new
34
+
35
+ task :test => :spec
36
+ task :default => :spec
@@ -0,0 +1,146 @@
1
+ require 'date'
2
+ require 'time'
3
+
4
+ class RubyVersionReader
5
+ VERSION = '0.1.0'
6
+
7
+ attr_accessor :path, :environment_manager
8
+
9
+ def initialize(given_path, given_environment_manager = 'rvm')
10
+ given_path = './' if given_path.empty?
11
+ @path = File.expand_path(given_path)
12
+
13
+ @environment_manager = given_environment_manager
14
+ end
15
+
16
+ def to_s
17
+ return @to_s if @to_s
18
+
19
+ @to_s = "#{engine}-#{full_version}"
20
+ @to_s += "@#{gemset}" unless gemset.empty?
21
+
22
+ @to_s
23
+ end
24
+ alias inspect to_s
25
+
26
+ def gemset
27
+ read_ruby_gemset_file
28
+ end
29
+
30
+ def environment_manager_load_string
31
+ case environment_manager
32
+ when 'rvm'
33
+ "rvm use #{to_s}"
34
+ when 'rbenv'
35
+ "RBENV_VERSION=#{to_s}"
36
+ when 'chruby'
37
+ "chruby-exec #{to_s} --"
38
+ else
39
+ raise "Unsupported environment_manager: #{environment_manager.inspect}"
40
+ end
41
+ end
42
+
43
+ # comparable
44
+ def <=>(other)
45
+ value = case other
46
+ when Integer
47
+ major.to_i
48
+ when Float
49
+ full_version.to_f
50
+ when String
51
+ other_engine = extract_engine_from_string(other)
52
+
53
+ if engine == other_engine
54
+ other = extract_full_version_from_string(other)
55
+ full_version
56
+ else
57
+ return engine <=> other_engine
58
+ end
59
+ else
60
+ # A tiny bit of recursion
61
+ return self <=> other.to_s
62
+ end
63
+ value <=> other
64
+ end
65
+ include Comparable
66
+
67
+ # chaining for dsl-like language
68
+ def is?(other = nil)
69
+ if other
70
+ self == other
71
+ else
72
+ self
73
+ end
74
+ end
75
+ alias is is?
76
+
77
+ # aliases
78
+ alias below <
79
+ alias below? <
80
+ alias at_most <=
81
+ alias at_most? <=
82
+ alias above >
83
+ alias above? >
84
+ alias at_least >=
85
+ alias at_least? >=
86
+ alias exactly ==
87
+ alias exactly? ==
88
+ def not(other)
89
+ self != other
90
+ end
91
+ alias not? not
92
+ alias between between?
93
+
94
+ # accessors
95
+ def engine
96
+ @engine ||= extract_engine_from_string(read_ruby_version_file)
97
+ end
98
+
99
+ def full_version
100
+ @full_version ||= extract_full_version_from_string(read_ruby_version_file)
101
+ end
102
+
103
+ def major
104
+ full_version.to_i
105
+ end
106
+ alias main major
107
+
108
+ def minor
109
+ full_version.split('.')[1].to_i
110
+ end
111
+ alias mini minor
112
+
113
+ def tiny
114
+ full_version.split('.')[2].to_i
115
+ end
116
+ alias teeny tiny
117
+
118
+ def patchlevel
119
+ full_version.split('-')[1].to_s
120
+ end
121
+
122
+ private
123
+
124
+ def extract_engine_from_string(value)
125
+ value = value.gsub(/\-?#{Regexp.escape(extract_full_version_from_string(value))}/, '')
126
+ value.empty? ? 'ruby' : value
127
+ end
128
+
129
+ def extract_full_version_from_string(value)
130
+ value.gsub(/\A(\D+\-p?)?/, '')
131
+ end
132
+
133
+ def read_ruby_version_file
134
+ version_path = File.join(path, '.ruby-version')
135
+ File.exists?(version_path) ? read_and_strip(version_path) : ''
136
+ end
137
+
138
+ def read_ruby_gemset_file
139
+ gemset_path = File.join(path, '.ruby-gemset')
140
+ File.exists?(gemset_path) ? read_and_strip(gemset_path) : ''
141
+ end
142
+
143
+ def read_and_strip(file_path)
144
+ File.read(file_path).strip
145
+ end
146
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/ruby_version_reader', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "ruby_version_reader"
7
+ gem.version = RubyVersionReader::VERSION
8
+ gem.summary = 'Read .ruby-version and .ruby-gemset files.'
9
+ gem.description = 'Provides a RubyVersionReader class to simplify reading the .ruby-version and .ruby-gemset files.'
10
+ gem.license = "MIT"
11
+ gem.authors = ["Marcos Wright-Kuhns"]
12
+ gem.email = "marcos@wrightkuhns.com"
13
+ gem.homepage = "https://github.com/metavida/ruby_version_reader"
14
+
15
+ gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) }
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_development_dependency 'bundler', '~> 1.0'
21
+ gem.add_development_dependency 'rake', '~> 10.1'
22
+ gem.add_development_dependency 'rdoc', '~> 3.0'
23
+ gem.add_development_dependency 'rspec', '~> 2.4'
24
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
25
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'RubyVersionReader' do
4
+ before :all do
5
+ @remember_version = RUBY_VERSION
6
+ capture_stderr{ RUBY_VERSION = '1.8.7' }
7
+ end
8
+
9
+ it 'should display RUBY_VERSION if called directly (to_s)' do
10
+ RubyVersionReader.to_s.should == '1.8.7'
11
+ end
12
+
13
+ context 'with "is" method, with parameter' do
14
+ it 'should check for main version (1.8 or 1.9) when Float paramater is given' do
15
+ RubyVersionReader.is?( 1.8 ).should == true
16
+ RubyVersionReader.is?( 1.9 ).should == false
17
+ end
18
+
19
+ it 'should check with string comparison if parameter is not Float' do
20
+ RubyVersionReader.is?( '1.8' ).should == false
21
+ end
22
+ end
23
+
24
+ context 'with "is" method, without parameter, but method chaining' do
25
+ it 'should return a string for usage with comparison operators' do
26
+ (RubyVersionReader.is > '1.8.7').should == false
27
+ (RubyVersionReader <= '1.8.7').should == true
28
+ (RubyVersionReader.is.between? '1.8.6', '1.8.7').should == true
29
+ end
30
+
31
+ it 'should create some handy compare aliases' do
32
+ RubyVersionReader.is.above( '1.8.7' ).should == false
33
+ RubyVersionReader.is.at_least( '1.8.7' ).should == true
34
+ RubyVersionReader.is.exactly( '1.8.7' ).should == true
35
+ end
36
+
37
+ it 'also allows to check for the release dates' do
38
+ RubyVersionReader.is.older_than( Date.today ).should == true
39
+ RubyVersionReader.is.newer_than( '2000-01-01' ).should == true
40
+ end
41
+ end
42
+
43
+ it 'should define some accessors' do
44
+ RubyVersionReader.major.should == 1
45
+ RubyVersionReader.minor.should == 8
46
+ RubyVersionReader.tiny.should == 7
47
+ RubyVersionReader.patchlevel.should == RUBY_PATCHLEVEL
48
+ RubyVersionReader.description.should == RUBY_DESCRIPTION
49
+ end
50
+
51
+ context 'with "gemset" method' do
52
+ it 'should return an empty string by default' do
53
+ RubyVersionReader.gemset.should == ''
54
+ end
55
+
56
+ it 'should work with a .ruby-gemset file' do
57
+ path = '.ruby-gemset'
58
+ given_gemset_name = 'ruby_version_rspec_gemset'
59
+ begin
60
+ # Given
61
+ File.open(path, 'w') { |file| file << given_gemset_name + "\n\n" }
62
+
63
+ # When/Then
64
+ RubyVersionReader.gemset.should == given_gemset_name
65
+ ensure
66
+ File.delete(path)
67
+ end
68
+ end
69
+
70
+ it 'should work by calling `rvm info`' do
71
+ given_gemset_name = 'ruby_version_rspec_gemset'
72
+
73
+ Object.should_receive(:`) {
74
+ YAML.dump({ 'ruby-version@ruby-gemset' => { 'environment' => { 'gemset' => given_gemset_name } } })
75
+ }
76
+
77
+ puts RubyVersionReader.gemset#.should == given_gemset_name
78
+ end
79
+ end
80
+
81
+ after :all do
82
+ capture_stderr{ RUBY_VERSION = @remember_version }
83
+ end
84
+ end
@@ -0,0 +1,20 @@
1
+ require 'ruby_version_reader'
2
+
3
+ require 'rspec'
4
+ require 'stringio'
5
+
6
+ def capture_stdout
7
+ capture = StringIO.new
8
+ restore, $stdout = $stdout, capture
9
+ yield
10
+ $stdout = restore
11
+ capture.string
12
+ end
13
+
14
+ def capture_stderr
15
+ capture = StringIO.new
16
+ restore, $stderr = $stderr, capture
17
+ yield
18
+ $stderr = restore
19
+ capture.string
20
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_version_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcos Wright-Kuhns
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-03 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.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubygems-tasks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.2'
83
+ description: Provides a RubyVersionReader class to simplify reading the .ruby-version
84
+ and .ruby-gemset files.
85
+ email: marcos@wrightkuhns.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CHANGELOG.md
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ - Rakefile
99
+ - lib/ruby_version_reader.rb
100
+ - ruby_version_reader.gemspec
101
+ - spec/ruby_version_reader_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/metavida/ruby_version_reader
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Read .ruby-version and .ruby-gemset files.
127
+ test_files:
128
+ - spec/ruby_version_reader_spec.rb
129
+ - spec/spec_helper.rb