path2 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/path2.rb +15 -29
  2. metadata +8 -2
data/lib/path2.rb CHANGED
@@ -3,17 +3,17 @@ require 'thread'
3
3
  class Path
4
4
 
5
5
  def initialize(*args)
6
- @id = generate_id
7
- @self = "path2_#{@id}"
8
- @semaphore = Mutex.new
6
+ @options = args.last.is_a?(Hash) ? args.pop : {}
7
+ @options[:recursive] ||= false
9
8
  @entries = build_entries(*args)
10
- Thread.current[@self] = {}
11
9
  end
12
10
 
13
11
  def find(arg)
14
- cache("find_#{arg}") do
15
- grep(/#{arg}/)
16
- end
12
+ grep(/#{arg}/).first
13
+ end
14
+
15
+ def join(path)
16
+ Path.new(find(path))
17
17
  end
18
18
 
19
19
  def grep(pattern)
@@ -25,43 +25,29 @@ class Path
25
25
  end
26
26
 
27
27
  def push(*paths)
28
- @entries.concat(build_entries(*paths)).uniq!
28
+ entries.concat(build_entries(*paths)).uniq!
29
29
  end
30
30
 
31
31
  def pop(*args)
32
32
  args.each do |x|
33
- @entries.delete_if {|y| File.expand_path(x) == y }
33
+ entries.delete_if {|y| File.expand_path(x) == y }
34
34
  end
35
35
  end
36
36
 
37
37
  private
38
38
 
39
39
  def dir(path)
40
- Dir["#{path}/**/*"]
40
+ if @options[:recursive]
41
+ Dir["#{path}/*"] + Dir["#{path}/**/*"]
42
+ else
43
+ Dir["#{path}/*"]
44
+ end
41
45
  end
42
46
 
43
47
  def build_entries(*paths)
44
- @semaphore.synchronize {
45
- paths.map{|path| dir(File.expand_path(path)) }.flatten.uniq
46
- }
48
+ paths.map{|path| dir(File.expand_path(path)) }.flatten.uniq
47
49
  end
48
50
 
49
- def cache(name, &block)
50
- _thread(name) || _thread(name, &block)
51
- end
52
-
53
- def _thread(name, &block)
54
- if block_given?
55
- Thread.current[@self][name] = block.call
56
- else
57
- Thread.current[@self][name]
58
- end
59
- end
60
-
61
- def generate_id
62
- Time.now.to_i
63
- end
64
-
65
51
  end
66
52
 
67
53
  def Path(*args)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-06 00:00:00.000000000 Z
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: bryann83@gmail.com
@@ -32,12 +32,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
32
32
  - - ! '>='
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
+ segments:
36
+ - 0
37
+ hash: -1513887647798597346
35
38
  required_rubygems_version: !ruby/object:Gem::Requirement
36
39
  none: false
37
40
  requirements:
38
41
  - - ! '>='
39
42
  - !ruby/object:Gem::Version
40
43
  version: '0'
44
+ segments:
45
+ - 0
46
+ hash: -1513887647798597346
41
47
  requirements: []
42
48
  rubyforge_project:
43
49
  rubygems_version: 1.8.11