pa 1.0.3 → 1.1.3

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.
@@ -1,64 +1,103 @@
1
1
  class Pa
2
- module ClassMethods::State
3
- # @see File.chmod
4
- def chmod(mode, *paths) paths.map!{|v|get(v)}; File.chmod(mode, *paths) end
2
+ module State
3
+ extend Util::Concern
4
+ module ClassMethods
5
+ # goes to File
6
+ [ :exists?, :atime, :ctime, :mtime, :stat, :lstat, :size, :zero?, :executable?, :executable_real?, :world_executable?, :readable?, :readable_real?, :world_readalbe?, :writeable?, :writeable_real?, :world_writeable?, :directory?, :file?, :blockdev?, :chardev?, :piple?, :socket?, :symlink?, :dangling?, :owned?, :grpowned?, :setgid?, :setuid?, :stricky?, :identical? ].each do |name|
7
+ define_method(name) { |*args|
8
+ File.__send__ name, *args
9
+ }
10
+ end
5
11
 
6
- # @see File.lchmod
7
- def lchmod(mode, *paths) paths.map!{|v|get(v)}; File.lchmod(mode, *paths) end
12
+ # @see File.chmod
13
+ def chmod(mode, *paths)
14
+ paths.map!{|v|get(v)}
15
+ File.chmod(mode, *paths)
16
+ end
8
17
 
9
- # @see File.chown
10
- def chown(user, group=nil, *paths) paths.map!{|v|get(v)}; File.chown(user, group, *paths) end
18
+ # @see File.lchmod
19
+ def lchmod(mode, *paths)
20
+ paths.map!{|v|get(v)}
21
+ File.lchmod(mode, *paths)
22
+ end
11
23
 
12
- # @see File.lchown
13
- def lchown(user, group=nil, *paths) paths.map!{|v|get(v)}; File.lchown(user, group, *paths) end
24
+ # @see File.chown
25
+ def chown(user, group=nil, *paths)
26
+ paths.map!{|v|get(v)}
27
+ File.chown(user, group, *paths)
28
+ end
14
29
 
15
- # @see File.utime
16
- def utime(atime, mtime, *paths) paths.map!{|v|get(v)}; File.utime(atime, mtime, *paths) end
30
+ # @see File.lchown
31
+ def lchown(user, group=nil, *paths)
32
+ paths.map!{|v|get(v)}
33
+ File.lchown(user, group, *paths)
34
+ end
17
35
 
18
- # get file type
19
- #
20
- # file types:
21
- # "chardev" "blockdev" "symlink" ..
22
- #
23
- # @param [String] path
24
- # @return [String]
25
- def type(path)
26
- case (t=File.ftype(get(path)))
27
- when "characterSpecial"
28
- "chardev"
29
- when "blockSpecial"
30
- "blockdev"
31
- when "link"
32
- "symlink"
33
- else
34
- t
35
- end
36
- end # def type
36
+ # @see File.utime
37
+ def utime(atime, mtime, *paths)
38
+ paths.map!{|v|get(v)}
39
+ File.utime(atime, mtime, *paths)
40
+ end
37
41
 
38
- # is path a mountpoint?
39
- #
40
- # @param[String] path
41
- # @return [Boolean]
42
- def mountpoint? path
43
- path=get(path)
44
- begin
45
- stat1 = File.lstat(path)
46
- stat2 = File.lstat(File.join(path, '..'))
47
- stat1.dev == stat2.dev && stat1.ino == stat2.ino || stat1.dev != stat2.dev
48
- rescue Errno::ENOENT
49
- false
50
- end
51
- end
52
- end
42
+ # get file type
43
+ #
44
+ # file types:
45
+ # "chardev" "blockdev" "symlink" ..
46
+ #
47
+ # @param [String] path
48
+ # @return [String]
49
+ def type(path)
50
+ case (t=File.ftype(get(path)))
51
+ when "characterSpecial"
52
+ "chardev"
53
+ when "blockSpecial"
54
+ "blockdev"
55
+ when "link"
56
+ "symlink"
57
+ else
58
+ t
59
+ end
60
+ end # def type
53
61
 
