which_works 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/which_works.rb +30 -0
- data/spec/all_spec.rb +4 -0
- data/spec/options_spec.rb +77 -0
- data/which_works.gemspec +4 -3
- metadata +25 -22
data/README.md
CHANGED
@@ -15,8 +15,8 @@ Which.which('unknown') #=> nil
|
|
15
15
|
Which.which('/usr/bin/svn') #=> "/usr/bin/svn"
|
16
16
|
Which.which('/usr/bin/foo') #=> nil
|
17
17
|
|
18
|
-
# the :all option
|
19
|
-
# not just the first one found
|
18
|
+
# the :all option returns all executable files,
|
19
|
+
# not just the first one found in the path
|
20
20
|
Which.which('svn', :all => true) #=> [ "/opt/local/bin/svn", "/usr/bin/svn" ]
|
21
21
|
|
22
22
|
# the :array option always returns an array
|
@@ -27,6 +27,18 @@ Which.which('ls', 'screen', :array => true) #=> [ "/bin/ls", "/usr/bin/screen"
|
|
27
27
|
# combined options
|
28
28
|
Which.which('ls', 'svn', :all => true, :array => true)
|
29
29
|
#=> [ "/bin/ls", "/opt/local/bin/svn", "/usr/bin/svn" ]
|
30
|
+
|
31
|
+
# you can change the default options
|
32
|
+
Which.options = { :all => true }
|
33
|
+
Which.options[:array] = true
|
34
|
+
Which.which('ls') #=> [ "/bin/ls" ]
|
35
|
+
Which.which('svn') #=> [ "/opt/local/bin/svn", "/usr/bin/svn" ]
|
36
|
+
|
37
|
+
# default options can be overridden as usual
|
38
|
+
Which.which('ls', :array => false) #=> "/bin/ls"
|
39
|
+
|
40
|
+
# see the current default options
|
41
|
+
Which.options #=> { :all => true, :array => true }
|
30
42
|
```
|
31
43
|
|
32
44
|
Tested with <a href="https://www.relishapp.com/rspec">RSpec</a>, <a href="https://github.com/thoughtbot/shoulda">shoulda</a> and <a href="http://travis-ci.org/#!/AlphaHydrae/which_works">Travis CI</a>.
|
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
|
|
18
18
|
gem.homepage = "http://github.com/AlphaHydrae/which_works"
|
19
19
|
gem.license = "MIT"
|
20
20
|
gem.summary = %Q{Ruby UNIX-like which.}
|
21
|
-
gem.description = %Q{
|
21
|
+
gem.description = %Q{Locates a program file in the user's path. The which method takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.}
|
22
22
|
gem.email = "hydrae.alpha@gmail.com"
|
23
23
|
gem.authors = ["AlphaHydrae"]
|
24
24
|
# dependencies defined in Gemfile
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/lib/which_works.rb
CHANGED
@@ -30,6 +30,7 @@ module Which
|
|
30
30
|
|
31
31
|
found = []
|
32
32
|
options = programs.last.kind_of?(Hash) ? programs.pop : {}
|
33
|
+
options = @@options.merge options
|
33
34
|
|
34
35
|
programs.each do |program|
|
35
36
|
|
@@ -44,6 +45,7 @@ module Which
|
|
44
45
|
|
45
46
|
# using expand_path makes it work with absolute program paths
|
46
47
|
absolute_path = File.expand_path "#{program}#{ext}", path
|
48
|
+
next if found.include? absolute_path
|
47
49
|
|
48
50
|
if File.executable? absolute_path
|
49
51
|
program_found = true
|
@@ -62,4 +64,32 @@ module Which
|
|
62
64
|
found
|
63
65
|
end
|
64
66
|
end
|
67
|
+
|
68
|
+
# Returns the default options for {#which}. The returned
|
69
|
+
# hash can be modified directly.
|
70
|
+
#
|
71
|
+
# == Examples
|
72
|
+
# Which.options #=> {}
|
73
|
+
# Which.options = { :all => true }
|
74
|
+
# Which.options #=> { :all => true }
|
75
|
+
# Which.options[:array] = true #=> { :all => true, :array => true }
|
76
|
+
def self.options
|
77
|
+
@@options
|
78
|
+
end
|
79
|
+
|
80
|
+
# Sets the default options for {#which}.
|
81
|
+
#
|
82
|
+
# == Examples
|
83
|
+
# Which.options = { :all => true, :array => true }
|
84
|
+
# Which.which('ls') #=> [ "/bin/ls" ]
|
85
|
+
# Which.which('svn') #=> [ "/opt/local/bin/svn", "/usr/bin/svn" ]
|
86
|
+
def self.options= options
|
87
|
+
raise ArgumentError, "Default options must be a hash, #{options.class.name} given." unless options.kind_of?(Hash)
|
88
|
+
@@options = options.dup
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
# Default options for {#which}.
|
94
|
+
@@options = Hash.new
|
65
95
|
end
|
data/spec/all_spec.rb
CHANGED
@@ -17,4 +17,8 @@ describe ':all option' do
|
|
17
17
|
it "should not find anything when given no command to find" do
|
18
18
|
Which.which({ :all => true }).should be_nil
|
19
19
|
end
|
20
|
+
|
21
|
+
it "should only find one executable file when given an absolute path" do
|
22
|
+
Which.which('/opt/local/bin/svn', :all => true).should == '/opt/local/bin/svn'
|
23
|
+
end
|
20
24
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe 'Default Options' do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
WhichSpecHelper.stub_unix!
|
7
|
+
Which.options.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should have no options by default" do
|
11
|
+
Which.options.should == {}
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should allow to set the :all option to true by default" do
|
15
|
+
Which.options = { :all => true }
|
16
|
+
Which.which('svn').should == [ '/opt/local/bin/svn', '/usr/bin/svn' ]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should allow to set the :array option to true by default" do
|
20
|
+
Which.options = { :array => true }
|
21
|
+
Which.which('ls').should == [ '/bin/ls' ]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should allow to set all options to true by default" do
|
25
|
+
Which.options = { :all => true, :array => true }
|
26
|
+
Which.which('ls').should == [ '/bin/ls' ]
|
27
|
+
Which.which('svn').should == [ '/opt/local/bin/svn', '/usr/bin/svn' ]
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should allow to clear default options" do
|
31
|
+
Which.options = { :all => true, :array => true }
|
32
|
+
Which.options = {}
|
33
|
+
Which.options.should == {}
|
34
|
+
Which.which('ls').should == '/bin/ls'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return the default options" do
|
38
|
+
Which.options = { :all => true, :array => false }
|
39
|
+
Which.options.should == { :all => true, :array => false }
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should copy the given options" do
|
43
|
+
options = { :all => true, :array => true }
|
44
|
+
Which.options = options
|
45
|
+
Which.options.should == options
|
46
|
+
Which.options.should_not equal(options)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return its internal default options hash" do
|
50
|
+
Which.options[:array] = true
|
51
|
+
Which.options[:all] = 42
|
52
|
+
Which.options[:array].should be_true
|
53
|
+
Which.options[:all].should == 42
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should allow to override the :all option" do
|
57
|
+
Which.options = { :all => true, :array => true }
|
58
|
+
Which.which('svn', :all => false).should == [ '/opt/local/bin/svn' ]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should allow to override the :array option" do
|
62
|
+
Which.options = { :all => true, :array => true }
|
63
|
+
Which.which('ls', :array => false).should == '/bin/ls'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should allow to override all options" do
|
67
|
+
Which.options = { :all => true, :array => true }
|
68
|
+
Which.which('ls', :all => false, :array => false).should == '/bin/ls'
|
69
|
+
Which.which('svn', :all => false, :array => false).should == '/opt/local/bin/svn'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should only accept a hash as default options" do
|
73
|
+
[ nil, false, true, String.new, Array.new, 2, 4.5 ].each do |invalid|
|
74
|
+
lambda{ Which.options = invalid }.should raise_error(ArgumentError)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/which_works.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "which_works"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["AlphaHydrae"]
|
12
|
-
s.date = "2012-03-
|
13
|
-
s.description = "
|
12
|
+
s.date = "2012-03-11"
|
13
|
+
s.description = "Locates a program file in the user's path. The which method takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked."
|
14
14
|
s.email = "hydrae.alpha@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
|
|
32
32
|
"spec/array_spec.rb",
|
33
33
|
"spec/basic_spec.rb",
|
34
34
|
"spec/helper.rb",
|
35
|
+
"spec/options_spec.rb",
|
35
36
|
"which_works.gemspec"
|
36
37
|
]
|
37
38
|
s.homepage = "http://github.com/AlphaHydrae/which_works"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: which_works
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-11 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70324806494120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.9.2
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70324806494120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70324806493580 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.8.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70324806493580
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: shoulda
|
38
|
-
requirement: &
|
38
|
+
requirement: &70324806493040 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.11.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70324806493040
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &70324806492460 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70324806492460
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: jeweler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70324806491920 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.8.3
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70324806491920
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
|
-
requirement: &
|
71
|
+
requirement: &70324806491400 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 0.5.4
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70324806491400
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: yard
|
82
|
-
requirement: &
|
82
|
+
requirement: &70324806490800 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: 0.7.5
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70324806490800
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rdiscount
|
93
|
-
requirement: &
|
93
|
+
requirement: &70324806490220 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ~>
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: 1.6.8
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70324806490220
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: travis-lint
|
104
|
-
requirement: &
|
104
|
+
requirement: &70324806489620 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ~>
|
@@ -109,8 +109,10 @@ dependencies:
|
|
109
109
|
version: 1.3.0
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
113
|
-
description:
|
112
|
+
version_requirements: *70324806489620
|
113
|
+
description: Locates a program file in the user's path. The which method takes a list
|
114
|
+
of command names and searches the path for each executable file that would be run
|
115
|
+
had these commands actually been invoked.
|
114
116
|
email: hydrae.alpha@gmail.com
|
115
117
|
executables: []
|
116
118
|
extensions: []
|
@@ -133,6 +135,7 @@ files:
|
|
133
135
|
- spec/array_spec.rb
|
134
136
|
- spec/basic_spec.rb
|
135
137
|
- spec/helper.rb
|
138
|
+
- spec/options_spec.rb
|
136
139
|
- which_works.gemspec
|
137
140
|
homepage: http://github.com/AlphaHydrae/which_works
|
138
141
|
licenses:
|
@@ -149,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
149
152
|
version: '0'
|
150
153
|
segments:
|
151
154
|
- 0
|
152
|
-
hash:
|
155
|
+
hash: 1669874747922136134
|
153
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
157
|
none: false
|
155
158
|
requirements:
|