Module CoreEx::Pathname
In: lib/core_ex/pathname.rb

Methods

/   cp   cp_r   ensure_mkdir   ensure_mkpath   expand_path_with   ext   extsplit   import!   load_path!   mv   overwrite   rm   rm_f   rm_r   rm_rf   split   split_with_load_path   to_io   to_path   to_yaml_string   unlink  

Classes and Modules

Module CoreEx::Pathname::Assertions
Module CoreEx::Pathname::ClassMethods
Module CoreEx::Pathname::ShortCut

Attributes

open_mode  [RW] 

Public Instance methods

Allow this kind of things:

 root = Pathname.new('/tmp/test')
 foo, bar = 'foo', 'bar'

 ...

 (root/foo/bar).each_line do |line|
   ...
 end

[Source]

# File lib/core_ex/pathname.rb, line 38
    def / ( rhs )
      self + rhs
    end

[Source]

# File lib/core_ex/pathname.rb, line 77
    def cp ( aPath )
      ::FileUtils.cp self.to_s, aPath.to_s
    end

[Source]

# File lib/core_ex/pathname.rb, line 85
    def cp_r ( aPath )
      ::FileUtils.cp_r self.to_s, aPath.to_s
    end

[Source]

# File lib/core_ex/pathname.rb, line 42
    def ensure_mkdir
      (mkdir) rescue Errno::EEXIST
    end

[Source]

# File lib/core_ex/pathname.rb, line 46
    def ensure_mkpath
      (mkpath) rescue Errno::EEXIST
    end

[Source]

# 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.

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Source]

# File lib/core_ex/pathname.rb, line 81
    def mv ( aPath )
      ::FileUtils.mv self.to_s, aPath.to_s
    end

[Source]

# File lib/core_ex/pathname.rb, line 20
    def overwrite ( new_contents )
      open('w') do |file|
        file.print new_contents
      end
      self
    end

[Source]

# File lib/core_ex/pathname.rb, line 89
    def rm
      ::FileUtils.rm self.to_s
    end

[Source]

# File lib/core_ex/pathname.rb, line 101
    def rm_f
      ::FileUtils.rm_f self.to_s
    end

[Source]

# File lib/core_ex/pathname.rb, line 93
    def rm_r
      ::FileUtils.rm_r self.to_s
    end

[Source]

# File lib/core_ex/pathname.rb, line 97
    def rm_rf
      ::FileUtils.rm_rf self.to_s
    end

[Source]

# File lib/core_ex/pathname.rb, line 72
    def split ( *args )
      args.shift if args.first == '/'
      split_no_args(*args)
    end

[Source]

# 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

[Source]

# File lib/core_ex/pathname.rb, line 114
    def to_io
      @open_mode ||= :r
      open(@open_mode.to_s)
    end

[Source]

# File lib/core_ex/pathname.rb, line 119
    def to_path
      self
    end

[Source]

# File lib/core_ex/pathname.rb, line 123
    def to_yaml_string
      to_s
    end

The current ruby‘s unlink implementation has a bug.

[Source]

# 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

[Validate]