<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://jonathanleighton.com/</id>
  <title>Jon Leighton</title>
  <updated>2012-04-19T22:00:00Z</updated>
  <link rel="alternate" href="http://jonathanleighton.com/"/>
  <link rel="self" href="http://jonathanleighton.com/articles.xml"/>
  <author>
    <name>Jon Leighton</name>
    <uri>http://jonathanleighton.com/</uri>
  </author>
  <entry>
    <id>tag:jonathanleighton.com,2012-04-20:/articles/2012/focused-controller/</id>
    <title type="html">Rails controllers and OOP</title>
    <published>2012-04-19T22:00:00Z</published>
    <updated>2012-04-19T22:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/focused-controller/"/>
    <content type="html">&lt;p&gt;This realisation lead me to experiment with altering the conventions
for controller code, and today I gave a talk on the topic at
&lt;a href="http://railsberry.com/"&gt;Railsberry&lt;/a&gt; called &lt;a href="http://railsberry.com/talks#jonleighton"&gt;You Hate Your Codebase
&amp;amp; It’s Your Fault&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I wrote a plugin named &lt;a href="https://github.com/jonleighton/focused_controller"&gt;Focused
Controller&lt;/a&gt; a while
back, as part of this experiment, but have kept quiet about it until
now, partly because it was undocumented.&lt;/p&gt;

&lt;p&gt;But now I’m ready to announce it and I’m really keen for people to try
it out and share their thoughts about this idea. I’ve gone into detail
about the problems and solutions in the README, so I won’t repeat that
here. Please just &lt;a href="https://github.com/jonleighton/focused_controller"&gt;check it
out&lt;/a&gt;!&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2012-04-15:/articles/2012/poltergeist-0-6-0/</id>
    <title type="html">Poltergeist 0.6.0: Javascript error handling and remote debugging</title>
    <published>2012-04-14T22:00:00Z</published>
    <updated>2012-04-14T22:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/poltergeist-0-6-0/"/>
    <content type="html">&lt;p&gt;My goal with Poltergeist is to create the &lt;em&gt;fastest and best&lt;/em&gt;
&lt;a href="https://github.com/jnicklas/capybara"&gt;Capybara&lt;/a&gt;
driver. A common problem when using Capybara, particularly when using a
headless driver, is debugging. To be the best driver, Poltergeist needs
to be the easiest to debug.&lt;/p&gt;

&lt;h2 id="javascript-errors"&gt;Javascript errors&lt;/h2&gt;

&lt;p&gt;Prior to 0.6.0, Poltergeist had rudimentary support for detecting
Javascript errors on the page, but it was impossible to report the stack
trace for these errors. This stems from a lack of support for reporting
stack traces in QtWebKit, which
&lt;a href="http://phantomjs.org/"&gt;PhantomJS&lt;/a&gt;
is based on. This problem
affects
&lt;a href="https://github.com/thoughtbot/capybara-webkit"&gt;capybara-webkit&lt;/a&gt; too.&lt;/p&gt;

&lt;p&gt;PhantomJS 1.5 was recently released, and prior to the release I worked
hard to add stack trace support there. This was a pretty hard task and
involved diving deep into WebKit’s source, but I got there eventually.&lt;/p&gt;

&lt;p&gt;Poltergeist 0.6.0 updates the PhantomJS dependency to 1.5, and so I am
happy to say that if there is a Javascript error in your page (or in
Poltergeist’s Javascript code), the error will trigger an exception in
your Capybara test, and you’ll see a full stack trace showing where it
happened.&lt;/p&gt;

&lt;p&gt;To my knowledge, Poltergeist is the first Capybara driver to support
Javascript error handling at this level.&lt;/p&gt;

&lt;h2 id="remote-debugging"&gt;Remote debugging&lt;/h2&gt;

&lt;p&gt;You may have heard of the ‘remote debugging’ feature in Safari or
Chrome. Essentially remote debugging means that your browser can run a
WebSocket API on a given port, and then another browser (possibly on a
completely different computer) can connect to it and use a Web Inspector
to interact with the page.&lt;/p&gt;

