| Module | CoreEx::Pathname |
| In: |
lib/core_ex/pathname.rb
|
| open_mode | [RW] |
Allow this kind of things:
root = Pathname.new('/tmp/test')
foo, bar = 'foo', 'bar'
...
(root/foo/bar).each_line do |line|
...
end
# File lib/core_ex/pathname.rb, line 38 def / ( rhs ) self + rhs end
# File lib/core_ex/pathname.rb, line 85 def cp_r ( aPath ) ::FileUtils.cp_r self.to_s, aPath.to_s end
# 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
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.
# 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
# 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
# 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
# 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
# File lib/core_ex/pathname.rb, line 20 def overwrite ( new_contents ) open('w') do |file| file.print new_contents end self end
# File lib/core_ex/pathname.rb, line 72 def split ( *args ) args.shift if args.first == '/' split_no_args(*args) end
# 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