54
- module State
55
- def chmod(mode); File.chmod(mode, path) end
56
- def lchmod(mode); File.lchmod(mode, path) end
57
- def chown(uid, gid=nil); File.chown(uid, gid, path) end
58
- def lchown(uid, gid=nil); File.lchown(uid, gid, path) end
59
- def utime(atime, mtime); File.utime(atime, mtime, path) end
60
- end
61
62
 
63
+ # is path a mountpoint?
64
+ #
65
+ # @param[String] path
66
+ # @return [Boolean]
67
+ def mountpoint?(path)
68
+ path=get(path)
69
+ begin
70
+ stat1 = File.lstat(path)
71
+ stat2 = File.lstat(File.join(path, '..'))
72
+ stat1.dev == stat2.dev && stat1.ino == stat2.ino || stat1.dev != stat2.dev
73
+ rescue Errno::ENOENT
74
+ false
75
+ end
76
+ end
77
+ end
78
+
79
+ module InstanceMethods
80
+ def chmod(mode)
81
+ File.chmod(mode, path)
82
+ end
83
+
84
+ def lchmod(mode)
85
+ File.lchmod(mode, path)
86
+ end
87
+
88
+ def chown(uid, gid=nil)
89
+ File.chown(uid, gid, path)
90
+ end
91
+
92
+ def lchown(uid, gid=nil)
93
+ File.lchown(uid, gid, path)
94
+ end
95
+
96
+ def utime(atime, mtime)
97
+ File.utime(atime, mtime, path)
98
+ end
99
+ end
100
+ end
62
101
  end
63
102
 
64
103
 
@@ -0,0 +1,32 @@
1
+ class Pa
2
+ class Util
3
+ module Concern
4
+ def included(base)
5
+ base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
6
+ base.send :include, const_get(:InstanceMethods) if const_defined?(:InstanceMethods)
7
+ end
8
+ end
9
+
10
+ class << self
11
+ # extract options
12
+ # @see extract_options!
13
+ # @example
14
+ # def mkdir(*args)
15
+ # paths, o = args.extract_options
16
+ # end
17
+ #
18
+ # @return [Array<Array,Hash>]
19
+ def extract_options(ary, default={})
20
+ if ary.last.is_a?(Hash) && ary.last.instance_of?(Hash)
21
+ [ary[0...-1], ary[-1].merge(default)]
22
+ else
23
+ [ary, default]
24
+ end
25
+ end
26
+
27
+ def wrap_array(value)
28
+ Array === value ? value : [value]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ class Pa
2
+ VERSION = "1.1.3"
3
+ end
data/pa.gemspec CHANGED
@@ -1,9 +1,9 @@
1
- $: << "."
2
- require "version"
1
+ $: << File.expand_path("../lib", __FILE__)
2
+ require "pa/version"
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "pa"
6
- s.version = Pa::VERSION::IS
6
+ s.version = Pa::VERSION
7
7
  s.summary = "a path library for Ruby"
8
8
  s.description = <<-EOF
9
9
  a path library for Ruby
@@ -15,6 +15,4 @@ a path library for Ruby
15
15
  s.rubyforge_project = "xx"
16
16
 
17
17
  s.files = `git ls-files`.split("\n")
18
-
19
- s.add_dependency "tagen", "~>1.0.0"
20
18
  end
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
  require "fileutils"
3
3
  require "tmpdir"
4
4
 
5
- module Pa::ClassMethods::Cmd
5
+ module Pa::Cmd::ClassMethods
6
6
  public :_copy, :_touch, :_mkdir, :_mktmpname, :_rmdir, :_copy, :_move
7
7
  end
8
8
 
@@ -20,7 +20,7 @@ describe Pa do
20
20
  FileUtils.rm_r @tmpdir
21
21
  end
22
22
 
23
- describe "#glob" do
23
+ describe "#glob2" do
24
24
  before(:each) do
25
25
  @files = %w(fa .fa)
26
26
  FileUtils.touch(@files)
@@ -31,18 +31,18 @@ describe Pa do
31
31
 
32
32
  context "call without any option" do
33
33
  it "returns 1 items" do
34
- Pa.glob("*").should have(1).items
34
+ Pa.glob2("*").should have(1).items
35
35
  end
36
36
  end
37
37
 
38
38
  context "call with :dotmatch option" do
