joy of rubyzip 0

Posted by jeremy
on Tuesday, October 17

rubyzip is a lib for creating / working with zip archives in ruby. It comes in gem form (gems are like free pizza. Your favorite kind of pizza that is delivered every time you want pizza. For free.)

» gem install rubyzip

Bam! Command line action! Superpower code files install! It all happens with the magic of the wires and the electrons we don’t see doing everything that is asked of them (at the speed of light!).

So now I have the rubyzip toolbox full of power methods for reading/writing/editing zip files all with ruby code. A zipfile is my baggie that I pile my files into so that they stay dry and fresh (and maybe a little smooshed, but that’s ok – they’re files after all)

I have to tell my model about the new hotness:
require 'zip/zip'
require 'zip/zipfilesystem'

So now my model knows rubyzip is invited to the party. Of course now I want to zip some stuff up.

def bundle_pdfs(name = self.subsetname, set = self.pertaining_set.set_name)
  # If for some reason a file disappears from the filesystem the db record is killed so that the zip will still be made
  self.documents.collect {|pdf| pdf.audit_file_presence}                                
  # Where the files (pdfs) for the subset are on the server
  location = "#{RAILS_ROOT}/files/#{set}/#{name}" 
  # Open or Create the Zip file for the subset archive 
  Zip::ZipFile.open("#{RAILS_ROOT}/files/#{set}/#{name}.zip", Zip::ZipFile::CREATE) {   
    |zipfile| # Send the zipfile into the block                                                                                  
    # Using the Active Record mojo grab all the documents inside the subset
    self.documents.collect {|pdf|                                                       
    # Assuming the files don't change once uploaded we just skip existing entries  
    unless zipfile.file.exists?("#{pdf.filename}") 
      # Add each new pdf from the subset to the zip archive                                     
      zipfile.add( "#{pdf.filename}", "#{location}/#{pdf.filename}")                    
    end
    } # When the block exits the zipfile is magically closed                                                                                  
  }
end

BAM! BOOYAH! Zip archives of the children of my model class saved to the file system outside of the public directory so no one can get to them unless I say so (or their ID validates them as having access). That’s some ugly code but it works (on windows small business server 2003/ IIS6 too!).

I get very frustrated at times with ruby code. Ruby coders get off on being clever. Their cleverness involves assumed knowledge and inside joke like design patterns that if you don’t “get” the joke you wonder where all the steps are that get to where you want to go with the code.

Like above if you didn’t know the zipfile is created if it doesn’t exist already by the initialization of the class you may run around in your head for an hour trying to figure out how you make a zip file in the first place. Luckily the author of the gem told me this early and often. But these sort of “assumed knowledge” events are not as clear to someone new to ruby/programming as one would think.

One man code factory technoweenie is in constant and heartbreakingly consistent violation of this. But I get it. He’s too busy writing all of this fracken brilliant rails stuff to stop and go back and explain the brilliance to us pea brains. He probably feels the same way about documenting/explaining his plugins as I feel about describing CSS styles to marketing managers, which means its a beat down and utterly defeating even in the act of contemplation.

Rails is easy. If you’re a ruby programmer. If you’re an on again off again php hack with grandiose ambitions and enough technical knowhow to break everything daily it’s hard. And every time you learn something new or understand something better about ruby/rails it sinks in deeper how feeble and weak your code-fu is and you start wondering if bussing tables wasn’t such a bad way to pay the bills as you’ll never have a big enough brain to do this stuff with any amount of skill.

In all honesty I can see how once you “get it” ruby code doesn’t need documentation. It IS the documentation. It’s very plain spoken when you speak it’s language. To the ruby brain I’m sure comments seem redundant and possible arrogant in nature (how dare you explain your code to me LIKE AN INFANT with all of these obvious and redundant comments you louse!).

Oh at times what I wouldn’t give for some of that arrogance and redundancy.

Comments

Leave a response

Comment