<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Patrick Ward</title>
 <link href="http://patrickward.com/atom.xml" rel="self"/>
 <link href="http://patrickward.com"/>
 <updated>2012-04-09T05:46:58-04:00</updated>
 <id>http://patrickward.com</id>
 <author>
   <name>Patrick Ward</name>
   <email></email>
 </author>
 <generator>Willow</generator>
 <rights>Copyright © 2009-2012 Patrick Ward</rights>

 <entry>
   <title>Using NullDB with MiniTest:Spec and Rails 3.2</title>
   <link href="http://patrickward.com/code/2012/04/09/using-nulldb-with-minitest-spec/"/>
   <updated>2012-04-09T00:00:00-04:00</updated>
   <id>http://patrickward.com/code/2012/04/09/using-nulldb-with-minitest-spec</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I've been using MiniTest:Spec for testing my latest rails project. In addition, I've been trying to adapt the patterns and lessons discussed in Avdi Grimm's [Objects on Rails](http://objectsonrails.com/) (_I recommend purchasing his book, I did and was not dissapointed_).

However, Rails 3.2+ does not work well with NullDB. It would crash when running the `NullDB::nullify` and `NullDB::restore` commands.  After a few trials, I found that I could bypass thie built-in nullify command and establish the ActiveRecord connection myself.

First, I'm using the latest version from the GitHub repository in my Gemfile.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Gemfile - using the GitHub repository for the latest version.
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;.....
gem &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;activerecord-nulldb-adapter&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;symbol&quot;&gt;:git&lt;/span&gt; =&amp;amp;gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;git://github.com/nulldb/nulldb.git&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
.....
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
Next, I created spec helpers to setup and teardown the NullDB connections. This file loads in another spec_helper_lite.rb file (_discussed in the Objects on Rails book_). I then require the spec_helper_nulldb.rb file in those tests that can use the NullDB adpater in place of the regular database adapter that I'm using.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;spec_helper_nulldb.rb
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;require &amp;amp;quot;bundler/setup&amp;amp;quot;
require_relative &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;spec_helper_lite&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;

require &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;active_record&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;class&quot;&gt;SpecHelpers&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;function&quot;&gt;setup_nulldb&lt;/span&gt;
    require &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;nulldb/rails&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
    schema_path = &lt;span class=&quot;constant&quot;&gt;File&lt;/span&gt;.expand_path(&lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;../db/schema.rb&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;constant&quot;&gt;File&lt;/span&gt;.dirname(&lt;span class=&quot;predefined-constant&quot;&gt;__FILE__&lt;/span&gt;))
    &lt;span class=&quot;comment&quot;&gt;# NullDB.nullify(:adapter =&amp;amp;gt; :nulldb, :schema =&amp;amp;gt; schema_path)&lt;/span&gt;
    &lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;::&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;.establish_connection(&lt;span class=&quot;symbol&quot;&gt;:adapter&lt;/span&gt; =&amp;amp;gt; &lt;span class=&quot;symbol&quot;&gt;:nulldb&lt;/span&gt;, &lt;span class=&quot;symbol&quot;&gt;:schema&lt;/span&gt; =&amp;amp;gt; schema_path)
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;function&quot;&gt;teardown_nulldb&lt;/span&gt;
    &lt;span class=&quot;comment&quot;&gt;# NullDB.restore&lt;/span&gt;
    spec_path = &lt;span class=&quot;constant&quot;&gt;File&lt;/span&gt;.expand_path(&lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;../config/database.yml&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;constant&quot;&gt;File&lt;/span&gt;.dirname(&lt;span class=&quot;predefined-constant&quot;&gt;__FILE__&lt;/span&gt;))
    spec = &lt;span class=&quot;constant&quot;&gt;YAML&lt;/span&gt;.load_file(spec_path)
    &lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;::&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;.establish_connection(spec[&lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;])
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

So far, this is working great and gives me the benefits of NullDB's speed.

</content>
 </entry>
 <entry>
   <title>Recursively chmod Only Directories or Only Files</title>
   <link href="http://patrickward.com/code/2012/03/04/recursively-chmod-only-directories-or-only-files/"/>
   <updated>2012-03-04T00:00:00-05:00</updated>
   <id>http://patrickward.com/code/2012/03/04/recursively-chmod-only-directories-or-only-files</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I needed to `chmod` a deeply nested directory structure recently, but the directories needed to be **777** while the files needed to be set to a different level of permission. It's trivial to `chmod` on a directory and all it's files, but doing it only on the directories while leaving the files alone, required the use of the `find` command.

So, this is another note to self, for the next time I need to remember this.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Recursively change permissions on directories only:
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;find . -type d -exec chmod 777 {} \;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Breaking it down:

* Uses the `find` command on the current directory `.`
* `-type d` tells `find` to only look for files of type directory
* `-exec` tells `find` to perform the following action on each matching file. In this case it was to `chmod` each directory to 777.