39
39
  it "returns 2 items" do
40
- Pa.glob("*", dotmatch: true).should have(2).items
40
+ Pa.glob2("*", dotmatch: true).should have(2).items
41
41
  end
42
42
  end
43
43
  end
44
44
 
45
- describe "#each" do
45
+ describe "#each2" do
46
46
  # fa .fa fa~
47
47
  # dira/
48
48
  # dirb/
@@ -60,33 +60,33 @@ describe Pa do
60
60
 
61
61
  it "runs on" do
62
62
  ret = []
63
- Pa.each{|pa| ret << pa.fname}
63
+ Pa.each2{|pa| ret << pa}
64
64
  ret.sort.should == %w(.fa dira fa fa~)
65
65
  end
66
66
 
67
67
  it "return a Enumerator when call without block" do
68
- Pa.each.should be_an_instance_of Enumerator
68
+ Pa.each2.should be_an_instance_of Enumerator
69
69
  end
70
70
 
71
71
  it "raise Errno::ENOENT if path doesn't exists" do
72
- lambda { Pa.each("path_doesn't_exits"){} }.should raise_error(Errno::ENOENT)
72
+ lambda { Pa.each2("path_doesn't_exits"){} }.should raise_error(Errno::ENOENT)
73
73
  end
74
74
 
75
75
  it "raise Errno::ENOTDIDR if path isn't a directory" do
76
- lambda { Pa.each("fa"){} }.should raise_error(Errno::ENOTDIR)
76
+ lambda { Pa.each2("fa"){} }.should raise_error(Errno::ENOTDIR)
77
77
  end
78
78
 
79
- it "each(.) return 'foo' not '.foo'" do
80
- Pa.each.with_object([]){|pa,m| m<<pa.p}.sort.should == %w(.fa dira fa fa~)
79
+ it "each2(.) return 'foo' not '.foo'" do
80
+ Pa.each2.with_object([]){|pa,m| m<<pa}.sort.should == %w(.fa dira fa fa~)
81
81
  end
82
82
 
83
- it "each(nodot: true) -> list all files except dot file" do
84
- Pa.each(nodot: true).with_object([]){|pa,m|m<<pa.b}.sort.should == %w(dira fa fa~)
83
+ it "each2(nodot: true) -> list all files except dot file" do
84
+ Pa.each2(nodot: true).with_object([]){|pa,m|m<<pa}.sort.should == %w(dira fa fa~)
85
85
  end
86
86
 
87
87
  end
88
88
 
89
- describe "#each_r" do
89
+ describe "#each2_r" do
90
90
  # fa .fa fa~
91
91
  # dira/
92
92
  # dirb/
@@ -102,14 +102,14 @@ describe Pa do
102
102
  FileUtils.rm_r @dirs
103
103
  end
104
104
 
105
- it "each_r -> Enumerator" do
106
- Pa.each_r.should be_an_instance_of Enumerator
107
- Pa.each_r.with_object([]){|(pa,r),m|m<<r}.sort.should == %w(.fa dira dira/dirb dira/dirb/b fa fa~)
105
+ it "each2_r -> Enumerator" do
106
+ Pa.each2_r.should be_an_instance_of Enumerator
107
+ Pa.each2_r.with_object([]){|(pa,r),m|m<<r}.sort.should == %w(.fa dira dira/dirb dira/dirb/b fa fa~)
108
108
  end
109
109
  end
110
110
 
111
111
 
112
- describe "#ls" do
112
+ describe "#ls2" do
113
113
  # filea
114
114
  # dira/
115
115
  # fileb
@@ -125,13 +125,11 @@ describe Pa do
125
125
  end
126
126
 
127
127
  it "runs ok -> Array" do
128
- Pa.ls.should == ["filea", "dira"]
128
+ Pa.ls2.should == ["filea", "dira"]
129
129
  end
130
130
 
131
131
  it "call a block" do
132
- Pa.ls { |pa, fname| pa.directory? }.should == ["dira"]
132
+ Pa.ls2 { |pa, fname| File.directory?(pa) }.should == ["dira"]
133
133
  end
134
134
  end
135
-
136
-
137
135
  end
