markdown.me

Markdown, a great shorthand syntax for creating HTML, and subsequently, for taking notes. I often take notes for different situations and use the Markdown syntax to help give them structure and organization. One thing I found I often wanted was a way to enter that Markdown somewhere, and have it generate an HTML page from it, with a permalink so I could share it or access it later. The Markdown dingus provides a great UI for testing out HTML conversion, but doesn’t provide any persistence, so I threw together a pretty quick site that fit my needs.

markdown.me

It’s pretty basic, but you can throw in your Markdown and you end up with a unique url you can share with the generated HTML. I was too lazy to add a full-blown account registration layer to organize and manage your documents, but did add Facebook login so you can do that if desired. Perhaps I’ll add other forms of login if anyone else ends up using it. Anything you put on there is public as well, for now. Here’s the permalink to some notes I took at this year’s Velocity Conference – http://markdown.me/4dfcfc5ec4fee0367a000000

  • del.icio.us
  • Digg
  • Facebook
  • Reddit
  • Twitter
  • Yahoo! Buzz
Tagged , , | 1 Comment

PHPUnit, Mocks and Closures

I like PHPUnit, and I like closures, but haven’t found many useful cases for them (closures) in PHP yet. Just recently I came across a situation where I needed a closure when working with PHPUnit Mock objects. For those not familiar with Mock Objects in regards to testing, here’s a link. It’s fairly common when creating a mock object for testing to specify what a function is expected to be called with, and what it will return in that case. Below I’m creating a mock ‘User’ object that will return 18 for the age however many times it is called.

class UserTest extends PHPUnit_Framework_TestCase{
	public function testGetAge() {
		$user = $this->getMock('User', array('getAge'))
			->expects($this->any())
			->method('getAge')
			->will($this->returnValue(18));
		$this->assertTrue($user->getAge(), 18);
	}
}

This is a pretty straight forward example of Mocks, but what happens when the value you want to return is dependent on the value it is called with, and there is some logic required to process this? This is where closures come in handy. Let’s say you have a dynamic array you’ve built up, and you want to return true if a particular function, hasValue is called with a value that exists in that array. Here’s how you can do it with Mocks and closures, using the returnCallback functionality of PHPUnit:

<?php
class UserTest extends PHPUnit_Framework_TestCase{
	public function testInArray() {
		$values = array(1,10,78,30);
		$user = $this->getMock('User', array('hasValue'))
			->expects($this->any())
			->method('hasValue')
			->with($this->anything())
			->will($this->returnCallback(function($value) use ($values){
				return in_array($value, $values);
			}));
		$this->assertTrue($user->hasValue(10));
		$this->assertTrue($user->hasValue(99) == false);
	}
}

When I call hasValue, passing in 10, we’ll get true, and with 99 it’s false. This is checked at runtime when the closure is executed, checking against the $values array. Also, stay away from Canvas Ride, it’s dangerously addictive.

  • del.icio.us
  • Digg
  • Facebook
  • Reddit
  • Twitter
  • Yahoo! Buzz
Tagged , , , | Leave a comment

PHP and an abstract Singleton

PHP is quirky. I’ve heard it called other things, but I’ll leave it at that. I had a great learning experience while writing an abstract singleton class. Static binding in PHP behaves uniquely, and is often a hurdle when you try and develop an kind of an API using inheritance and static properties. If you aren’t familiar with static binding in PHP, read the doc on late static binding and it will give you a good overview. This issue came up while creating an abstract singleton class.

Let me explain by example. Here’s my first pass at a simple abstract singleton class that I could theoretically extend from other classes, and just pick up “singleton” behavior. Disclaimer!!! Don’t use this class, it doesn’t work!

abstract class Singleton {
	private static $instance = null;
	final public static function getInstance() {
		if(static::$instance === null) {
			static::$instance = new static();
		}
		return static::$instance;
	}
	protected function __construct() { }
}

This worked, initially, until I created two classes that extended Singleton and found the second one I called behaved remarkably like the first one.

class FirstSingleton extends Singleton {
...
}
class SecondSingleton extends Singleton {
...
}

Any calls to SecondSingleton::getInstace() were really just returning the FirstSingleton instance. Since the private static $instance property is delcared on the abstract Singleton class, there was really just one $instance being stored across calls to getInstance() from various subclasses. PHP doesn’t bind the $instance property to the called class, it is bound to the class where it is declared, Singleton in this case. You could declare the $instance property on all classes that extend Singleton, but that is defeating the point of the abstract class. Here’s round two, which does work, but I have mixed feelings about:

abstract class Singleton {
	private static $instances;
	final public static function getInstance() {
		$className = get_called_class();
		if(isset(self::$instances[$className]) == false) {
			self::$instances[$className] = new static();
		}
		return self::$instances[$className];
	}
	protected function  __construct() { }
}

The change here is that instead of storing a single $instance property on the abstract Singleton class, we’re storing an array of instances, indexed by the class name of the called class. It behaves as expected, but the code isn’t as clear or clean as I would have hoped it would be.

  • del.icio.us
  • Digg
  • Facebook
  • Reddit
  • Twitter
  • Yahoo! Buzz
Tagged , | 2 Comments

Review: Yahoo! User Interface Library 2.x Cookbook

I’ve had some time to really dig in to Matt Snider’s latest book, Yahoo! User Interface Library 2.x Cookbook, and have some great things to say about it. I wanted to take a different approach to reading and reviewing this book, and hopefully it proves helpful to readers. I’ve been working on whoopdwhoop.com in my free time a lot lately, and normally as I’m developing front-end code using YUI, I reference their online documentation (which is pretty good IMO) when I have questions. I decided to just rely on this book for the past few weeks to really see how useful it was as a reference while developing. In short, it has been a great development companion. Let me explain…
I was working on adding some DataTable functionality for a client’s site, and found the chapter in Matt’s book to provide great examples and explanations for getting it going relatively quickly. He lays out multiple use cases for the DataTable, as he does so with just about all of the chapters, and that wide coverage provides great reference as I found each example provided additional insight into how to use the particular component of the YUI library. I think this approach will be a great way for beginners to ramp up to using the library, as they start fairly vanilla and basic, but as you progress the more advanced use cases and features are discussed. For a developer that is more familiar with the basics, they will quickly learn to jump a few sections in to each chapter to get to the meat of what they want to learn.

That’s one example of how I found the book to be helpful in practice. If you want a solid picture of how you will work with YUI, this book is great.

  • del.icio.us
  • Digg
  • Facebook
  • Reddit
  • Twitter
  • Yahoo! Buzz
Tagged , | 1 Comment

Awaiting Yahoo! User Interface Library 2.x Cookbook

It’s been out for awhile now, but I’ve got a copy of “Yahoo! User Interface Library 2.x Cookbook” by Matt Snider on it’s way. I’m excited to give it a read, Matt definitely knows the YUI Library from his work on Mint.

  • del.icio.us
  • Digg
  • Facebook
  • Reddit
  • Twitter
  • Yahoo! Buzz
Tagged , | Leave a comment

Page optimized by WP Minify WordPress Plugin