This same technique can be used to `chown` directories and/or perform the same actions on only files.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Recursively change owner on directories only:
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;find . -type d -exec chown www-data.www-data {} \;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Recursively chmod/chown on files:
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;find . -type f -exec chmod 644 {} \;
find . -type f -exec chown www-data.www-data {} \;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</content>
 </entry>
 <entry>
   <title>A Few Notes on Using Rsync</title>
   <link href="http://patrickward.com/code/2012/02/11/a-few-notes-on-using-rsync/"/>
   <updated>2012-02-11T00:00:00-05:00</updated>
   <id>http://patrickward.com/code/2012/02/11/a-few-notes-on-using-rsync</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I've had to work with Rsync recently for several projects that required syncing up code between servers. So, before I forget the little bits of information that I've gleaned in the last few days, here are some notes to remind me and anyone else that might need a refresher.

I have not needed to set up an Rsync daemon yet, so this post will focus on using it with an SSH shell.

## Dual Rsyncing
#### Rsync must be installed on both ends of the pipe

If you're using Rsync without a daemon through the SSH shell, you still need to have Rsync installed on both the local and remote computers. I've used Rsync in the past, but never had to worry about this simple fact because Rsync was always installed. So, when I created a new server that didn't have Rsync installed by default, I was [quickly reminded][rsync-how] that Rsync works on both ends, not just the client side.

&amp;gt; When Rsync communicates with a remote non-daemon server via a remote shell the startup method is to fork the remote shell which will start an Rsync server on the remote system. Both the Rsync client and server are communicating via pipes through the remote shell. As far as the rsync processes are concerned there is no network. In this mode the rsync options for the server process are passed on the command-line that is used to start the remote shell.

## Customize SSH access
#### Use -e to control how SSH is used during the Rsync session

You're using [SSH][ssh], right? When you're using it with Rsync, you can customize the SSH session itself, allowing you to set any of the SSH flags you might need during the session. For example, I have rather large login banners with lots of legal wording that pop up when you sign in to any of my servers. That's nice, but I don't need those when I'm running automated scripts for my own purposes. So, I always like to pass the `-q` flag to SSH which allows SSH to run with a little less noise. Using Rsync's `-e` flag, you can pass a block of text that indicates both the shell and any required flags.


&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;The following snippet uses Rsync with the SSH shell, passing both a specific port and the quiet flag:
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;#
# Syncing files over ssh, using the quiet flag
#
$ rsync -avz -e 'ssh -q -p 22' /local/dir username@remotehost:/destination/dir

&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

The above command also makes use of some additional useful flags:

- `-a`: use archive mode (explained next)
- `-v`: use verbose mode (to tell me what's happening)
- `-z`: compress the file data during transfer (using less bandwidth)


## Archive mode
#### An explanation of the combination of flags when using `-a`

Most of the Rsync examples I see use the `-a` flag, which is really just a shortcut for several other flags put together. So, I thought I'd list them here as a quick reminder to my future self.

Option `-a` is equivalent to a combination of the following:

- `-r`: recurse into directories
- `-l`: copy symlinks as symlinks
- `-p`: preserve permissions
- `-t`: preserve modification times
- `-g`: preserve group
- `-o`: preserve owner (onlyl if using Rsync as a super-user only)
- `-D`: preserve device files (super-user only) and preserve special files

## Checksum calculation
#### Using checksum when modification times and size don't work

Sometimes, my modification times don't match up correctly, or I've used a program that tends to update modification times on a file even though the file hasn't changed. In that case, I find it's useful to send the `-c` flag, or `--checksum`, to Rsync, which tells it to skip a file if it's checksum matches the checksum of a file on the other side of the pipe rather than use it's modification time and file size.

Warning, though, this can be quite a bit slower than a normal Rsync transfer because the checksum requires the process to do a full checksum on both ends of the pipe.


&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Using checksum instead of modification times
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;#
# Checksum
#
$ rsync -avzc /local/dir username@remotehost:/destination/dir

&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

## Show Me the Progress
#### Use `--progress` to get a better picture of what's happening during the transfer

Most of the time my Rsync transfers are fairly quick, but in those cases where I'm transferring larger files or lots of files at a time, I like to keep an eye on which files are being copied, at what rate they are being copied, etc. For those times, it's useful to use the `--progress` indicator to give you a clearer picture of Rsync's current process.

&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;#
# Show me the progress
#
$ rsync -avz --progress assets/ username@remotehost:/destination/assets

Syncing assets...
building file list ...
94 files to consider
chunks/
site/less/
site/less/bootstrap/
templates/
templates/home.tpl
        4812 100%    3.92MB/s    0:00:00 (xfer#1, to-check=0/94)

sent 2970 bytes  received 108 bytes  2052.00 bytes/sec
total size is 1755190  speedup is 570.24
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


## Making Exclusions
### Using `--exclude` and `--exclude-from` to tell Rsync what not to sync

For many of my projects, I have certain files that don't belong on the destination side of the Rsync equation. For those, I create a simple file that follows the same basic pattern as exludes in GNU Tar or Git. For example, the following file can be used to exclude OS X .DS_Store or VIM .swp files from being transferred to the destination.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Filename: .rsync-exclude
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;.DS_Store
*.swp
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

I can then pass this filename to Rsync, telling it to ignore any files that match the patterns in that file.


&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Telling Rsync to exclude certain patterns from a file
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;#
# Transfer, but ignore patterns in .rsync-exclude
#
$ rsync -avz --exclude-from '.rsync-exclude' assets/ username@remotehost:/destination/assets
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

If you don't have a lot of patterns to match, you can also use the `--exclude` option to tell it specifically what not to transfer based on a pattern.


&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Telling Rsync not to transfer files that start with Foo
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;#
# Ignore Foo files
#
$ rsync -avz --exclude 'Foo*' assets/ username@remotehost:/destination/assets
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

## Transfer by Inclusion Only
#### Using `--include` to tell Rsync what not to exclude.

On the flip-side of exclusion are the inclusions you can give Rsync. There is also a `--include-from` option for matching patterns from a file. I find this can be useful if you want to specifically include certain files while excluding all others.

For example, the following will include only Bar files, but exclude all others.


&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Include only Bar files, excluding everything else.
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;#
# Only work with Bar files
#
$ rsync -avz --include 'Bar*' --exclude '*' assets/ username@remotehost:/destination/assets
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

[rsync-how]: http://rsync.samba.org/how-rsync-works.html
[ssh]: http://en.wikipedia.org/wiki/Secure_Shell
</content>
 </entry>
 <entry>
   <title>Movie Review: Drive (2011)</title>
   <link href="http://patrickward.com/words/2012/02/06/movie-review-drive-2011/"/>
   <updated>2012-02-06T00:00:00-05:00</updated>
   <id>http://patrickward.com/words/2012/02/06/movie-review-drive-2011</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">In lieu of watching the Super Bowl last night, I watched the movie Drive, staring Ryan Gosling, Carey Mulligan, Bryan Cranston, Ron Perlman, and the amazing Albert Brooks. I was blown away by this movie, not what I was expecting. It's a stylistic, existential, &amp;quot;man with no name&amp;quot; movie in the spirit of Sergio Leone's &amp;quot;Dollars Trilogy&amp;quot;. Only, instead of Clint Eastwood in the old west, we get an equally talented Ryan Gosling with cars, mobsters, and over the top violence. Gosling did a fantastic job. He has truly perfected the art of no-dilogue acting, something a less talented actor would not have been able to pull off.

There is a palpable tension throughout the movie, released only during driving scenes and sudden bouts of violence. But it works, it keeps you on the edge, wondering what will happen next. It fits the story, which is a parable in a way. Without giving away too much, I'll just remind you to think of &amp;quot;The Scorpion and the Frog&amp;quot; while you watch this movie.

Thankfully, this movie didn't turn out to be a typical Hollywood high-action car chase movie. It's a smart, cinematic movie with a surprisingly excellent role reversal for Albert Brooks. Now, I'm a fan of Mr. Brooks comedic talents, but I have a new found respect for him as a villain now. He walks the line between his usual likeable character and the sadistic, cold-hearted mobster he turns out to be. It's subtle though, like much of the other acting here. Cranston, Mulligan, and Perlman all shine and fit both their roles and the style of the movie perfectly. There isn't any scene stealer, hyperbolic acting in this movie, which is a welcome change in today's plastic movie going experiences.

If you're a fan of cerebral, understated movies that aren't carried on the backs of unnecessary action scenes and subpar acting, you'll love this movie. But, for the action junkies, it has plenty of movie conflict when needed. I think it has classic written all over it, not unlike the classic &amp;quot;Bullit&amp;quot; with Steve McQueen.


</content>
 </entry>
 <entry>
   <title>Randomizing Javascript Arrays</title>
   <link href="http://patrickward.com/code/2012/02/02/randomizing-javascript-arrays/"/>
   <updated>2012-02-02T00:00:00-05:00</updated>
   <id>http://patrickward.com/code/2012/02/02/randomizing-javascript-arrays</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I needed to randomize a list of images the other day using Coffeescript. So, I investigated a few techniques for doing that.  Apparently, the [Fisher Yates algorithm](http://en.wikipedia.org/wiki/Fisher-Yates_shuffle) (or *Knuth shuffle*) is one of the most efficient methods to randomize an array of values, because each *roll of the dice* is just as likely as another.

The following methods perform an in-place shuffle of the contents of an array. Original javascript algorithm can be [found here](http://sedition.com/perl/javascript-fy.html).

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;A Coffeescript version
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;randomizeArray = (arr) -&amp;amp;gt;
  i = arr.length
  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; i == &lt;span class=&quot;integer&quot;&gt;0&lt;/span&gt; then &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;predefined-constant&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;while&lt;/span&gt; --i
    j = Math.floor(Math.random() * (i+&lt;span class=&quot;integer&quot;&gt;1&lt;/span&gt;))
    [arr[i], arr[j]] = [arr[j], arr[i]]
  arr
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;A pure Javascript version
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;function&quot;&gt;randomizeArray&lt;/span&gt;(arr) {
  &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; i = arr.length;
  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (i == &lt;span class=&quot;integer&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;predefined-constant&quot;&gt;false&lt;/span&gt;;
  &lt;span class=&quot;keyword&quot;&gt;while&lt;/span&gt; (--i) {
    &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; j = Math.floor(Math.random() * (i + &lt;span class=&quot;integer&quot;&gt;1&lt;/span&gt;) );
    &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; tempi = arr[i];
    &lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; tempj = arr[j];
    arr[i] = tempj;
    arr[j] = tempi;
  }
  &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; arr;
}
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;


</content>
 </entry>
 <entry>
   <title>Obligatory New First Post</title>
   <link href="http://patrickward.com/words/2012/01/31/obligatory-new-first-post/"/>
   <updated>2012-01-31T00:00:00-05:00</updated>
   <id>http://patrickward.com/words/2012/01/31/obligatory-new-first-post</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I've decided to give the blog idea another shot. So, here's the obligatory new first post to get things started. It's a small nudge to get the lizard brain out of the way and the words flowing again. *That little sucker's got a serious hold on me at times.*

True to my split personality, I've collected both my interests in literature &amp;amp;amp; liberal arts studies along with my interests in programming &amp;amp;amp; technology onto this one blog. It might be a strange combination, but I have no desire to manage multiple blogs right now.

I would say that a blog like this ends up consisting primarily of *&amp;quot;notes to self&amp;quot;*. However, in case there are any bored souls out there looking for something to read, I've split the posts into two main categories, created separate feeds for words and code, and will try to tag the posts with meaningful keywords in the future. That way, anyone who cares to can steer clear of an area they aren't concerned about.

### Willow

I've built the site with a derivative of the [Jekyll](https://github.com/mojombo/jekyll) blogging software called [Willow](https://github.com/patrickward/willow), which is of my own doing. It's an offshoot, but different in it's own right, using [Mustache](https://github.com/defunkt/mustache) as the templating engine and currently only supporting [Redcarpet](https://github.com/tanoku/redcarpet/) for Markdown syntax. I'll write more about it later, but the key differences at the present moment include:

* Uses the Mustache templating language instead of Liquid. I like it better than Liquid and it was a nice challenge.
* It does not parse all files in the directory. Instead, Willow uses specific directories (**posts**, **pages**, **public**, **assets**, etc.) for organizing different file types. Jekyll folders always looked messy to me and I didn't like creating filenames with leading underscores, so I opted to make Willow directories a little more like a structured application.
* Includes a Sprockets compiler for working with CSS and Javascript assets. Minification will be added later.
* Uses GLI for the command line executable, which provides a nicer command line syntax.
* A refactored document handling system; cleaned up and refactored for possible extensions in the future.
* A SiteList object that handles collections of pages, posts, categories, and tags with a consistent api. This allows me to use collated posts, grab categories as a hash or an array, etc.
* Uses Redcarpet exclusively for Markdown objects so that it can take advantage of Redcarpet's hooks. Of course, that's not to say that another Markdown parser can't be added later.
* Replaces Jekyll's Generator with Before and After Hooks. It's just the beginning of an idea, but I'd also like to add hooks before and after the reading, renering, and writing of pages and posts as well.
* Rather than associating output extensions per parser (or converter), Willow uses a default file extension that can be overriden on a per page basis. This allows you to create, for instance, .shtml or .php files using any parser, instead of the standard html extension. This was useful for a recent project of mine in which a client needed to continue using .shtml extensions on an existing server and was unwilling to change at the time.

</content>
 </entry>
 <entry>
   <title>How I Use Compass With Rails 3.1</title>
   <link href="http://patrickward.com/code/2011/08/19/how-i-use-compass-with-rails-3-dot-1/"/>
   <updated>2011-08-19T00:00:00-04:00</updated>
   <id>http://patrickward.com/code/2011/08/19/how-i-use-compass-with-rails-3-dot-1</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">For one of my ongoing projects, I'm using the latest version of Rails (3.1.0.rc6 as of this post) along with the [Compass](http://compass-style.org) CSS framework. At first, this turned out to be an adventure in maintaining a calm composure. But, my experience of the Rails Asset Pipeline coupled with the Compass framework is proving to be worth the effort.

## My First Approach

My first approach to the problem was to keep them completely separate. I generated a Compass project outside of the asset pipeline folders and set it's config parameters to generate CSS files within the normal *app/assets/stylesheets* folder under rails. The setup was essentially like this:

&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;    RAILS_ROOT
      + compass/
        + sass/
          style.scss
          _layout_partial.scss
          _some_other_partial.scss
          _etc...
      + app/
        + assets/
          + stylesheets/
            application.css
            style.css
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

Using [Guard](https://github.com/guard/guard) or `compass watch` on the command line I would essentially pre-compile the Compass CSS files. Using the default Rails application.css file, Sprockets would then pick up the pre-compiled style.css file for use within the Rails framework.

While this worked, it was hackish and required additional steps to complete. Thankfully, the [Rails](https://github.com/rails/rails), [sass-rails](https://github.com/rails/sass-rails), and [Compass](https://github.com/chriseppstein/compass) projects have all come a long way in the past few months.

## My New Approach

I've since abandoned the idea of using Compass outside of the asset pipeline and now use it coupled with Sprockets own require system. In addition to the latest versions of the Compass and Rails projects, an understanding of how Sprockets and the asset pipeline work is essential to making this work.

### Use the right version of Compass

To begin with, I found it necessary to use the git version of the rails31 branch of Compass, which seems to have some fixes for using it with Rails 3.1 conventions. Here's a summary of the key Gemfile adjustments I've made recently to get things kickstarted.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;Gemfile.rb
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;gem &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;rails&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;3.1.0.rc6&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;

group &lt;span class=&quot;symbol&quot;&gt;:assets&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt;
  gem &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;sass-rails&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &amp;amp;quot;~&amp;amp;gt; &lt;span class=&quot;float&quot;&gt;3.1&lt;/span&gt;.&lt;span class=&quot;error&quot;&gt;0&lt;/span&gt;.rc&amp;amp;quot;
  gem &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;coffee-rails&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &amp;amp;quot;~&amp;amp;gt; &lt;span class=&quot;float&quot;&gt;3.1&lt;/span&gt;.&lt;span class=&quot;error&quot;&gt;0&lt;/span&gt;.rc&amp;amp;quot;
  gem &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;uglifier&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
  gem &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;compass&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;symbol&quot;&gt;:git&lt;/span&gt; =&amp;amp;gt; &amp;amp;quot;https&lt;span class=&quot;symbol&quot;&gt;:/&lt;/span&gt;/github.com/chriseppstein/compass.git&amp;amp;quot;, &lt;span class=&quot;symbol&quot;&gt;:branch&lt;/span&gt; =&amp;amp;gt; &amp;amp;quot;rails31&amp;amp;quot;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

### Sprockets vs Sass vs Compass

At this point, I needed to understand the key differences betweeen Sprockets, Sass, and Compass.

**Sprockets** is essentially a pre-processor and rails uses it to stitch together your CSS files into a single, monolithic stylesheet. While it recognizes and precompiles (or filters) **Sass** files, it's require system does not understand the concept of Sass partials or mixins through the use of imports and includes. So, you have to separate the logic used in Sass from the logic used in Sprockets. There is a linear process that you must be aware of when working with the two that looks something like this:

1. Rails starts up the asset pipeline (or sprockets), which begins to read in the &amp;quot;required&amp;quot; files you specified
2. When it finds a .sass or .scss file in it's require path, it passes that file off to Sass for rendering. (*This is true for any file that requires additional processing, such as .erb files.*)
3. Sass then uses it's own rendering engine to import, include, and process partials, variables, etc. This is separate and outside the scope of what Sprockets does. So, if you want to reference Sass variables or mixins in different files, you need to ensure that the proper Sass @import calls have been made in those files.
4. Sprockets then stitches all of the rendered files it finds back into it's own process using the separate logic of its require system.

&amp;gt; The key distiction here is that Sprockets does not render Sass files, it filters them through Sass before including them back into it's own require system. They are separate and distinct in their knowledge of each other.

For **Compass**, remember that, at heart, it is a collection of Sass mixins. When used this way, you can create a folder structure that keeps Sass and Sprockets logically and visually separated.

### Separating Sass from Sprockets

What helped me was physically separating the Sass based code from the Sprocket requires. This is not the standard way that the Rails 3.1 generators expect you to use the asset pipeline, but it made more sense to me for several reasons:

1. I tend to organize my Compass/Sass files in heirarchical order which depends on a liberal use of @import and @include for mixins
2. I felt I had more control over the order of requires and the process that Sprockets uses to include files.
3. I liked the visual and physical separation of CSS code

The folder structure for my most recent project ended up looking similar to the following:

&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;    RAILS_ROOT
      + app/
        + assets/
          + stylesheets/
            application.css
            + application/
              application.css.scss
              _base.scss
              _buttons.scss
              ...
              _layout.scss
              _menu.scss
              _users.scss
              etc...
            + aristo/
              jquery.ui.all.css.scss
              _base.scss
              _button.scss
              etc..
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

This enabled me to separate key areas of the application's CSS and use the Compass framework, along with the full capabilities of Sass, without worrying about how Sprockets was going to pull in each of my files.

### Removing require\_tree

Rails puts a few magical requires into the default application.css, which at first seem perfectly reasonable, until you start using it the way I'm describing here. Then, it becomes a bit of a hassle.

So, my first order of business was to remove the `require_tree` directive from my application.css file. That directive requires all stylesheets from the current directory at the location of the directive. Since I am trying to have more control over how stylesheets are ordered in my application, that directive doesn't help me. So, out it goes and in go more specific requires based on my folder structure above.

### Adding application.css.scss

I like to structure my Compass files using a primary css file that pulls in additional partials as needed. This led me to creating the application.css.scss file underneath the *application* subdirectory: `application/application.css.scss`

Rather than use Rails generated scss files (e.g. users.css.scss for a users_controller), I opted to use partials underneath the application directory and pull them into the main application.css.scss file instead. This gives me greater control over the order of files that are imported and let's me use Compass and Sass the way that I'm used to using them. **In fact, when Rails generates the users.css.scss file, I just delete it before I make a commit to git.**

As you can see, the application.css.scss file becomes a mid-level processor for my Compass structured css:

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;application.css.scss
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;// &lt;span class=&quot;type&quot;&gt;Framework&lt;/span&gt;
&lt;span class=&quot;directive&quot;&gt;@import&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;compass/reset&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;;
&lt;span class=&quot;directive&quot;&gt;@import&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;compass/css3&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;;
&lt;span class=&quot;directive&quot;&gt;@import&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;susy&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;;
// &lt;span class=&quot;type&quot;&gt;Application&lt;/span&gt;
&lt;span class=&quot;directive&quot;&gt;@import&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;;
&lt;span class=&quot;directive&quot;&gt;@import&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;defaults&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;;
&lt;span class=&quot;directive&quot;&gt;@import&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;layout&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;;
// &lt;span class=&quot;type&quot;&gt;etc&lt;/span&gt;...
// &lt;span class=&quot;type&quot;&gt;Controllers&lt;/span&gt;
&lt;span class=&quot;directive&quot;&gt;@import&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;content&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;delimiter&quot;&gt;'&lt;/span&gt;&lt;/span&gt;;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
I can now use Compass and Sass the way I like to without worrying about how the Rails asset pipeline is going to see them.

### But, What About the application.css file?

Let's not forget that we're still using Sprockets. As I mentioned earlier, I take out the `require_tree` directive and replace it with specific requires for the Sass files I'm interested in including into my manifest.

In my case I'm adding both an application css file along with a jQuery UI file I converted to Sass for use in my application.

I keep the `require_self` directive, which leaves my application.css file looking similar to the following:

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;application.css
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comment&quot;&gt;/*
 *= require_self
 *= require &amp;amp;quot;aristo/jquery.ui.all&amp;amp;quot;
 *= require &amp;amp;quot;application/application&amp;amp;quot;
 *= require &amp;amp;quot;some_additional_css_file&amp;amp;quot;
*/&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

At this point, I can add additional plain css files, Sass files, vendor files, etc. all within the boundaries of how my application expects them to be ordered (i.e. required). An example of that is the use of my customized jquery.ui.all file that I'm requiring outside of my Compass framework.

## Summary

This approach allows me to use the best of Compass and Sass along with the flexibility (and Rails oriented way) of the asset pipeline (or Sprockets), but without having to sacrifice the tried and true method that Compass users are used to.

Condensed into a few easy steps, the process is:

1. Use the rails31 branch of the Compass gem in your Gemfile.
2. Separate your application's Compass/Sass files from Sprockets (e.g. I use a folder structure)
3. Remove the Rails generated css.scss files and use partials with Compass instead
4. Remove the `require_tree` directive from application.css
4. Add your additional Sass or CSS files using regular `require` directives in application.css

</content>
 </entry>
 <entry>
   <title>Techniques to Clear Floats in CSS</title>
   <link href="http://patrickward.com/code/2011/08/17/techniques-to-clear-floats-in-css/"/>
   <updated>2011-08-17T00:00:00-04:00</updated>
   <id>http://patrickward.com/code/2011/08/17/techniques-to-clear-floats-in-css</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I ran into the pervavise issue of having to clear a DIV that contains only floated DIV elements in it recently. The problem is that the outer container does not stretch to accomodate the height of it's child containers. So, if you have a background or a border on the containing element, it does not fully cover the child elements.

It's an old issue, but I keep forgetting where to find the information on how to fix it. So, this is a note to self for future reference.

I found the following resources to be the most useful in terms of the pros and cons of each technique. Please see them for more detailed instruction. I've summarized the basics below.

* See [CSS Clearing Floats on Stackoverflow](http://stackoverflow.com/questions/4701015/css-clearing-floats)
* See [Clearing Floats on Quirksmode](http://www.quirksmode.org/css/clearing.html)
* See [Which Method of 'clearfix' is Best? on Stackoverflow](http://stackoverflow.com/questions/211383/which-method-of-clearfix-is-best)
* See [How to Clear Floats Without Structural Markup](http://www.positioniseverything.net/easyclearing.html)

## Add an Empty DIV and clear:both Styling

The easiest, and oldest, fix is to add an empty DIV with a style of *clear:both* to the end of the outer container. This works, but it's not semantic and makes a mess of the page's markup.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;sample.css
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;constant&quot;&gt;#container&lt;/span&gt; { &lt;span class=&quot;key&quot;&gt;border&lt;/span&gt; &lt;span class=&quot;float&quot;&gt;1px&lt;/span&gt; &lt;span class=&quot;key&quot;&gt;solid&lt;/span&gt; &lt;span class=&quot;color&quot;&gt;#000000&lt;/span&gt;; }
&lt;span class=&quot;constant&quot;&gt;#left&lt;/span&gt;   { &lt;span class=&quot;key&quot;&gt;width&lt;/span&gt;: &lt;span class=&quot;float&quot;&gt;45%&lt;/span&gt;; &lt;span class=&quot;key&quot;&gt;float&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;left&lt;/span&gt;; }
&lt;span class=&quot;constant&quot;&gt;#right&lt;/span&gt;  { &lt;span class=&quot;key&quot;&gt;width&lt;/span&gt;: &lt;span class=&quot;float&quot;&gt;45%&lt;/span&gt;; &lt;span class=&quot;key&quot;&gt;float&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;right&lt;/span&gt;; }
&lt;span class=&quot;class&quot;&gt;.clear&lt;/span&gt; { &lt;span class=&quot;key&quot;&gt;clear&lt;/span&gt;:&lt;span class=&quot;value&quot;&gt;both&lt;/span&gt;; }
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;sample.html
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;div id=&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;container&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;
  &lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;div id=&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;left&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;Lorem ipsum...&lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;/div&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;
  &lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;div id=&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;right&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;Lorem ipsum...&lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;/div&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;
  &lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;div class=&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;clear&lt;span class=&quot;entity&quot;&gt;&amp;amp;quot;&lt;/span&gt;&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;&lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;/div&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;
&lt;span class=&quot;entity&quot;&gt;&amp;amp;lt;&lt;/span&gt;/div&lt;span class=&quot;entity&quot;&gt;&amp;amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

## Use the clearfix Hack

Beyond adding additional markup to the page, you can use the infamous 'clearfix' hack, which cheats by adding additional content *after* the containing element. While this prevents you from having to muddy the markup on the page, it feels a bid hackish to me.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;sample.css
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comment&quot;&gt;/* This fixes the problem for most browsers */&lt;/span&gt;
&lt;span class=&quot;class&quot;&gt;.clearfix&lt;/span&gt;&lt;span class=&quot;pseudo-class&quot;&gt;:after&lt;/span&gt;
{
  &lt;span class=&quot;key&quot;&gt;content&lt;/span&gt;: &lt;span class=&quot;error&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;value&quot;&gt;quot&lt;/span&gt;;.&lt;span class=&quot;error&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;key&quot;&gt;quot&lt;/span&gt;;;
  &lt;span class=&quot;key&quot;&gt;display&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;block&lt;/span&gt;;
  &lt;span class=&quot;key&quot;&gt;height&lt;/span&gt;: &lt;span class=&quot;float&quot;&gt;0&lt;/span&gt;;
  &lt;span class=&quot;key&quot;&gt;clear&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;both&lt;/span&gt;;
  &lt;span class=&quot;key&quot;&gt;visibility&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;hidden&lt;/span&gt;;
}

&lt;span class=&quot;comment&quot;&gt;/* This fixes the problem for IE7 */&lt;/span&gt;
&lt;span class=&quot;class&quot;&gt;.clearfix&lt;/span&gt; { &lt;span class=&quot;key&quot;&gt;min-height&lt;/span&gt;: &lt;span class=&quot;float&quot;&gt;1%&lt;/span&gt;; }

&lt;span class=&quot;comment&quot;&gt;/* This fixes the problem for IE6 */&lt;/span&gt;
&lt;span class=&quot;type&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;type&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;class&quot;&gt;.clearfix&lt;/span&gt; { &lt;span class=&quot;key&quot;&gt;height&lt;/span&gt;: &lt;span class=&quot;float&quot;&gt;1%&lt;/span&gt;; }
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

## Use the overflow:hidden Technique

The method I've tended to use and that has the least problems in my experience is the *overflow* method, with some additional css to trigger [hasLayout](http://www.satzansatz.de/cssd/onhavinglayout.html) in IE.

This method works great as long as there is no fixed height on the outer container. See [Quirksmode](http://www.quirksmode.org/css/clearing.html) for ways to resolve issues related to this technique.

I've seen some references indicate that you can use *overflow:auto* to achieve the same results, but you risk having scrollbars show. So far, the *overflow:hidden* method has been the cleanest and most efficient technique for me.

&lt;div class=&quot;code_block&quot;&gt;&lt;div class=&quot;code_header&quot;&gt;sample.css
&lt;/div&gt;&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;comment&quot;&gt;/* Cleanest method */&lt;/span&gt;
&lt;span class=&quot;class&quot;&gt;.container&lt;/span&gt; {
  &lt;span class=&quot;key&quot;&gt;overflow&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;hidden&lt;/span&gt;;
  &lt;span class=&quot;key&quot;&gt;display&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;inline-block&lt;/span&gt;; &lt;span class=&quot;comment&quot;&gt;/* Necessary to trigger &amp;amp;quot;hasLayout&amp;amp;quot; in IE */&lt;/span&gt;
  &lt;span class=&quot;key&quot;&gt;display&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;block&lt;/span&gt;; &lt;span class=&quot;comment&quot;&gt;/* Sets element back to block */&lt;/span&gt;
}
&lt;span class=&quot;comment&quot;&gt;/* Or, use the zoom method to trigger &amp;amp;quot;hasLayout&amp;amp;quot; in IE */&lt;/span&gt;
&lt;span class=&quot;class&quot;&gt;.container&lt;/span&gt; {
  &lt;span class=&quot;key&quot;&gt;overflow&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;hidden&lt;/span&gt;;
  &lt;span class=&quot;key&quot;&gt;zoom&lt;/span&gt;: &lt;span class=&quot;float&quot;&gt;1&lt;/span&gt;; &lt;span class=&quot;comment&quot;&gt;/* Necessary to trigger &amp;amp;quot;hasLayout&amp;amp;quot; in IE */&lt;/span&gt;
  &lt;span class=&quot;key&quot;&gt;display&lt;/span&gt;: &lt;span class=&quot;value&quot;&gt;block&lt;/span&gt;; &lt;span class=&quot;comment&quot;&gt;/* Sets element back to block */&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

</content>
 </entry>
 <entry>
   <title>Understanding Subject in RSpec 2</title>
   <link href="http://patrickward.com/code/2011/08/15/understanding-subject-in-rspec-2/"/>
   <updated>2011-08-15T00:00:00-04:00</updated>
   <id>http://patrickward.com/code/2011/08/15/understanding-subject-in-rspec-2</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I needed to write some shared examples in RSpec the other day for a simple ActiveRecord concern I was writing.

In doing so, it forced me to understand what RSpec means by the &amp;quot;subject&amp;quot; of an example. I'll admit this is a very simple concept, but until now I didn't take the time to understand it and the [relish docs](http://relishapp.com/rspec/rspec-core) weren't overly descriptive on how it works.

In general, the *subject* of an example is just **the object being described**. But what, specifically, does that mean?

In an implicit context, it means the subject is an *instance* of the object being described. For example, in a model spec, I might be describing a User class:

``` Ruby
    describe User do
      it { should respond_to :authenticate }
    end
```

Which can also be written as:

``` Ruby
    describe User do
      subject.should respond_to :authenticate
    end
```

Here, the *subject* is an instance of User. RSpec creates an instance of User by calling new on the User class without any arguments. It's the same as saying this:

``` Ruby
    describe User do
      subject { User.new }
      it { should respond_to :authenticate }
    end
```

That last example, is what RSpec calls an **explicit subject**. Instead of deducing the subject from the outer describe, an *explicit subject* is declared within the example and can be used afterwards the same way that an *implicit subject* would.

&amp;gt; The key distinction for me, and one that I didn't understand previously, was that the *subject* is an **instance** of the class being described (implicitly or explicitly).

This came up when I needed to test both an instance of a subject as well as a static class method of a subject's parent class.

## But how do I test a static class method against an instance?

Simple, just call *class* on the instance.

``` Ruby
    describe User do
      subject.class.some_class_method.should do_something
    end
```

It's all very simple, but I just hadn't grokked it until now. Fortunately, this realization came in handy when I was writing a simple shared example to test common functionality against multiple models.

In this case I wanted to test that a model had a specific instance method as well as some class-based scopes.

``` Ruby
    shared_examples_for &amp;quot;a Trashable&amp;quot; do |model_type|

      before(:each) do
        @objects = [
          Factory(model_type, :trashed =&amp;gt; false),
          Factory(model_type, :trashed =&amp;gt; true)
        ]
      end

      it &amp;quot;should respond to a trash method&amp;quot; do
        subject.should respond_to :trash
      end

      # Note - have to call class on the implicit subject to call a class method!
      it &amp;quot;should not show trashed subjects by default&amp;quot; do
        subject.class.all.should_not include(@objects.second)
      end

      it &amp;quot;should show trashed subjects&amp;quot; do
        subject.class.trashed.should include(@objects.second)
        subject.class.trashed.should_not include(@objects.first)
      end
    end
```

By using *subject* and passing in the model_type to generate some factories, I'm able to dynamically adjust to whatever model the shared example exists within at the time. Simple, dry, and efficient.

</content>
 </entry>
 <entry>
   <title>Installing PHPUnit With MAMP 2.0.1</title>
   <link href="http://patrickward.com/code/2011/08/07/installing-phpunit-with-mamp-2-dot-0-1/"/>
   <updated>2011-08-07T00:00:00-04:00</updated>
   <id>http://patrickward.com/code/2011/08/07/installing-phpunit-with-mamp-2-dot-0-1</id>
   <author>
     <name>Patrick Ward</name>
   </author>
   <rights>Copyright © 2009-2012 Patrick Ward</rights>
   <content type="html">I needed to install PHPUnit with my local version of MAMP Pro 2.0.1 recently. After starting with this article for ideas, I came up with the following adjusted steps:

**1.** Create a soft link to the MAMP version of pear:

``` shell
ln -s /Applications/MAMP/bin/php/php5.3.6/bin/pear \
/usr/local/bin/pear
```

**2.** I found that the default pear.conf file was either corrupt or invalid. Following a forum suggestion on the mamp.info site, I just removed it by renaming it:

``` bash
mv /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf \
/Applications/MAMP/bin/php/php5.3.6/conf/pear.conf.bak
```

**3.** Add the pear channels associated with PHPUnit:

``` bash
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
pear channel-discover components.ez.no
```

**4.** Install PHPUnit:

``` bash
pear install phpunit/PHPUnit
```

_Note: I ran into a an issue at this point, where pear would not install an unstable version of HTTP_Request2 that it found. So, I just forced it to install the RC1 version instead:_

``` bash
pear install -f HTTP_Request2-2.0.0RC1
```

_I ran the pear install command after this._

**5.** At this point, PHPUnit should be installed. So, I created a new soft link to the phpunit file:

``` bash
ln -s /Applications/MAMP/bin/php/php5.3.6/bin/phpunit \
/usr/local/bin/phpunit
```

**6.** Done. To test the install type:

``` bash
phpunit --version
```
</content>
 </entry>

</feed>