&lt;p&gt;This feature is actually a feature of WebKit which means that PhantomJS,
being built on WebKit, can have remote debugging too. I’m sure you’ve
guessed by now where this is going, so yes, Poltergeist 0.6.0 supports
remote debugging.&lt;/p&gt;

&lt;p&gt;There is an important caveat. It &lt;a href="http://code.google.com/p/phantomjs/issues/detail?id=430"&gt;has been
reported&lt;/a&gt; that
remote debugging in PhantomJS doesn’t work on OS X. I don’t own a Mac,
so it’s hard for me to confirm this problem or debug it myself. For now
all I can say is that I have used the remote debugging feature
successfully on Linux, and I would be delighted if somebody wants to
step up and work out how to fix it on OS X.&lt;/p&gt;

&lt;p&gt;Here’s how it works:&lt;/p&gt;

&lt;p&gt;If you setup Poltergeist with the &lt;code&gt;:inspector =&amp;gt; true&lt;/code&gt; option (&lt;a href="https://github.com/jonleighton/poltergeist"&gt;see the
docs for details&lt;/a&gt;),
PhantomJS will get set up in remote debugging mode. Note that due to
internal WebKit reasons, we can’t report Javascript stack traces in this
mode (but that’s okay, because you can see the stack traces in your inspector
window).&lt;/p&gt;

&lt;p&gt;With the inspector enabled, you can insert &lt;code&gt;page.driver.debug&lt;/code&gt; into your
test. This will pause the test and launch a browser window with two
links. The first one is the “page” that PhantomJS runs its script in
(don’t worry about this).&lt;/p&gt;

&lt;p&gt;The second one is the one you want. Click on
it and you’ll see a web inspector - this should look familiar. You can
now add breakpoints to your code, or inspect the DOM, or whatever. When
you’re read press return in the console and your test will continue. If
you want to pause it again, just add &lt;code&gt;page.driver.pause&lt;/code&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2012-02-19:/articles/2012/stop-test-unit-autorun/</id>
    <title type="html">How to prevent Ruby's test/unit library from autorunning your tests</title>
    <published>2012-02-18T23:00:00Z</published>
    <updated>2012-02-18T23:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/stop-test-unit-autorun/"/>
    <content type="html">&lt;p&gt;&lt;code&gt;test/unit&lt;/code&gt; installs an &lt;code&gt;at_exit&lt;/code&gt; hook which runs all the tests before
Ruby exits. This was problematic, because it meant that all the tests
ran &lt;em&gt;after&lt;/em&gt; perftools had already finished its profile.&lt;/p&gt;

&lt;p&gt;So I wanted to turn off the autorunning and explicitly run the test
suite. I found out how to monkey-patch &lt;code&gt;test/unit&lt;/code&gt; to achieve this, and
figured it might be useful to someone in the future, so:&lt;/p&gt;

