filestr 0.0.1
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/CHANGELOG.rdoc +3 -0
- data/LICENSE +20 -0
- data/README.rdoc +115 -0
- data/lib/filestr.rb +228 -0
- data/lib/version.rb +6 -0
- data/test/test_all.rb +55 -0
- metadata +60 -0
data/CHANGELOG.rdoc
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 tero.isannainen@gmail.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
= FileStr
|
2
|
+
|
3
|
+
* Introduction
|
4
|
+
* Usage
|
5
|
+
* Methods
|
6
|
+
|
7
|
+
|
8
|
+
= Introduction
|
9
|
+
|
10
|
+
FileStr is a library that augments String objects with File, Dir, and
|
11
|
+
FileUtils capabilities (class methods). The String (self) is passed as
|
12
|
+
the first argument to original class methods. With FileStr you can use
|
13
|
+
"normal" strings as filename/dirname objects.
|
14
|
+
|
15
|
+
It is sometimes inconvenient to write
|
16
|
+
|
17
|
+
File.exist?( 'report.txt' )
|
18
|
+
|
19
|
+
when you could instead do
|
20
|
+
|
21
|
+
'report.txt'.f_exist?
|
22
|
+
|
23
|
+
The class methods from File are prefixed with "f_", Dir methods with
|
24
|
+
"d_", and FileUtils methods with "u_". There are some special versions
|
25
|
+
of methods which are prefixed with "s_" (See: Methods).
|
26
|
+
|
27
|
+
FileStr class inherits String class, i.e. it has all String
|
28
|
+
methods. It also includes all the augmenting methods. You can keep
|
29
|
+
the augmenting methods within FileStr class, or you can explicitly
|
30
|
+
make them also part of String class.
|
31
|
+
|
32
|
+
|
33
|
+
= Usage
|
34
|
+
|
35
|
+
After requiring the "filestr" library, you have FileStr class defined
|
36
|
+
which includes methods from File, Dir, and FileUtils.
|
37
|
+
|
38
|
+
You can create a FileStr object from String:
|
39
|
+
|
40
|
+
require 'filestr'
|
41
|
+
fs = 'report.txt'.to_fs
|
42
|
+
|
43
|
+
or
|
44
|
+
|
45
|
+
fs = 'report.txt'.fs
|
46
|
+
|
47
|
+
Then you can check if the file exists?
|
48
|
+
|
49
|
+
fs.f_exist?
|
50
|
+
|
51
|
+
If you execute:
|
52
|
+
|
53
|
+
String.fileStr
|
54
|
+
|
55
|
+
you get all augmenting methods straight to String class and you can
|
56
|
+
use strings directly. For example:
|
57
|
+
|
58
|
+
'report.txt'.f_exist?
|
59
|
+
|
60
|
+
or
|
61
|
+
|
62
|
+
'bin'.d_exist?
|
63
|
+
|
64
|
+
to test if there is a sub-directory called "bin".
|
65
|
+
|
66
|
+
You can rename (move) a file with:
|
67
|
+
|
68
|
+
'report.txt'.u_mv( 'history.txt' )
|
69
|
+
|
70
|
+
|
71
|
+
= Methods
|
72
|
+
|
73
|
+
Almost all class methods from File and Dir are imported. A filtered
|
74
|
+
selection is taken from FileUtils.
|
75
|
+
|
76
|
+
Some methods from File and Dir doesn't take a name (filename, dirname)
|
77
|
+
as the first argument. There are modified versions of these methods
|
78
|
+
within FileStr where the "name" argument comes from self. These method
|
79
|
+
are prefixed with "s_".
|
80
|
+
|
81
|
+
List of special methods:
|
82
|
+
s_chmod, s_chown, s_fnmatch
|
83
|
+
|
84
|
+
You can change a mode of a file:
|
85
|
+
|
86
|
+
'report.txt'.s_chmod( 0777 )
|
87
|
+
|
88
|
+
There is also a special version of Dir.glob, called s_glob. s_glob
|
89
|
+
concatenates the string (self) to the given "pattern" argument, and
|
90
|
+
passed this to Dir.glob.
|
91
|
+
|
92
|
+
The complete list of methods coming directly from File, Dir, and
|
93
|
+
FileUtils is:
|
94
|
+
|
95
|
+
f_binread, f_binwrite, f_copy_stream, f_foreach, f_read,
|
96
|
+
f_readlines, f_sysopen, f_write, f_absolute_path, f_atime,
|
97
|
+
f_basename, f_birthtime, f_blockdev?, f_chardev?, f_ctime,
|
98
|
+
f_delete, f_directory?, f_dirname, f_executable?,
|
99
|
+
f_executable_real?, f_exist?, f_exists?, f_expand_path,
|
100
|
+
f_extname, f_filename, f_file?, f_ftype, f_grpowned?,
|
101
|
+
f_identical?, f_join, f_link, f_lstat, f_mtime, f_new, f_open,
|
102
|
+
f_owned?, f_path, f_pipe?, f_readable?, f_readable_real?,
|
103
|
+
f_readlink, f_realdirpath, f_realpath, f_rename, f_setgid?,
|
104
|
+
f_setuid?, f_size, f_size?, f_socket?, f_split, f_stat,
|
105
|
+
f_sticky?, f_symlink, f_symlink?, f_truncate, f_unlink,
|
106
|
+
f_world_readable?, f_world_writable?, f_writable?,
|
107
|
+
f_writable_real?, f_zero?, d_chdir, d_chroot, d_delete,
|
108
|
+
d_entries, d_exist?, d_exists?, d_foreach, d_mkdir, d_new,
|
109
|
+
d_open, d_rmdir, d_unlink, u_cd, u_cmp, u_mkdir_p, u_ln_s,
|
110
|
+
u_cp, u_cp_r, u_mv, u_rm, u_rm_f, u_rm_r, u_rm_rf, u_touch,
|
111
|
+
u_uptodate?
|
112
|
+
|
113
|
+
You can use the original documentation of the class methods
|
114
|
+
above. Just imagine the string (self) as being the first argument, and
|
115
|
+
the rest are as in documentation.
|
data/lib/filestr.rb
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
# {FileStr} is library that augments String objects with File, Dir,
|
2
|
+
# and FileUtils capabilities (class methods). The String (self) is
|
3
|
+
# passed as the first argument to original class methods.
|
4
|
+
|
5
|
+
|
6
|
+
require 'fileutils'
|
7
|
+
|
8
|
+
|
9
|
+
# {FileStr} class related module which maps missing methods to
|
10
|
+
# {FileStr} defined methods.
|
11
|
+
module FileStrModule
|
12
|
+
|
13
|
+
# Call FileStr supported methods (if available).
|
14
|
+
def method_missing( m, *args, &block )
|
15
|
+
|
16
|
+
klass = FileStr.support[ m ]
|
17
|
+
|
18
|
+
if klass
|
19
|
+
# Generate the actual class method name.
|
20
|
+
cm = m.to_s[2..-1].to_sym
|
21
|
+
klass.send( cm, self, *args, &block )
|
22
|
+
else
|
23
|
+
raise NoMethodError
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
# ------------------------------------------------------------
|
29
|
+
# File class support:
|
30
|
+
|
31
|
+
# Chmod with mode_int.
|
32
|
+
#
|
33
|
+
# @param mode_int [Integer] New file mode.
|
34
|
+
def s_chmod( mode_int )
|
35
|
+
File.chmod( mode_int, self )
|
36
|
+
end
|
37
|
+
|
38
|
+
# Chown with owner_int, group_int.
|
39
|
+
#
|
40
|
+
# @param owner_int [Integer] New file owner.
|
41
|
+
# @param group_int [Integer] New file group.
|
42
|
+
def s_chown( owner_int, group_int )
|
43
|
+
File.chown( owner_int, group_int, self )
|
44
|
+
end
|
45
|
+
|
46
|
+
# Fnmatch to pattern with flags.
|
47
|
+
#
|
48
|
+
# @param pattern [String] Glob patttern for matching.
|
49
|
+
# @param flags [Constant] Flags for matching.
|
50
|
+
def s_fnmatch( pattern, *flags )
|
51
|
+
File.fnmatch( pattern, self, *flags )
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
# ------------------------------------------------------------
|
56
|
+
# Dir class support:
|
57
|
+
|
58
|
+
# Glob with self as base string.
|
59
|
+
#
|
60
|
+
# @param pattern [String] Suffix of glob pattern (concat to self).
|
61
|
+
def s_glob( pattern = "", *args, &block )
|
62
|
+
Dir.glob( self + pattern, *args, &block )
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Enable use of File, Dir, and FileUtils class methods straight from
|
69
|
+
# {FileStr}/String objects.
|
70
|
+
class FileStr < String
|
71
|
+
|
72
|
+
include FileStrModule
|
73
|
+
|
74
|
+
|
75
|
+
# Extend a class (String) with {FileStr} features.
|
76
|
+
#
|
77
|
+
# @param klass [Constant] Class to extend.
|
78
|
+
def FileStr.extend( klass = String )
|
79
|
+
klass.send( :include, FileStrModule )
|
80
|
+
end
|
81
|
+
|
82
|
+
# Return hash describing {FileStr} provided File/Dir support.
|
83
|
+
def FileStr.support
|
84
|
+
@@fileStrSupport
|
85
|
+
end
|
86
|
+
|
87
|
+
# Supported methods from File, Dir, and FileUtils.
|
88
|
+
@@fileStrSupport = {
|
89
|
+
|
90
|
+
# File Methods from IO.
|
91
|
+
:f_binread => File,
|
92
|
+
:f_binwrite => File,
|
93
|
+
:f_copy_stream => File,
|
94
|
+
# :for_fd => File, # NA
|
95
|
+
:f_foreach => File,
|
96
|
+
# :new => File, # NA
|
97
|
+
# :open => File, # NA
|
98
|
+
# :pipe => File, # NA
|
99
|
+
# :popen => File, # NA
|
100
|
+
:f_read => File,
|
101
|
+
:f_readlines => File,
|
102
|
+
# :select => File, # NA
|
103
|
+
:f_sysopen => File,
|
104
|
+
# :try_convert => File, # NA
|
105
|
+
:f_write => File,
|
106
|
+
|
107
|
+
# File Methods.
|
108
|
+
:f_absolute_path => File,
|
109
|
+
:f_atime => File,
|
110
|
+
:f_basename => File,
|
111
|
+
:f_birthtime => File,
|
112
|
+
:f_blockdev? => File,
|
113
|
+
:f_chardev? => File,
|
114
|
+
# :chmod => File, # s-version
|
115
|
+
# :chown => File, # s-version
|
116
|
+
:f_ctime => File,
|
117
|
+
:f_delete => File,
|
118
|
+
:f_directory? => File,
|
119
|
+
:f_dirname => File,
|
120
|
+
:f_executable? => File,
|
121
|
+
:f_executable_real? => File,
|
122
|
+
:f_exist? => File,
|
123
|
+
:f_exists? => File,
|
124
|
+
:f_expand_path => File,
|
125
|
+
:f_extname => File,
|
126
|
+
:f_filename => File,
|
127
|
+
:f_file? => File,
|
128
|
+
# :fnmatch => File, # s-version
|
129
|
+
# :fnmatch? => File, # NA
|
130
|
+
:f_ftype => File,
|
131
|
+
:f_grpowned? => File,
|
132
|
+
:f_identical? => File,
|
133
|
+
:f_join => File,
|
134
|
+
# :lchmod => File,
|
135
|
+
# :lchown => File,
|
136
|
+
:f_link => File,
|
137
|
+
:f_lstat => File,
|
138
|
+
# :mkfifo => File, # Unknown
|
139
|
+
:f_mtime => File,
|
140
|
+
:f_new => File,
|
141
|
+
:f_open => File,
|
142
|
+
:f_owned? => File,
|
143
|
+
:f_path => File,
|
144
|
+
:f_pipe? => File,
|
145
|
+
:f_readable? => File,
|
146
|
+
:f_readable_real? => File,
|
147
|
+
:f_readlink => File,
|
148
|
+
:f_realdirpath => File,
|
149
|
+
:f_realpath => File,
|
150
|
+
:f_rename => File,
|
151
|
+
:f_setgid? => File,
|
152
|
+
:f_setuid? => File,
|
153
|
+
:f_size => File,
|
154
|
+
:f_size? => File,
|
155
|
+
:f_socket? => File,
|
156
|
+
:f_split => File,
|
157
|
+
:f_stat => File,
|
158
|
+
:f_sticky? => File,
|
159
|
+
:f_symlink => File,
|
160
|
+
:f_symlink? => File,
|
161
|
+
:f_truncate => File,
|
162
|
+
# :umask => File, # NA
|
163
|
+
:f_unlink => File,
|
164
|
+
# :utime => File, # NA
|
165
|
+
:f_world_readable? => File,
|
166
|
+
:f_world_writable? => File,
|
167
|
+
:f_writable? => File,
|
168
|
+
:f_writable_real? => File,
|
169
|
+
:f_zero? => File,
|
170
|
+
|
171
|
+
# Dir Methods.
|
172
|
+
:d_chdir => Dir,
|
173
|
+
:d_chroot => Dir,
|
174
|
+
:d_delete => Dir,
|
175
|
+
:d_entries => Dir,
|
176
|
+
:d_exist? => Dir,
|
177
|
+
:d_exists? => Dir,
|
178
|
+
:d_foreach => Dir,
|
179
|
+
# :getwd => Dir, # NA
|
180
|
+
# :glob => Dir, # s-version
|
181
|
+
# :home => Dir, # NA
|
182
|
+
:d_mkdir => Dir,
|
183
|
+
:d_new => Dir,
|
184
|
+
:d_open => Dir,
|
185
|
+
# :pwd => Dir, # NA
|
186
|
+
:d_rmdir => Dir,
|
187
|
+
:d_unlink => Dir,
|
188
|
+
|
189
|
+
# FileUtils Methods (selection).
|
190
|
+
:u_cd => FileUtils,
|
191
|
+
:u_cmp => FileUtils,
|
192
|
+
:u_mkdir_p => FileUtils,
|
193
|
+
:u_ln_s => FileUtils,
|
194
|
+
:u_cp => FileUtils,
|
195
|
+
:u_cp_r => FileUtils,
|
196
|
+
:u_mv => FileUtils,
|
197
|
+
:u_rm => FileUtils,
|
198
|
+
:u_rm_f => FileUtils,
|
199
|
+
:u_rm_r => FileUtils,
|
200
|
+
:u_rm_rf => FileUtils,
|
201
|
+
:u_touch => FileUtils,
|
202
|
+
:u_uptodate? => FileUtils,
|
203
|
+
}
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# Extend String with capability to convert itself to {FileStr}.
|
210
|
+
class String
|
211
|
+
|
212
|
+
# Use FileStr method directly in String.
|
213
|
+
def String.fileStr
|
214
|
+
FileStr.extend( String )
|
215
|
+
end
|
216
|
+
|
217
|
+
# Convert String to FileStr object.
|
218
|
+
def toFileStr
|
219
|
+
FileStr.new( self )
|
220
|
+
end
|
221
|
+
|
222
|
+
alias to_fs toFileStr
|
223
|
+
|
224
|
+
alias fs toFileStr
|
225
|
+
|
226
|
+
end
|
227
|
+
|
228
|
+
require_relative 'version'
|
data/lib/version.rb
ADDED
data/test/test_all.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require_relative '../lib/filestr'
|
4
|
+
|
5
|
+
|
6
|
+
class FileStrTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
Dir.chdir 'test'
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
Dir.chdir '..'
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_filestr
|
17
|
+
f = "filestr.txt".to_fs
|
18
|
+
|
19
|
+
f.u_touch
|
20
|
+
assert_true( f.f_exist? )
|
21
|
+
|
22
|
+
f.u_rm_f
|
23
|
+
assert_false( f.f_exist? )
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# Check that we are testing the current number of methods.
|
28
|
+
def test_string
|
29
|
+
String.fileStr
|
30
|
+
|
31
|
+
f = "string.txt"
|
32
|
+
|
33
|
+
f.u_touch
|
34
|
+
assert_true( f.f_exist? )
|
35
|
+
|
36
|
+
txt = "basic testing text\n"
|
37
|
+
f.f_write txt
|
38
|
+
assert_equal( txt, f.f_read )
|
39
|
+
|
40
|
+
list = './'.s_glob('*')
|
41
|
+
assert_equal( [ './string.txt', './test_all.rb' ], list )
|
42
|
+
|
43
|
+
d = 'test_dir'
|
44
|
+
d.d_mkdir
|
45
|
+
assert_true( d.d_exist? )
|
46
|
+
d.d_rmdir
|
47
|
+
assert_false( d.d_exist? )
|
48
|
+
|
49
|
+
fs = f.fs
|
50
|
+
|
51
|
+
fs.u_rm_f
|
52
|
+
assert_false( fs.f_exist? )
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filestr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tero Isannainen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-12-28 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'FileStr is a library that augments String objects with File, Dir,
|
15
|
+
and
|
16
|
+
|
17
|
+
FileUtils capabilities (class methods). The String (self) is passed as
|
18
|
+
|
19
|
+
the first argument to original class methods. With FileStr you can use
|
20
|
+
|
21
|
+
*normal* strings as filename/dirname objects.'
|
22
|
+
email: tero.isannainen@gmail.com
|
23
|
+
executables: []
|
24
|
+
extensions: []
|
25
|
+
extra_rdoc_files:
|
26
|
+
- README.rdoc
|
27
|
+
- CHANGELOG.rdoc
|
28
|
+
files:
|
29
|
+
- README.rdoc
|
30
|
+
- CHANGELOG.rdoc
|
31
|
+
- LICENSE
|
32
|
+
- lib/filestr.rb
|
33
|
+
- lib/version.rb
|
34
|
+
- test/test_all.rb
|
35
|
+
homepage:
|
36
|
+
licenses:
|
37
|
+
- Ruby
|
38
|
+
post_install_message: Check README...
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.9.3
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.23
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Augment Strings with File, Dir, and FileUtils capabilities.
|
60
|
+
test_files: []
|