hike 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ module Hike
2
+ VERSION = "0.1.0"
3
+
4
+ autoload :Extensions, "hike/extensions"
5
+ autoload :NormalizedArray, "hike/normalized_array"
6
+ autoload :Paths, "hike/paths"
7
+ autoload :Trail, "hike/trail"
8
+ end
@@ -0,0 +1,11 @@
1
+ module Hike
2
+ class Extensions < NormalizedArray
3
+ def normalize_element(extension)
4
+ if extension[/^\./]
5
+ extension
6
+ else
7
+ ".#{extension}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,54 @@
1
+ module Hike
2
+ class NormalizedArray < Array
3
+ def initialize
4
+ super()
5
+ end
6
+
7
+ def []=(*args)
8
+ value = args.pop
9
+
10
+ if value.respond_to?(:to_ary)
11
+ value = normalize_elements(value)
12
+ else
13
+ value = normalize_element(value)
14
+ end
15
+
16
+ super *args.concat([value])
17
+ end
18
+
19
+ def <<(element)
20
+ super normalize_element(element)
21
+ end
22
+
23
+ def collect!
24
+ super do |element|
25
+ result = yield element
26
+ normalize_element(result)
27
+ end
28
+ end
29
+
30
+ alias_method :map!, :collect!
31
+
32
+ def insert(index, *elements)
33
+ super index, *normalize_elements(elements)
34
+ end
35
+
36
+ def push(*elements)
37
+ super *normalize_elements(elements)
38
+ end
39
+
40
+ def replace(elements)
41
+ super normalize_elements(elements)
42
+ end
43
+
44
+ def unshift(*elements)
45
+ super *normalize_elements(elements)
46
+ end
47
+
48
+ def normalize_elements(elements)
49
+ elements.map do |element|
50
+ normalize_element(element)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,13 @@
1
+ module Hike
2
+ class Paths < NormalizedArray
3
+ def initialize(root = ".")
4
+ @root = root
5
+ super()
6
+ end
7
+
8
+ def normalize_element(path)
9
+ path = File.join(@root, path) unless path[/^\//]
10
+ File.expand_path(path)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,39 @@
1
+ module Hike
2
+ class Trail
3
+ attr_reader :root, :paths, :extensions
4
+
5
+ def initialize(root)
6
+ @root = File.expand_path(root)
7
+ @paths = Paths.new(@root)
8
+ @extensions = Extensions.new
9
+ end
10
+
11
+ def find(logical_path)
12
+ candidates = candidates_for(logical_path)
13
+
14
+ paths.each do |path|
15
+ candidates.each do |candidate|
16
+ filename = File.join(path, candidate)
17
+ return filename if exists?(filename)
18
+ end
19
+ end
20
+
21
+ nil
22
+ end
23
+
24
+ protected
25
+ def candidates_for(logical_path)
26
+ candidates = extensions.map { |ext| logical_path + ext }
27
+ candidates.unshift(logical_path) if has_extension?(logical_path)
28
+ candidates
29
+ end
30
+
31
+ def has_extension?(logical_path)
32
+ extensions.include?(File.extname(logical_path))
33
+ end
34
+
35
+ def exists?(path)
36
+ File.exists?(path)
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hike
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Sam Stephenson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-16 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A Ruby library for finding files in a set of paths.
23
+ email:
24
+ - sstephenson@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/hike/extensions.rb
33
+ - lib/hike/normalized_array.rb
34
+ - lib/hike/paths.rb
35
+ - lib/hike/trail.rb
36
+ - lib/hike.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/sstephenson/hike
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Find files in a set of paths
71
+ test_files: []
72
+