Module: CSD::Extensions::Core::Pathname

Included in:
Pathname
Defined in:
lib/csd/extensions/core/pathname.rb

Overview

This module comprises extensions to Pathname objects.

Instance Method Summary collapse

Instance Method Details

#children_directories(&block) ⇒ Object

This method returns the full paths to all children-directories (i.e. first generation of descendants) of a directory in either an Array, or in a block.

Examples

Dir.directories('/home/user')    # => ['/home/user/Desktop', '/home/user/Documents', ...]

Dir.directories('/home/user') do |dir|
  puts dir
end


22
23
24
25
26
27
28
29
# File 'lib/csd/extensions/core/pathname.rb', line 22

def children_directories(&block)
  result = []
  children.map do |child|
    next unless child.directory?
    block_given? ? yield(child) : result << child
  end
  result
end

#current_path?Boolean

Verifies whether the current directory (i.e. pwd) is the path of this Pathname object. Returns true or false.

Examples

Dir.chdir('/home/user')
Pathname.new('/home/user').current_path?            # => true
Pathname.new('/home/user/../user').current_path?    # => true
Pathname.new('/lib').current_path?                  # => false
Pathname.new('/i/do/not/exist').current_path?       # => false

Returns:

  • (Boolean)


50
51
52
# File 'lib/csd/extensions/core/pathname.rb', line 50

def current_path?
  self.exist? and self.realpath == self.class.getwd.realpath
end

#enquoteObject

Converts a Pathname object into a String and wraps it into two quotation marks (according to the String#enquite method). This method is useful for printing a path in a readable way in a command-line user interface.



35
36
37
# File 'lib/csd/extensions/core/pathname.rb', line 35

def enquote
  to_s.enquote
end