Another Bubble?

26-May-08

Who knows? But a funny song anyway.

Filed in Uncategorized | Permalink | Comments (0) »

Shoes Talk

05-May-08

So, I gave my aforementioned talk on Shoes at the Dallas TechFest. It went pretty well, with plenty of questions. So, how about some slides? Have at ‘em. Code too!

shoes.pdf

shoes_talk.zip

Filed in Shoes, Speaking | Permalink | Comments (0) »

Towards a More Humble Shoes

02-May-08

I have an almost religious attachment to TDD (or BDD, whatever). I can’t really get comfortable with a programming task until I can squeeze my [TB]DD rhythm in somehow. I find the best way to write testable GUI code is the Model View Presenter pattern, or as Michael Feathers called it, The Humble Dialog Box.

In a nutshell, the MVP pattern is like the MVC pattern, except the view does not interrogate the controller (presenter, in this case). Rather, it limits itself to forwarding events like button clicks to its presenter, and waits for the presenter to ask it to display data.

The presenter is the big dog at the kennel. He receives events from the view, talks to the model and whatever other actors are present, and eventually shoves data back to the views. “Show the user this stuff,” he barks, “and make it snappy.”

He’s got a lot on his plate, but we’re okay with that because he’s easy to test. He’s especially easy to test with a dynamic language like Ruby.

So when I started coding a bit with Shoes, I was dismayed to find no easy way to take the behavior out of the view. I started playing with a way make my Shoes pages humble, and in the end it didn’t take much trouble.

We start with a couple of base classes. I’ll stick these on GitHub eventually, but you can grab them from here for now if you want to use them.

class HumbleShoes < Shoes
  alias_method :proud_button, :button
  def button name, *args
    proud_button(*args) { tell_presenter_click name }
  end

  alias_method :proud_edit_line, :edit_line
  def edit_line name, *args
    controls[name.to_sym] = proud_edit_line(*args)
  end

  def collect_control_vals
    controls.inject({}){|vals,kv| vals.merge(kv.first=>kv.last.text)}
  end
  def controls
    @controls ||= {}
  end
  def presenter
    @presenter ||= make_presenter
  end
  def tell_presenter_click name
    presenter.send “#{name}_click”, collect_control_vals
  end
  def make_presenter
    Kernel.const_get(self.class.to_s.sub(/[vV]iew/,”)+’Presenter’).new self
  end
end

class HumbleShoesPresenter
  def initialize view
    @view = view
  end
end

As you can see, the view class only handles buttons and edit_lines. I’ll add more over the next few days. To use them, define a view and presenter class for each Shoes page.

require File.dirname(__FILE__)+'/humble_shoes'
require File.dirname(__FILE__)+'/shout_presenter'

class ShoutView < HumbleShoes
  url '/', :main
  def main
      @edit = edit_line :edit
      button :shout, 'S H O U T   I T ! ! !', :>200
    end
  end
  def set_edit_txt txt
    @edit.text = txt
  end
end

The click blocks are gone from the buttons, and each control has a name parameter. The view has a method the presenter can use to push data. We could have the presenter do “@view.edit.text =” instead, but I prefer to leave the view in charge of how to display the data.

Here’s the presenter:

class ShoutPresenter < HumbleShoesPresenter
  def shout_click ctrl_vals
    @view.set_edit_txt ctrl_vals[:edit].upcase.gsub(/./,'\0 ')+"! ! !"
  end
end

The presenter implements a “_click” method for each clickable control. The method takes a hash of text values for each control, so the presenter has everything the user’s entered to work with.

Again, the main thing is that it’s testable. Here’s a spec:

require 'spec'
require 'shout_presenter'

describe "the shout presenter" do
  it "should shout when you click the shout button" do
    view = mock 'view'
    presenter = ShoutPresenter.new view
    view.should_receive(:set_edit_txt).with('O H   Y E A H ! ! !')
    presenter.shout_click(:edit=>'Oh Yeah')
  end
end

Mock-heavy testing, to be sure, but testing nonetheless.

Filed in GUI, Shoes, TDD | Permalink | Comments (1) »

Upcoming Talk

30-Apr-08

How in the world do people find time to blog? I was hoping for about a post/day, and I’ve yet to post anything of substance.

Well, here’s something anyway: Come to my talk! I’ll be talking about Shoes, why the lucky stiff’s GUI framework at the Dallas Tech Fest on May 3rd. The Tech Fest is free(!) but you do need to register. Go to dallastechfest.com for details.

Filed in Shoes, Speaking | Permalink | Comments (0) »

About the Name…

28-Apr-08

I have a daughter we can call Rabbit Ears and a son we’ll call Hammer Down. Here’s a recent conversation:

Me: Yes, monkeys do too have red butts.
Hammer Down: No! Monkey’s don’t have red butts!
Me: Yes, they do.
Rabbit Ears: No they don’t!
Me: Rabbit Ears, everybody knows monkeys have red butts.
Rabbit Ears: Well everybody knows you’re unkind to monkeys!!

Filed in Kids, Meta | Permalink | Comments (6) »

Home
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.