Pow → ‘Path Manipulation’
Get Version
0.1.1What
Pow encapsulate the functionality of several ruby libraries (FileUtils, Files, Pathname and Dir) by creating objects out path strings.
Installing
sudo gem install pow
The basics
# The Simplest Example require 'rubygems' require 'pow' path = Pow["."] # Let's create a pow object in the current directory.
Wow, amazing right? I guess not. But we’ll get to the good stuff. Let’s look at how pow deals with directories. Assume we are dealing with a directory structure like this
Demonstration of usage
Consider this directory structure
- /tmp
- README
- /sub_dir
- /suber_dir
- file.txt
- program
- /extra_dir
# Open up the /tmp dir path = Pow["/tmp"] # Check out what's inside path.each {|child| puts "#{child} - #{child.class.name}" }
Output
/tmp/README - Pow::File /tmp/subdir - Pow::Directory /tmp/suber_dir - Pow::Directory /tmp/subdir/file.txt - Pow::File /tmp/subdir/program - Pow::File /tmp/extra_dir - Pow::Directory
Cool, but what if I just want the directories or just the files
path.files.each {|file| "This is a file #{file}"} path.directories.each {|directory| "Here is a directory #{directory}"}
When pow encounters a directory it creates a Pow::Directory object. This is enumerable so you can use all your favorite iterative methods on directory structures now.
path.find_all {|p| p.directory? and p.modified_at > 1.day.ago} path.reject {|p| p.permissions == 744} path.collect {|p| p.size}
You can also create directories and files
path["new_dir"].create # New dir path["tmp/tmp.file"].create # New file (if it has a '.' Pow considers it a file by default)" path["tmp/also_a_file"].create_file # If you want to force file creation just use create_file (create_directory for the reverse)
You can read and write to files as well
# Shove some stuff into tmp.file path["tmp/tmp.file"].open("w") do |file| file.puts "Hey Baby!" end # Get the stuff out of tmp.file" path["tmp/tmp.file"].open do |file| puts file.read end # Alternatively path["tmp/tmp.file"].read
You can delete files and directories
begin path.delete rescue PowError => e puts "Oh no! It threw an error because the tmp dir is not empty... we will have to force it." end path["tmp"].delete! path.exists? # => false
With Pow you can also create directory structures such as
- /moar_tmp
- /empty_dir
- /sub_dir
- info.txt
- README
Pow["./moar"].create do Pow["empty_dir"].create Pow["sub_dir"].create do Pow["info.txt"].create {|file| file.puts "Here is the info you desired!"} end Pow["README"].create_file {|file| file.puts "I'm so glad you read this."} end puts "So what are the dirs in tmp" puts Pow["./moar"].directories.join(", ") puts puts "What are the files in tmp" puts Pow["./moar"].files.join(", ") puts puts "Is the empty dir empty? #{Pow['./moar/empty_dir'].empty?}" Pow["./moar"].delete! # Clean up your messOutput
So what are the dirs in tmp /Users/corey/Code/pow/moar/empty_dir, /Users/corey/Code/pow/moar/sub_dir What are the files in tmp /Users/corey/Code/pow/moar/README Is the empty dir empty? true
Is the there a super cool, but ultimately pointless way to create a Pow object?”
path = Pow["."]/:tmp/:sub_dir/:another_dir/:deeper_still/"a.file" path.create
Forum
How to submit patches
Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above.
The trunk repository is svn://rubyforge.org/var/svn/pow/trunk for anonymous access.
License
This code is free to use under the terms of the MIT license.
Contact
All contact can be done through the rubyforge page http://rubyforge.org/projects/pow/
Corey Johnson, 9th October 2007
Theme extended from Paul Battley