&lt;h2 id="ruby-19"&gt;Ruby 1.9&lt;/h2&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;require &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;test/unit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Test::Unit::Runner&lt;/span&gt;
  &lt;span class="cv"&gt;@@stop_auto_run&lt;/span&gt; = &lt;span class="pc"&gt;true&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;FooTest&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;TestCase&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;test_foo&lt;/span&gt;
    assert &lt;span class="pc"&gt;true&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;Runner&lt;/span&gt;.new.run(&lt;span class="pc"&gt;ARGV&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="ruby-18"&gt;Ruby 1.8&lt;/h2&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;require &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;test/unit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="r"&gt;module&lt;/span&gt; &lt;span class="cl"&gt;Test::Unit&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="pc"&gt;self&lt;/span&gt;.&lt;span class="fu"&gt;run?&lt;/span&gt;; &lt;span class="pc"&gt;true&lt;/span&gt;; &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;FooTest&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;TestCase&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;test_foo&lt;/span&gt;
    assert &lt;span class="pc"&gt;true&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;AutoRunner&lt;/span&gt;.run&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2012-02-19:/articles/2012/finding-methods/</id>
    <title type="html">Tracking down method definitions in Ruby</title>
    <published>2012-02-18T23:00:00Z</published>
    <updated>2012-02-18T23:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/finding-methods/"/>
    <content type="html">&lt;p&gt;A trick that I often use is to get a &lt;code&gt;Method&lt;/code&gt; object for the method that
I want to find, and then to print out its location. Like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Foo&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;bar&lt;/span&gt;
    &lt;span class="s"&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="k"&gt;omg, where am I?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

p &lt;span class="co"&gt;Foo&lt;/span&gt;.new.method(&lt;span class="sy"&gt;:bar&lt;/span&gt;).source_location &lt;span class="c"&gt;# found you!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Recently I was trying to find out where the &lt;code&gt;#flash&lt;/code&gt; method of
&lt;code&gt;ActionDispatch::TestRequest&lt;/code&gt; was defined. It’s not defined in the main
files that contain the definitions for &lt;code&gt;ActionDispatch::TestRequest&lt;/code&gt; or
&lt;code&gt;ActionDispatch::Request&lt;/code&gt; (which is the superclass).&lt;/p&gt;

&lt;p&gt;So I pulled out my standard tool:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;r = &lt;span class="co"&gt;ActionDispatch&lt;/span&gt;::&lt;span class="co"&gt;TestRequest&lt;/span&gt;.new
p r.method(&lt;span class="sy"&gt;:flash&lt;/span&gt;).source_location&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It didn’t work:&lt;/p&gt;

&lt;pre&gt;ArgumentError: wrong number of arguments (1 for 0)&lt;/pre&gt;

&lt;p&gt;It turns out that &lt;code&gt;ActionDispatch::Request&lt;/code&gt; has its own &lt;code&gt;method&lt;/code&gt; method,
which relates to the HTTP method. Hence Ruby’s &lt;code&gt;method&lt;/code&gt; method is
overridden.&lt;/p&gt;

&lt;p&gt;However, all was not lost! Ruby also makes it possible to get a
reference to a method definition that isn’t bound to any particular
object. This is called an &lt;code&gt;UnboundMethod&lt;/code&gt;, and you can’t call it until
you bind it to some object. So I was able to get an unbound reference to
the original &lt;code&gt;method&lt;/code&gt; method and then bind it to my object. Like so:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;r = &lt;span class="co"&gt;ActionDispatch&lt;/span&gt;::&lt;span class="co"&gt;TestRequest&lt;/span&gt;.new
meth = &lt;span class="co"&gt;Object&lt;/span&gt;.instance_method(&lt;span class="sy"&gt;:method&lt;/span&gt;)
p meth.bind(r).call(&lt;span class="sy"&gt;:flash&lt;/span&gt;).source_location &lt;span class="c"&gt;# found it!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It turns out that the &lt;code&gt;Request&lt;/code&gt; class is reopened in
&lt;code&gt;actionpack/lib/action_dispatch/middleware/flash.rb&lt;/code&gt; and the &lt;code&gt;flash&lt;/code&gt;
method gets defined there.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2012-01-09:/articles/2012/encapsulating-hashes/</id>
    <title type="html">Hashes and encapsulation</title>
    <published>2012-01-08T23:00:00Z</published>
    <updated>2012-01-08T23:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/encapsulating-hashes/"/>
    <content type="html">&lt;p&gt;&lt;a href="https://twitter.com/jonleighton/status/156424794152251393" class="image-link"&gt;
&lt;img src="twitter.png" alt="FYI: if you're accessing hash elements via obj.hashthings['foo'] rather than obj.hashthing('foo'), you're breaking encapsulation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then &lt;a href="https://twitter.com/joshkalderimis"&gt;Josh&lt;/a&gt;, not realising that I
was simply casting judgement down from my ivory tower, asked for more
than 140 characters worth of explanation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/joshkalderimis/status/156425566860476416" class="image-link"&gt;
&lt;img src="response.png" alt="@jonleighton FYI its nice if you explain a bit more :) blog post?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, I agreed. Here is that explanation.&lt;/p&gt;

&lt;p&gt;Suppose you have an object representing a
&lt;a href="http://en.wikipedia.org/wiki/Document_Object_Model"&gt;DOM&lt;/a&gt; element. DOM
elements have attributes, which you store as a hash. Something like
this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;DOMElement&lt;/span&gt;
  attr_reader &lt;span class="sy"&gt;:attributes&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;initialize&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    &lt;span class="iv"&gt;@attributes&lt;/span&gt; = &lt;span class="co"&gt;Hash&lt;/span&gt;[parse_attributes.map { |name, value|
      [name, &lt;span class="co"&gt;DOMAttribute&lt;/span&gt;.new(name, value)]
    }]
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In your code that uses &lt;code&gt;DOMElement&lt;/code&gt;, you access an attribute like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;element.attributes[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;margin-left&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you try to access an attribute that does not exist, &lt;code&gt;nil&lt;/code&gt; will be
returned.&lt;/p&gt;

&lt;p&gt;Later on, you realise that you are checking for &lt;code&gt;nil&lt;/code&gt; in lots of places
where you use attributes. Listening to the code smell, you decide that
missing attributes should instead return a &lt;a href="http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/"&gt;null
object&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What do you do? Well, your only option is to add a &lt;a href="http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-default-3D"&gt;default
proc&lt;/a&gt;
to the &lt;code&gt;Hash&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="iv"&gt;@attributes&lt;/span&gt; = &lt;span class="co"&gt;Hash&lt;/span&gt;[parse_attributes.map { |name, value|
  [name, &lt;span class="co"&gt;DOMAttribute&lt;/span&gt;.new(name, value)]
}]
&lt;span class="iv"&gt;@attributes&lt;/span&gt;.default_proc = proc { &lt;span class="co"&gt;NullDOMAttributes&lt;/span&gt;.new }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You have now made your object impossible to marshal without using
insane hackery, since procs cannot be marshalled.&lt;/p&gt;

&lt;p&gt;The more important point, though, is that by allowing users to dip into
the &lt;code&gt;@attributes&lt;/code&gt; hash like this, you are failing to encapsulate the
internal implementation of your &lt;code&gt;DOMElement&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;This presents another problem: an unrelated dodgy bit of code could
remove an attribute from the element, without your &lt;code&gt;DOMElement&lt;/code&gt; object
realising!&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;el.attributes.delete(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;margin-left&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you pass around the same hash between lots of different objects, that
hash is quite likely to get mutated at some point in my experience, and
when that happens it will affect all the other objects relying on it.&lt;/p&gt;

&lt;p&gt;Yes, you could call &lt;code&gt;@attributes.dup&lt;/code&gt; every time the &lt;code&gt;attributes&lt;/code&gt; method
is called, but that’s hardly very efficient when we only want to get at
a single attribute.&lt;/p&gt;

&lt;p&gt;If the code had been written in an encapsulated manner to begin with:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;DOMElement&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;initialize&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    &lt;span class="iv"&gt;@attributes&lt;/span&gt; = &lt;span class="co"&gt;Hash&lt;/span&gt;[parse_attributes.map { |name, value|
      [name, &lt;span class="co"&gt;DOMAttribute&lt;/span&gt;.new(name, value)]
    }]
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;attributes&lt;/span&gt;
    &lt;span class="iv"&gt;@attributes&lt;/span&gt;.dup
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;attribute&lt;/span&gt;(name)
    &lt;span class="iv"&gt;@attributes&lt;/span&gt;[name]
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It would be straightforward to add the null object without further consequences:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;attribute&lt;/span&gt;(name)
  &lt;span class="iv"&gt;@attributes&lt;/span&gt;.fetch(name) { &lt;span class="co"&gt;NullDOMElement&lt;/span&gt;.new }
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, exposing internal hashes can be convenient, but you should always
think twice before doing so and realise that you might regret the
decision at a later date.&lt;/p&gt;</content>
  </entry>
</feed>