@@ -1,39 +1,26 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Pa do
4
- describe "#test" do
5
- it "runs ok" do
6
- pa = Pa('/home/guten/')
7
- end
8
- end
9
-
10
- describe "#dir2" do
11
- it "runs ok" do
12
- Pa('/home/foo').dir2.should be_an_instance_of Pa
13
- end
14
- end
4
+ context "dir and dir2" do
5
+ it "#dir => Pa" do
6
+ Pa.dir("/home/guten").should be_an_instance_of Pa
7
+ end
15
8
 
16
- describe "#absoulte2" do
17
- it "runs ok" do
18
- Pa('.').absolute2.should be_an_instance_of Pa
19
- end
20
- end
9
+ it "#dir2 => Pa" do
10
+ Pa.dir2("/home/guten").should be_an_instance_of String
11
+ end
21
12
 
22
- describe "pwd2" do
23
- it "runs ok" do
24
- Pa.pwd2.should be_an_instance_of Pa
25
- end
26
- end
13
+ end
27
14
 
28
- describe ".shorten" do
15
+ describe ".shorten2" do
29
16
  it "short /home/usr/file into ~/file" do
30
17
  ENV["HOME"] = "/home/foo"
31
- Pa.shorten("/home/foo/file").should == "~/file"
18
+ Pa.shorten2("/home/foo/file").should == "~/file"
32
19
  end
33
20
 
34
21
  it "not short /home/other-user/file" do
35
22
  ENV["HOME"] = "/home/foo"
36
- Pa.shorten("/home/bar/file").should == "/home/bar/file"
23
+ Pa.shorten2("/home/bar/file").should == "/home/bar/file"
37
24
  end
38
25
  end
39
26
 
@@ -48,31 +35,26 @@ describe Pa do
48
35
 
49
36
  end
50
37
 
51
- describe "basename" do
52
- it "get a basename of a path" do
53
- Pa.basename("/home/foo").should == "foo"
54
- end
55
-
38
+ describe ".base2" do
56
39
  it "get name, ext with :ext => true" do
57
- Pa.basename("/home/foo.bar", ext: true).should == ["foo", "bar"]
40
+ Pa.base2("/home/foo.bar", ext: true).should == ["foo", "bar"]
58
41
  end
59
-
60
42
  end
61
-
62
- describe '.parent' do
43
+
44
+ describe '.parent2' do
63
45
  before :each do
64
46
  @path = "/home/foo/a.txt"
65
47
  end
66
48
 
67
49
  it "return parent path" do
68
- Pa.parent(@path).should == "/home/foo"
50
+ Pa.parent2(@path).should == "/home/foo"
69
51
  end
70
52
 
71
53
  it "return parent upto 2 level path" do
72
- Pa.parent(@path, 2).should == "/home"
54
+ Pa.parent2(@path, 2).should == "/home"
73
55
  end
74
56
  end
75
-
57
+
76
58
  describe "#==" do
77
59
  it "runs ok" do
78
60
  (Pa('/home') == Pa('/home')).should be_true
@@ -85,9 +67,9 @@ describe Pa do
85
67
  end
86
68
  end
87
69
 
88
- describe "#sub" do
70
+ describe "#sub2" do
89
71
  it "runs ok" do
90
- Pa('/home/foo').sub(/o/,'').should == Pa('/hme/foo')
72
+ Pa('/home/foo').sub2(/o/,'').should == '/hme/foo'
91
73
  end
92
74
  end
93
75
 
@@ -99,9 +81,9 @@ describe Pa do
99
81
  end
100
82
  end
101
83
 
102
- describe "#gsub" do
84
+ describe "#gsub2" do
103
85
  it "runs ok" do
104
- Pa('/home/foo').gsub(/o/,'').should == Pa('/hme/f')
86
+ Pa('/home/foo').gsub2(/o/,'').should == '/hme/f'
105
87
  end
106
88
  end
107
89
 
@@ -136,11 +118,4 @@ describe Pa do
136
118
  (Pa('/home/foo') =~ /foo/).should be_true
137
119
  end
138
120
  end
139
-
140
- describe "#dir_pa" do
141
- it "return Pa instance" do
142
- Pa('/home/guten').dir_pa.should be_an_instance_of(Pa)
143
- end
144
- end
145
-
146
121
  end