About Jamie on Software

Jamie on Software is the online journal of web developer and writer Jamie Rumbelow.

Jamie likes books, guitars, programming, open source and food. He writes about these things too. This is where he puts the things he writes.

My Books

Tags
Tweets
Feeds
We Love
Powered by Squarespace
Monday
Apr302012

Open source (pour over pasta)

I've been making a real effort to tidy up my open source portfolio recently; getting issues closed, code tidied up, unit test suites written and on the fantastic Travis and people happier!

As part of this transition, I've decided to retire Sparkplugs and open source the code. Big changes are happening in my professional and personal life over the next few months and I feel it would be disingenuous to pretend that I could carry on supporting and developing my formerly commercial add-ons. After all, there haven’t been any updates to my add-ons for a while now, and support has been flakey. The honest truth is I lost my passion.

But onto bigger and better things!

Exciting things are going on over at my GitHub page (https://github.com/jamierumbelow). You’ll find my two most popular CodeIgniter libraries, MY_Model and MY_Controller. You'll find a recent experiment with node.js, Postmaster, an SMTP testing server I'm remarkably proud of. 

Also, you'll find the source to Sparkplugs' two commercial add-ons, Taggable and MojoBlog. I’m sure I’ll still be involved and am happy to answer any support questions through the issue trackers on the GitHub repositories.

It's an exciting time to be in open source and I've realised that I enjoy the atmosphere so much more than the pressures of a commercial add-on retailer.

So go on. Check out my GitHub profile :)

Thursday
Feb232012

The CodeIgniter Handbook - Volume 1 - Available!

I am utterly thrilled to announce that my new book, The CodeIgniter Handbook Volume 1, is now available for order!

Volume 1, Who Needs Ruby? is a pragmatic, succinct guide to improving your efficiency and the cleanness of your code. Attractive to amateurs and professionals alike, The CodeIgniter Handbook isn't your usual programming book. It's short, useful and easy to read, and covers how to improve the quality of your code and the speed of your development time.

It's available to buy as a print book or an eBook. It's also much cheaper than most programming books, at only £12 (roughly $18) for the print book and £6 ($10) for the eBook.

I've had some great feedback already -- everyone appears to be enjoying it! Thanks for your support.

Purchase the CodeIgniter Handbook today!

Monday
Feb062012

Happier Shelling

I've just added something to my ~/.profile file that has made me much happier when moving around in Terminal / the UNIX shell. If you're anything like me, you forget where you put things and what directories look like.

In order to cure this ailment, I re-defined the cd command and got it to list the contents of a directory every time I change directories. It's nice to have a constant reminder of how unorganised I am.

If you'd like to have this sweetness, add this to your ~/.profile:

cd () {
  builtin cd "$*"
  ls -al
}

Then, either re-open your Terminal window or run $ source ~/.profile to load it into the shell. Happy cding!

Saturday
Jan072012

Syntax Sugar #5 - A Quick CodeIgniter Caching Helper Function

Caching your data in CodeIgniter? Good. You should be.

I use memcached lots. CI has a fantastic caching driver built in (as of 2.0). Normally, the code you'll write looks like this:

$accounts = $this->cache->memcached->get('user.' . $user_id . '.accounts');

if (!$accounts)
{
    $accounts = $this->db->where('user_id', $user_id)->get('accounts')->results();
    $this->cache->memcached->save('user.' . $user_id . '.accounts', $accounts);
}

$this->load->view('accounts/index', array( 'accounts' => $accounts ));

However this quickly becomes repetitive, boring and cluttering. With a bit of PHP5.3 magic, we can create a really nice little caching helper function that cleans this code up in no time:

function cache($key, $data) {
    $CI =& get_instance();
    $cache = $CI->cache->memcached->get($key);

    if (!$cache) {
        // There's been a miss, so run our data function and store it
        $cache = $data($CI);
        $CI->cache->memcached->save($key, $cache);
    }

    return $cache;
}

Now, our cluttered caching code becomes a commendable concoction of coding culinary craft:

$accounts = cache('user.' . $user_id . '.accounts', function(&$ci){
    return $ci->db->where('user_id', $user_id)->get('accounts')->results();
});

