lonelyelk-file_path 0.2
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.
- data/LICENSE +19 -0
- data/README.rdoc +19 -0
- data/Rakefile +22 -0
- data/lib/file_path.rb +92 -0
- data/spec/file_path_spec.rb +42 -0
- data/spec/spec.opts +2 -0
- metadata +66 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2009 Sergey Kruk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= FilePath
|
2
|
+
|
3
|
+
== Summary
|
4
|
+
|
5
|
+
Utility for comfortable operation with file paths
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
sudo gem install lonelyelk-file_path
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
require "rubygems"
|
14
|
+
require "file_path"
|
15
|
+
|
16
|
+
spec_dir = "spec"
|
17
|
+
spec_files = (FilePath.new / spec_dir / "*_spec.rb").list #=> an array
|
18
|
+
|
19
|
+
(FilePath.new("/some") / "dir" / "some" / "file").exist?
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
|
7
|
+
desc "Run all specs"
|
8
|
+
Spec::Rake::SpecTask.new do |t|
|
9
|
+
t.spec_files = Dir.glob(File.join("spec", "*_spec.rb"))
|
10
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Generate RDoc"
|
14
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
15
|
+
rdoc.rdoc_files.include(Dir.glob(File.join("lib", "*.rb"))).
|
16
|
+
include("README.rdoc", "LICENSE")
|
17
|
+
rdoc.main = "README.rdoc"
|
18
|
+
rdoc.rdoc_dir = "rdoc"
|
19
|
+
rdoc.options << "--charset=UTF-8"
|
20
|
+
rdoc.options << "--inline-source"
|
21
|
+
rdoc.title = "FilePath"
|
22
|
+
end
|
data/lib/file_path.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Class FilePath is used for comfortable work with file paths.
|
2
|
+
#
|
3
|
+
# Following File class methods are implemented:
|
4
|
+
# filepath.blockdev? => File.blockdev?(filepath)
|
5
|
+
# filepath.chardev? => File.chardev?(filepath)
|
6
|
+
# filepath.directory? => File.directory?(filepath)
|
7
|
+
# filepath.executable? => File.executable?(filepath)
|
8
|
+
# filepath.executable_real? => File.executable_real?(filepath)
|
9
|
+
# filepath.exist? => File.exist?(filepath)
|
10
|
+
# filepath.file? => File.file?(filepath)
|
11
|
+
# filepath.grpowned? => File.grpowned?(filepath)
|
12
|
+
# filepath.owned? => File.owned?(filepath)
|
13
|
+
# filepath.pipe? => File.pipe?(filepath)
|
14
|
+
# filepath.readable? => File.readable?(filepath)
|
15
|
+
# filepath.readable_real? => File.readable_real?(filepath)
|
16
|
+
# filepath.setgid? => File.setgid?(filepath)
|
17
|
+
# filepath.setuid? => File.setuid?(filepath)
|
18
|
+
# filepath.size? => File.size?(filepath)
|
19
|
+
# filepath.socket? => File.socket?(filepath)
|
20
|
+
# filepath.sticky? => File.sticky?(filepath)
|
21
|
+
# filepath.symlink? => File.symlink?(filepath)
|
22
|
+
# filepath.writable? => File.writable?(filepath)
|
23
|
+
# filepath.writable_real? => File.writable_real?(filepath)
|
24
|
+
# filepath.zero? => File.zero?(filepath)
|
25
|
+
class FilePath < String
|
26
|
+
# Creates new file path. If <code>path</code> param is not given, dirname of
|
27
|
+
# current file is used
|
28
|
+
def initialize(path = nil)
|
29
|
+
if path
|
30
|
+
replace expand(path)
|
31
|
+
else
|
32
|
+
replace expand(File.dirname(caller(1)[0].split(":")[0]))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Joins string or filepath into new path
|
37
|
+
# Example:
|
38
|
+
# FilePath.new("/somedir")/"other_dir"/"file" => "/somedir/other_dir/file"
|
39
|
+
def /(other)
|
40
|
+
self.class.new(expand(File.join(self, other)))
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns <code>true</code> if file path is a wildcard
|
44
|
+
def wildcard?
|
45
|
+
include?("*") || include?("?")
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns list of paths selected by wildcard or an array with one specified file
|
49
|
+
# if it exists
|
50
|
+
def list
|
51
|
+
Dir.glob(self).map{ |p| self.class.new(p)}
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns <code>true</code> if wildcard selection includes specified path
|
55
|
+
def include_path?(filepath)
|
56
|
+
list.include?(expand(filepath))
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns file path as in File.dirname but of FilePath type
|
60
|
+
def dirname
|
61
|
+
self.class.new(File.dirname(self))
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns string with basename like in File.basename
|
65
|
+
def basename(suffix = "")
|
66
|
+
File.basename(self, suffix)
|
67
|
+
end
|
68
|
+
|
69
|
+
[:blockdev?, :chardev?, :directory?, :executable?, :executable_real?, :exist?,
|
70
|
+
:file?, :grpowned?, :owned?, :pipe?, :readable?, :readable_real?, :setgid?,
|
71
|
+
:setuid?, :size?, :socket?, :sticky?, :symlink?, :writable?, :writable_real?,
|
72
|
+
:zero?].each do |mthd|
|
73
|
+
class_eval <<-RUBY
|
74
|
+
def #{mthd}
|
75
|
+
File.#{mthd}(self)
|
76
|
+
end
|
77
|
+
RUBY
|
78
|
+
end
|
79
|
+
|
80
|
+
class << self
|
81
|
+
# Path to current file: File.expand_path(__FILE__)
|
82
|
+
def current
|
83
|
+
new(caller(1)[0].split(":")[0])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
protected
|
88
|
+
|
89
|
+
def expand(path)
|
90
|
+
File.expand_path(path)
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "file_path.rb")
|
2
|
+
|
3
|
+
describe FilePath do
|
4
|
+
it "should show correct current path" do
|
5
|
+
FilePath.current.should == File.expand_path(__FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should show correct current dirname path when initialized" do
|
9
|
+
FilePath.new.should == File.expand_path(File.dirname(__FILE__))
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should call File class methods on path which return boolean" do
|
13
|
+
FilePath.new.directory?.should == true
|
14
|
+
FilePath.current.file?.should == true
|
15
|
+
FilePath.new("/some/dir").exist?.should == false
|
16
|
+
# Should I really be testing it?
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should recognize wildcard" do
|
20
|
+
(FilePath.new/"**"/"*.rb").wildcard?.should == true
|
21
|
+
(FilePath.new/"s?mefile.??").wildcard?.should == true
|
22
|
+
FilePath.current.wildcard?.should == false
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should work with lists" do
|
26
|
+
(FilePath.new/".."/"**"/"*.*").list.should == Dir.glob(File.join(File.dirname(__FILE__), "..", "**", "*.*")).map{ |p| FilePath.new(p)}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should correctly verify inclusion of filepath" do
|
30
|
+
(FilePath.new/"*.rb").include_path?(FilePath.current).should == true
|
31
|
+
(FilePath.new/"*.rb").include_path?(__FILE__).should == true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return dirname" do
|
35
|
+
FilePath.current.dirname.should == FilePath.new
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return basename" do
|
39
|
+
FilePath.current.basename.should == "file_path_spec.rb"
|
40
|
+
FilePath.current.basename(".rb").should == "file_path_spec"
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec.opts
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lonelyelk-file_path
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Kruk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-07 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: sergey.kruk@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- LICENSE
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- lib
|
30
|
+
- lib/file_path.rb
|
31
|
+
- spec
|
32
|
+
- spec/file_path_spec.rb
|
33
|
+
- spec/spec.opts
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/lonelyelk/file_path
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --main
|
39
|
+
- README.rdoc
|
40
|
+
- --charset
|
41
|
+
- UTF-8
|
42
|
+
- --webcvs
|
43
|
+
- http://github.com/lonelyelk/file_path/tree/master
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: FilePath is a ruby class to simplify file path interaction
|
65
|
+
test_files: []
|
66
|
+
|