Module: CoreEx::Pathname
- Included in:
- Pathname
- Defined in:
- lib/core_ex/pathname.rb
Defined Under Namespace
Modules: Assertions, ClassMethods, ShortCut
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#open_mode ⇒ Object
Returns the value of attribute open_mode.
127
128
129
|
# File 'lib/core_ex/pathname.rb', line 127
def open_mode
@open_mode
end
|
Instance Method Details
Allow this kind of things:
root = Pathname.new('/tmp/test')
foo, bar = 'foo', 'bar'
...
(root/foo/bar).each_line do |line|
...
end
38
39
40
|
# File 'lib/core_ex/pathname.rb', line 38
def / ( rhs )
self + rhs
end
|
#cp(aPath) ⇒ Object
77
78
79
|
# File 'lib/core_ex/pathname.rb', line 77
def cp ( aPath )
::FileUtils.cp self.to_s, aPath.to_s
end
|
#cp_r(aPath) ⇒ Object
85
86
87
|
# File 'lib/core_ex/pathname.rb', line 85
def cp_r ( aPath )
::FileUtils.cp_r self.to_s, aPath.to_s
end
|
#ensure_mkdir ⇒ Object
42
43
44
|
# File 'lib/core_ex/pathname.rb', line 42
def ensure_mkdir
(mkdir) rescue Errno::EEXIST
end
|
#ensure_mkpath ⇒ Object
46
47
48
|
# File 'lib/core_ex/pathname.rb', line 46
def ensure_mkpath
(mkpath) rescue Errno::EEXIST
end
|
#expand_path_with(directories, extensions = nil) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/core_ex/pathname.rb', line 155
def expand_path_with ( directories, extensions=nil )
ext = extname
exts, dirs = [''], ['']
unless extensions.nil?
if ext.empty?
exts = extensions
else
unless extensions.include? ext
raise ArgumentError, "bad extension `#{ext}' for #{self}"
end
end
end
dirs = directories unless absolute?
exts.each do |ext|
dirs.each do |dir|
dir = dir.to_path
file = dir + "#{self}#{ext}"
return file.expand_path.cleanpath if file.exist?
end
end
return nil
end
|
#ext(newext = '') ⇒ Object
Replace the file extension with newext
. If there is no extenson on the string, append the new extension to the end. If the new extension is not given, or is the empty string, remove any existing extension.
ext
is a user added method for the String class.
63
64
65
66
67
68
69
70
|
# File 'lib/core_ex/pathname.rb', line 63
def ext(newext='')
str = self.to_s
return self if ['.', '..'].include? str
if newext != ''
newext = (newext =~ /^\./) ? newext : ("." + newext)
end
(str.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || str + newext).to_path
end
|
#extsplit(aChar = '.') ⇒ Object
50
51
52
53
54
55
|
# File 'lib/core_ex/pathname.rb', line 50
def extsplit ( aChar='.' )
raise ArgumentError, "#{aChar} is not just a char" if aChar.size != 1
aChar = Regexp.escape(aChar)
to_s =~ /^(.*?)(#{aChar}[^#{aChar}]*)?$/
[::Pathname.new($1), $2 || '']
end
|
140
141
142
143
|
# File 'lib/core_ex/pathname.rb', line 140
def import!
base_path = expand_path.cleanpath.to_s.gsub(/\/+/, '/').to_path.split_with_load_path.last
Dependencies.depend_on to_s unless base_path.ext.to_s.camelize.constantize
end
|
#load_path! ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File 'lib/core_ex/pathname.rb', line 145
def load_path!
string = expand_path.cleanpath.to_s
raise "bad path name `#{self}' need a directory" unless directory?
return false if $LOAD_PATH.include? string and
Dependencies.load_paths.include? string
$LOAD_PATH.unshift string
Dependencies.load_paths.unshift string
return true
end
|
#mv(aPath) ⇒ Object
81
82
83
|
# File 'lib/core_ex/pathname.rb', line 81
def mv ( aPath )
::FileUtils.mv self.to_s, aPath.to_s
end
|
#overwrite(new_contents) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/core_ex/pathname.rb', line 20
def overwrite ( new_contents )
open('w') do |file|
file.print new_contents
end
self
end
|
89
90
91
|
# File 'lib/core_ex/pathname.rb', line 89
def rm
::FileUtils.rm self.to_s
end
|
101
102
103
|
# File 'lib/core_ex/pathname.rb', line 101
def rm_f
::FileUtils.rm_f self.to_s
end
|
93
94
95
|
# File 'lib/core_ex/pathname.rb', line 93
def rm_r
::FileUtils.rm_r self.to_s
end
|
97
98
99
|
# File 'lib/core_ex/pathname.rb', line 97
def rm_rf
::FileUtils.rm_rf self.to_s
end
|
#split(*args) ⇒ Object
72
73
74
75
|
# File 'lib/core_ex/pathname.rb', line 72
def split ( *args )
args.shift if args.first == '/'
split_no_args(*args)
end
|
#split_with_load_path(load_paths = $LOAD_PATH) ⇒ Object
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/core_ex/pathname.rb', line 129
def split_with_load_path ( load_paths=$LOAD_PATH )
str = to_s
load_paths = load_paths.sort { |x, y| y.to_path.to_s.size <=> x.to_path.to_s.size }
load_paths.each do |load_path|
if str =~ /^#{Regexp.quote(load_path)}\/*(.*)/
return [load_path.to_path, Regexp.last_match[1].to_path]
end
end
return nil
end
|
114
115
116
117
|
# File 'lib/core_ex/pathname.rb', line 114
def to_io
@open_mode ||= :r
open(@open_mode.to_s)
end
|
119
120
121
|
# File 'lib/core_ex/pathname.rb', line 119
def to_path
self
end
|
#to_yaml_string ⇒ Object
123
124
125
|
# File 'lib/core_ex/pathname.rb', line 123
def to_yaml_string
to_s
end
|
The current ruby’s unlink implementation has a bug.
106
107
108
109
110
111
112
|
# File 'lib/core_ex/pathname.rb', line 106
def unlink()
if FileTest.directory? @path and not FileTest.symlink? @path
Dir.unlink @path
else
File.unlink @path
end
end
|