The anonymous method you pass through will only be executed if the cache misses (i.e. there's no data under that key, or it has expired). It will be saved into the cache and return. Delicious.

Monday
Dec262011

CodeIgniter View Presenters

Whenever building applications with CodeIgniter, I always try my best to follow the Model-View-Controller design pattern as much as possible. MVC tells us that views are the presentational filter of the model's data. Over the course of an application's development, it can be very common for views to grow and accumulate cruft. I find that presenters can be a really sleek way of hiding presentational logic away in a class.

Let's look at a simple view that displays some information about a user's bank account:

<div id="account">
    <h1><?= $this->bank->get($account->bank_id)->name ?> - <?= $account->title ?></h1>

    <p class="information">
        <strong>Name:</strong> <?php if ($account->name): ?><?= $account->name ?><?php else: ?>N/A<?php endif; ?><br />
        <strong>Number:</strong> <?php if ($account->number): ?><?= $account->number ?><?php else: ?>N/A<?php endif; ?><br />
        <strong>Sort Code:</strong> <?php if ($account->sort_code): ?><?= substr($account->sort_code, 0, 2) . "-" . substr($account->sort_code, 2, 2) . "-" . substr($account->sort_code, 4, 2) ?><?php else: ?>N/A<?php endif; ?>
    </p>

    <p class="balances">
        <strong>Total Balance:</strong> <?php if ($account->total_balance): ?><?= "&pound;" . number_format($account->total_balance) ?><?php else: ?>N/A<?php endif; ?>
        <strong>Available Balance:</strong> <?php if ($account->available_balance): ?><?= "&pound;" . number_format($account->available_balance) ?><?php else: ?>N/A<?php endif; ?>
    </p>

    <p class="statements">
        <?php if ($this->statements->get_by('account_id', $account->id)): ?>
            <?= anchor('/statements/' . $account->id, 'View Statements') ?>
        <?php else: ?>
            Statements Not Currently Available
        <?php endif; ?>
    </p>
</div>

This is a rather typical view; it's displaying bits of content from an object, checking for a value's existence and pulling in bits from other database tables. It's fine, but it's all a bit messy, and we're duplicating a fair bit of code. Ideally, we want our view to look something like this:

<div id="account">
    <h1><?= $account->title() ?></h1>

    <p class="information">
        <strong>Name:</strong> <?= $account->name() ?><br />
        <strong>Number:</strong> <?= $account->number() ?><br />
        <strong>Sort Code:</strong> <?= $account->sort_code() ?>
    </p>

    <p class="balances">
        <strong>Total Balance:</strong> <?= $account->total_balance() ?>
        <strong>Available Balance:</strong> <?= $account->available_balance() ?>
    </p>

    <p class="statements"><?= $account->statements_link() ?></p>
</div>

This clears up our view considerably and removes a bunch of the duplication. It also strips out as much logic as possible from the views, and can make for some very succinct code.

We're going to create an application/presenters directory, and inside there an account_presenter.php file. This file will contain the presenter class for our account object. Let's start by extracting the title:

<?php

class Account_Presenter {

    public function __construct($account) {
        $this->account = $account;
        $this->ci =& get_instance();
    }

    public function title() {
        return $this->ci->bank->get($this->account->bank_id)->name . "-" . $account->title;
    }
}

We make sure that we can access the CodeIgniter superobject in $this->ci and the account object that we're presenting in $this->account. We can then port the logic that was previously in the view into a title() method.

We can now go ahead and tidy up the information section:

public function name() {
    return $this->account->name ?: "N/A";
}

public function number() {
    return $this->account->number ?: "N/A";
}

public function sort_code() {
    if ($sc = $this->account->sort_code) {
        return substr($sc, 0, 2) . "-" . substr($sc, 2, 2) . "-" . substr($sc, 4, 2);
    } else {
        return "N/A";
    }
}

Similarly, we can tidy up the balances section:

public function total_balance() {
    return ($this->account->total_balance) ? "&pound;" . number_format($this->account->total_balance) : "N/A";
}

public function available_balance() {
    return ($this->account->available_balance) ? "&pound;" . number_format($this->account->available_balance) : "N/A";
}

And finally, the statements link:

public function statements_link() {
    if ($this->ci->statements->get_by('account_id', $this->account->id)) {
        return anchor('/statements/' . $this->account->id, 'View Statements');
    } else {
        return "Statements Not Currently Available";
    }
}

Fantastic. Finally, in our controller, we need to pass through an instance of the presenter rather than the account object itself.

public function show($account_id) {
    $account = $this->account->get($account_id);
    $this->data['account'] = new Account_Presenter($account);

    $this->load->view('account/show', $this->data);
}

...and don't forget to load the presenter at the top of your controller:

require_once APPPATH . 'presenters/account_presenter.php';

...and we are done!

As I'm sure you can see, using presenters allows you to tidy up your views in a really object-oriented way. It'd be great to hear your thoughts about presenters and whether or not you plan on using them.

Page 1 ... 3 4 5 6 7 ... 12 Next 5 Entries »