path2 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +7 -0
  2. data/lib/path2.rb +89 -33
  3. data/lib/path2/version.rb +1 -1
  4. metadata +21 -10
data/README.md CHANGED
@@ -1 +1,8 @@
1
+ # Path2 [![TravisCI][travis-img]][travis-ci] [![Dependency Status][gemnasium-img]][gemnasium]
2
+
3
+ [travis-img]: https://secure.travis-ci.org/bry4n/path2.png?branch=master
4
+ [travis-ci]: http://travis-ci.org/bry4n/path2
5
+ [gemnasium-img]: https://gemnasium.com/bry4n/path2.png?travis
6
+ [gemnasium]: https://gemnasium.com/bry4n/path2
7
+
1
8
  Experimenting
@@ -1,13 +1,52 @@
1
- require 'thread'
1
+ require 'small/array'
2
2
 
3
3
  class Path
4
4
 
5
5
  def initialize(*args)
6
- @options = args.last.is_a?(Hash) ? args.pop : {}
7
- @options[:recursive] ||= false
8
- @options[:short] ||= false
9
- @root = args.first
10
- @entries = build_entries(@root)
6
+ @options = args.extract_options!
7
+ @options[:recursive] ||= false
8
+ @options[:expand_path] ||= false
9
+ @root = (args.shift || Dir.pwd)
10
+ @entries = tree
11
+ args.each &method(:<<)
12
+ end
13
+
14
+ def recursive!
15
+ @options[:recursive] = true
16
+ reload!
17
+ end
18
+
19
+ def expand_path!
20
+ @options[:expand_path] = true
21
+ reload!
22
+ end
23
+
24
+ def size
25
+ directory? ? entries.size : File.size(current)
26
+ end
27
+
28
+ def file?
29
+ !directory?
30
+ end
31
+
32
+ def stat
33
+ File.stat(current)
34
+ end
35
+
36
+ def directory?
37
+ File.directory?(current)
38
+ end
39
+
40
+ def dirname
41
+ directory? ? current : File.dirname(current)
42
+ end
43
+
44
+ def basename
45
+ File.basename(current)
46
+ end
47
+
48
+ def split
49
+ File.split(current)
11
50
  end
12
51
 
13
52
  def current
@@ -15,15 +54,23 @@ class Path
15
54
  end
16
55
 
17
56
  def exists?(path = nil)
18
- if path
19
- File.exists?([current, path].join("/"))
20
- else
21
- File.exists?(current)
22
- end
57
+ File.exists?((path ? [current, path].join("/") : current))
58
+ end
59
+
60
+ def reload!
61
+ @entries = tree
62
+ end
63
+
64
+ def reload
65
+ Path.new(@root, @options)
23
66
  end
24
67
 
25
68
  def find(arg)
26
- grep(/#{arg}/).first
69
+ grep(/#{arg}/)
70
+ end
71
+
72
+ def grep(pattern)
73
+ entries.grep(pattern)
27
74
  end
28
75
 
29
76
  def join(path)
@@ -31,44 +78,53 @@ class Path
31
78
  end
32
79
 
33
80
  def walk(path)
34
- yield Path.new([@root, path].join("/"), @options)
81
+ yield join(path)
35
82
  ensure
36
83
  self
37
84
  end
38
85
 
39
- def grep(pattern)
40
- entries.grep(pattern)
41
- end
42
-
43
86
  def entries
44
87
  @entries ||= []
45
88
  end
46
89
 
47
90
  def push(*paths)
48
- entries.concat(build_entries(*paths)).uniq!
91
+ paths.each do |path|
92
+ entries.concat(Path.new(path, @options).tree).uniq!
93
+ end
49
94
  end
95
+ alias :<< :push
50
96
 
51
- def pop(*args)
52
- args.each do |x|
53
- entries.delete_if {|y| File.expand_path(x) == y }
54
- end
97
+ def ignore(path)
98
+ reject(/#{path}/)
55
99
  end
56
100
 
57
- private
101
+ def reject(pattern)
102
+ @entries = (entries - grep(pattern))
103
+ self
104
+ end
58
105
 
59
- def dir(path)
60
- if @options[:recursive]
61
- Dir["#{path}/*"] + Dir["#{path}/**/*"]
62
- else
63
- Dir["#{path}/*"]
64
- end
106
+ def to_s
107
+ current
65
108
  end
66
109
 
67
- def build_entries(path)
68
- path = File.expand_path(path) if @options[:short]
69
- dir(path).uniq
110
+ def inspect
111
+ "#<Path @root=\"#{current}\" >"
70
112
  end
71
-
113
+
114
+ def tree(path = nil)
115
+ tree = []
116
+ Dir.foreach((path ||= (!@options[:expand_path] ? @root : dirname))) do |entry|
117
+ next if ['..','.'].include?(entry)
118
+ entry = File.join(path, entry)
119
+ if @options[:recursive] && File.directory?(entry)
120
+ tree << tree(entry)
121
+ else
122
+ tree << entry
123
+ end
124
+ end
125
+ tree.flatten
126
+ end
127
+
72
128
  end
73
129
 
74
130
  def Path(*args)
@@ -1,3 +1,3 @@
1
1
  class Path
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.3"
3
3
  end
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.2.1
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-08 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-05-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: small
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description:
15
31
  email: bryann83@gmail.com
16
32
  executables: []
@@ -33,22 +49,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
49
  - - ! '>='
34
50
  - !ruby/object:Gem::Version
35
51
  version: '0'
36
- segments:
37
- - 0
38
- hash: 4230538922531212955
39
52
  required_rubygems_version: !ruby/object:Gem::Requirement
40
53
  none: false
41
54
  requirements:
42
55
  - - ! '>='
43
56
  - !ruby/object:Gem::Version
44
57
  version: '0'
45
- segments:
46
- - 0
47
- hash: 4230538922531212955
48
58
  requirements: []
49
59
  rubyforge_project:
50
- rubygems_version: 1.8.11
60
+ rubygems_version: 1.8.23
51
61
  signing_key:
52
62
  specification_version: 3
53
63
  summary: ! '...'
54
64
  test_files: []
65
+ has_rdoc: