search_path 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ lib/**/*.rb
2
+ README.rdoc
3
+ ChangeLog.rdoc
4
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ html/
3
+ pkg/
4
+ vendor/cache/*.gem
5
+ /.idea
6
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format Fuubar
3
+
data/ChangeLog.rdoc ADDED
@@ -0,0 +1,4 @@
1
+ === 0.1.0 / 2012-08-19
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Thomas Baustert
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,71 @@
1
+ ## SearchPath
2
+
3
+ Allows to define search paths to find files in.
4
+
5
+ Useful for example to configure a few template directories to find a template in.
6
+
7
+ ## Install via Bundler
8
+
9
+ Add to Gemfile:
10
+
11
+ # Gemfile
12
+ gem "search_path"
13
+
14
+ And run bundler:
15
+
16
+ $ bundle install
17
+
18
+ ## Install without Bundler
19
+
20
+ $ gem install search_path
21
+
22
+ ## Usage
23
+
24
+ ### Example with find
25
+
26
+ # Define the search paths. Search paths are considered in the order given, first given first searched.
27
+ search_path = SearchPath.new(["/custom/path/to/files", "/standard/path/to/files"])
28
+
29
+ # Case 1: File not in "/custom/path/to/files" but in "/standard/path/to/files":
30
+ search_path.find("template.erb") # => "/standard/path/to/files/template.erb"
31
+
32
+ # Case 2: File in "/custom/path/to/files" and in "/standard/path/to/files":
33
+ search_path.find("template.erb") # => "/custom/path/to/files/template.erb"
34
+
35
+ # Case 3: File not in "/custom/path/to/files" and not in "/standard/path/to/files":
36
+ search_path.find("template.erb") # => nil
37
+
38
+ ### Example with find!
39
+
40
+ Same as `find` but raises `SearchPath::FileNotFoundError` if file cannot be found:
41
+
42
+ search_path.find!("template.erb") # => "/custom/path/to/files/template.erb"
43
+ search_path.find!("not_exists.erb") # => SearchPath::FileNotFoundError
44
+
45
+ ### Verify that search paths exists
46
+
47
+ In case you wanna make sure the given search paths exists use option `verify_paths`:
48
+
49
+ # Raises SearchPath::SearchPathNotExistError if "/path1/to/files" or "/path2/to/files" not exist.
50
+ search_path = SearchPath.new(["/path1/to/files", "/path2/to/files"], :verify_paths => true)
51
+
52
+ ### Search paths must be absolute
53
+
54
+ Note that each search path must be an absolute path. Using a relative path will raise an error:
55
+
56
+ # This Raises SearchPath::SearchPathNotAbsoluteError:
57
+ search_path = SearchPath.new("relative/path/to/files")
58
+
59
+ Because you want to avoid using hardcoded absolute paths in your code you define the paths
60
+ relative to search_path initialization code for example:
61
+
62
+ search_dir = File.expand("../../relative/path/to/files", __FILE__)
63
+ search_path = SearchPath.new([search_dir])
64
+
65
+ ## Contact
66
+
67
+ For comments and question feel free to contact me: business@thomasbaustert.de
68
+
69
+ Copyright © 2012 [Thomas Baustert](http://thomasbaustert.de), released under the MIT license
70
+
71
+
data/Rakefile ADDED
@@ -0,0 +1,38 @@
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 'rdoc/task'
24
+ RDoc::Task.new do |rdoc|
25
+ rdoc.title = "search_path"
26
+ end
27
+ task :doc => :rdoc
28
+
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new
31
+
32
+ task :test => :spec
33
+ task :default => :spec
34
+
35
+ require 'rubygems/tasks'
36
+ Gem::Tasks.new
37
+
38
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ class SearchPath
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,122 @@
1
+ require 'search_path/version'
2
+
3
+ class SearchPath
4
+
5
+ # Raised if search path is not absolute.
6
+ class SearchPathNotAbsoluteError < StandardError; end
7
+
8
+ # Raised if one or more given search paths does not exist.
9
+ class SearchPathNotExistError < StandardError; end
10
+
11
+ # Raised if given file was not found in the given search paths.
12
+ class FileNotFoundError < StandardError; end
13
+
14
+
15
+ attr_reader :search_paths
16
+
17
+ ##
18
+ # Initialize a new +SearchPath+ instance with the given search paths.
19
+ #
20
+ # == Parameters
21
+ #
22
+ # * +search_paths+ - The search paths to find a filename in.
23
+ #
24
+ # == Options
25
+ #
26
+ # * +:verify_paths+ - If +true+ verify the existence of each search path and raise an +SearchPathNotExistError+ exception if path not exists.
27
+ #
28
+ # ==== Examples
29
+ #
30
+ # search_path = SearchPath.new("/path1/to/files")
31
+ # search_path = SearchPath.new(["/path1/to/files", "/path2/to/files"])
32
+ #
33
+ # # => raises SearchPathNotAbsoluteError
34
+ # search_path = SearchPath.new(["a/relative/path"])
35
+ #
36
+ # # => raises SearchPathNotExistError for "/not/existing/path"
37
+ # search_path = SearchPath.new(["/existing/path", "/not/existing/path"], :verify_paths => true)
38
+ #
39
+ def initialize(search_paths, options = {})
40
+ @search_paths = Array(search_paths)
41
+
42
+ verify_search_paths_are_absolute!
43
+ verify_search_paths_exists! if options[:verify_paths] == true
44
+ end
45
+
46
+ ##
47
+ # Finds the given +filename+ in the search paths. Returns the full path to the file
48
+ # or +nil+ if the file could not be found in one of the search paths.
49
+ #
50
+ # == Parameters
51
+ #
52
+ # * +filename+ - The filename to find.
53
+ #
54
+ # ==== Examples
55
+ #
56
+ # search_path = SearchPath.new(["/path1/to/files", "/path2/to/files"])
57
+ #
58
+ # # File "searched_file.txt" exists in "/path1/to/files"
59
+ # search_path.find("searched_file.txt") # => "/path1/to/files/searched_file.txt"
60
+ #
61
+ # # File "searched_file.txt" exists in "/path2/to/files"
62
+ # search_path.find("searched_file.txt") # => "/path2/to/files/searched_file.txt"
63
+ #
64
+ # # File "searched_file.txt" exists in none
65
+ # search_path.find("searched_file.txt") # => nil
66
+ #
67
+ def find(filename)
68
+ search_paths.each do |path|
69
+ if File.exists?("#{path}/#{filename}")
70
+ return "#{path}/#{filename}"
71
+ end
72
+ end
73
+
74
+ nil
75
+ end
76
+
77
+ ##
78
+ # Same as #find but raises +FileNotFoundError+ exception if the file could not be found.
79
+ #
80
+ def find!(filename)
81
+ find(filename) || raise_file_not_found!(filename)
82
+ end
83
+
84
+ private
85
+
86
+ def verify_search_paths_are_absolute!
87
+ not_absolute_paths = search_paths.inject([]) do |list, path|
88
+ list << path unless Pathname.new(path).absolute?
89
+ list
90
+ end
91
+
92
+ raise raise_search_path_not_absolute!(not_absolute_paths) unless not_absolute_paths.empty?
93
+ end
94
+
95
+ def verify_search_paths_exists!
96
+ not_existing_paths = search_paths.inject([]) do |list, path|
97
+ list << path unless existing_directory?(path)
98
+ list
99
+ end
100
+
101
+ raise raise_search_path_not_exists!(not_existing_paths) unless not_existing_paths.empty?
102
+ end
103
+
104
+ def existing_directory?(path)
105
+ File.exists?(path) && File.directory?(path)
106
+ end
107
+
108
+ def raise_search_path_not_absolute!(paths)
109
+ raise SearchPathNotAbsoluteError.new("Search path(s) #{paths.inspect} is not absolute.")
110
+ end
111
+
112
+ def raise_search_path_not_exists!(paths)
113
+ raise SearchPathNotExistError.new("Search path(s) #{paths.inspect} does not exists.")
114
+ end
115
+
116
+ def raise_file_not_found!(filename)
117
+ raise FileNotFoundError.new("File '#{filename}' not found in search paths #{search_paths.inspect}.")
118
+ end
119
+
120
+ end
121
+
122
+
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/search_path/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "search_path"
7
+ gem.version = SearchPath::VERSION
8
+ gem.summary = %q{Allows to define search paths to find files in.}
9
+ gem.description = %q{Allows to define search paths to find files in.}
10
+ gem.license = "MIT"
11
+ gem.authors = ["Thomas Baustert"]
12
+ gem.email = "business@thomasbaustert.de"
13
+ gem.homepage = "https://github.com/thomasbaustert/search_path"
14
+
15
+ gem.files = `git ls-files`.split($/)
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 'fakefs'
21
+ gem.add_development_dependency 'rubygems-tasks'
22
+ gem.add_development_dependency 'bundler'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rdoc'
25
+ gem.add_development_dependency 'rspec'
26
+ gem.add_development_dependency 'fuubar'
27
+ end
28
+
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe SearchPath do
4
+
5
+ describe "#initialize" do
6
+
7
+ context "given relative path" do
8
+ it "raises exception" do
9
+ FakeFS do
10
+ create_dir("/absolute/path/to/")
11
+ Dir.chdir("/absolute/path/to") do
12
+ lambda {
13
+ SearchPath.new(["a/not/existing/relative/path"])
14
+ }.should raise_error(SearchPath::SearchPathNotAbsoluteError, "Search path(s) [\"a/not/existing/relative/path\"] is not absolute.")
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "verification existence of search paths" do
21
+
22
+ context "given not existing search path" do
23
+ context "as absolute path" do
24
+ it "raises exception if path not exists" do
25
+ lambda {
26
+ SearchPath.new(["/not/existing/path"], :verify_paths => true)
27
+ }.should raise_error(SearchPath::SearchPathNotExistError, "Search path(s) [\"/not/existing/path\"] does not exists.")
28
+ end
29
+ end
30
+
31
+ it "raises exception if one path not exists" do
32
+ FakeFS do
33
+ create_dir("/existing/path")
34
+ lambda {
35
+ SearchPath.new(["/existing/path", "/not/existing/path"], :verify_paths => true)
36
+ }.should raise_error(SearchPath::SearchPathNotExistError, "Search path(s) [\"/not/existing/path\"] does not exists.")
37
+ end
38
+ end
39
+
40
+ it "raises exception if path is not a directory" do
41
+ FakeFS do
42
+ create_filename("/existing/path/to/file.txt")
43
+ lambda {
44
+ SearchPath.new(["/existing/path/to/file.txt"], :verify_paths => true)
45
+ }.should raise_error(SearchPath::SearchPathNotExistError, "Search path(s) [\"/existing/path/to/file.txt\"] does not exists.")
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#search_paths" do
53
+ context "given search paths is nil" do
54
+ it "converts string to empty array" do
55
+ search_path = SearchPath.new(nil)
56
+ search_path.search_paths.should eq []
57
+ end
58
+ end
59
+
60
+ context "given search paths is as string" do
61
+ it "converts string to array with one element" do
62
+ search_path = SearchPath.new("/path/to/files")
63
+ search_path.search_paths.should eq ["/path/to/files"]
64
+ end
65
+ end
66
+
67
+ context "given search paths is an array of strings" do
68
+ it "returns array as given" do
69
+ search_path = SearchPath.new(["/path1/to/files", "/path2/to/more/files"])
70
+ search_path.search_paths.should eq ["/path1/to/files", "/path2/to/more/files"]
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "#find" do
76
+ context "empty search path given" do
77
+ it "should return nil" do
78
+ SearchPath.new(nil).find("searched_file.txt").should be_nil
79
+ SearchPath.new([]).find("searched_file.txt").should be_nil
80
+ end
81
+ end
82
+
83
+ context "search path given" do
84
+ context "file not exists in search path" do
85
+ it "should return nil" do
86
+ FakeFS do
87
+ search_path = SearchPath.new(["/not/existing/path"])
88
+ search_path.find("searched_file.txt").should be_nil
89
+ end
90
+ end
91
+ end
92
+
93
+ context "file exists in search path" do
94
+ it "should return full path to file" do
95
+ FakeFS do
96
+ create_filename("/path/to/files/searched_file.txt")
97
+ search_path = SearchPath.new(["/path/to/files"])
98
+ search_path.find("searched_file.txt").should eq "/path/to/files/searched_file.txt"
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "#find!" do
106
+ context "empty search path given" do
107
+ it "should raise exception" do
108
+ lambda { SearchPath.new(nil).find!("searched_file.txt") }.should(
109
+ raise_error(SearchPath::FileNotFoundError, "File 'searched_file.txt' not found in search paths []."))
110
+ lambda { SearchPath.new([]).find!("searched_file.txt") }.should raise_error(SearchPath::FileNotFoundError)
111
+ end
112
+ end
113
+
114
+ context "search path given" do
115
+ context "file not exists in search path" do
116
+ it "should raise exception" do
117
+ FakeFS do
118
+ search_path = SearchPath.new(["/not/existing/path"])
119
+ lambda { search_path.find!("searched_file.txt") }.should raise_error(SearchPath::FileNotFoundError)
120
+ end
121
+ end
122
+ end
123
+
124
+ context "file exists in search path" do
125
+ it "should return full path to file" do
126
+ FakeFS do
127
+ create_filename("/path/to/files/searched_file.txt")
128
+ search_path = SearchPath.new(["/path/to/files"])
129
+ search_path.find!("searched_file.txt").should eq "/path/to/files/searched_file.txt"
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ private
137
+
138
+ def create_filename(filename, content = "RESULT")
139
+ create_dir(File.dirname(filename))
140
+ File.open(filename, "w") { |file| file.write(content) }
141
+ end
142
+
143
+ def create_dir(dirname)
144
+ FileUtils.mkdir_p(dirname)
145
+ end
146
+
147
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require 'search_path'
3
+ require 'fakefs/spec_helpers'
4
+
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_path
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Baustert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fakefs
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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: rubygems-tasks
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: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
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: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rdoc
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
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: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: fuubar
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Allows to define search paths to find files in.
127
+ email: business@thomasbaustert.de
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .document
133
+ - .gitignore
134
+ - .rspec
135
+ - ChangeLog.rdoc
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - lib/search_path.rb
141
+ - lib/search_path/version.rb
142
+ - search_path.gemspec
143
+ - spec/search_path/search_path_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: https://github.com/thomasbaustert/search_path
146
+ licenses:
147
+ - MIT
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ segments:
159
+ - 0
160
+ hash: 4409282025559043119
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ segments:
168
+ - 0
169
+ hash: 4409282025559043119
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 1.8.24
173
+ signing_key:
174
+ specification_version: 3
175
+ summary: Allows to define search paths to find files in.
176
+ test_files:
177
+ - spec/search_path/search_path_spec.rb
178
+ - spec/spec_helper.rb