isotope|eleven


About_david_chapman

Facade Pattern Example

by: David A Chapman

May 22nd, 2013 12:07

After reading a great article on Sandi Metz' rules for developers. I thought I'd do a generalized example on the Facade Pattern that was used to solve the "Only instantiate one object in the controller" rule.

Generalization

The goal of the Facade Pattern is to provide a unified interface to a set of interfaces in a subsystem. This means you'd just have some object that can send back other objects. You would typically have something like this in a controller:

objects_controller.rb

class ObjectsController < ApplicationController
  before_filter :load_other_needed_objects, :only => [:index]

  def index
    @objects = Object.all
  end

  def load_other_needed_objects
    @other_needed_objects = OtherObject.all
  end
end

Using the Facade Pattern you simply create an object interface that can access all of these objects.

Observe:

object_interface.rb

class ObjectInterface
  attr_reader :objects

  def initialize(objects)
    @objects = objects
  end

  def other_needed_objects
    @other_needed_objects = OtherObjects.all
  end
end

And now the updated controller:

objects_controller.rb

class ObjectsController < ApplicationController
  def index
    @interface = ObjectInterface.new(Object.all)
  end
end

Now we're programming! :-)

About_josh_adams

Linkdump of Awesome Stuff #24

by: Josh Adams

May 10th, 2013 12:43

Plane totally has this

Linkdump image courtesy of the always-redditing David Chapman. Thanks David :)

I missed a week with the linkdumping, and for that I am truly ashamed. To make up for it, I shall provide two weeks worth of links today. Not terribly surprising I guess. To the links!

That's all I've got right now. I went ahead and embedded the slides from Sandi Metz's talk below, because everyone ought to see it.

Alright, so there's this thing called the Crazyflie that is a tiny little nano quadcopter. I posted about it in a linkdump back in February, and subsequently purchased one for us isotopes to play with. Again, that was back in Februrary - a really long time ago.

Anyway, it finally came in today (I had pre-ordered it, it's available for general consumption now but they're already almost sold out again I believe). I thought I took some excellent pictures of it as we unboxed it, but I seem to have fat fingered the button on it a few times. Anyway, here are some photos and a video of us building and playing with it anyway:

Box... In Construction Scale

Since my script for quickly getting a fresh Ubuntu install ready for Ruby development has gained some popularity, I've decided to maintain it. I've released a minor version of the setup script (version 1.0.1) that checks for things like apt and curl before trying to use them.

Installation

Remember to always read the contents of a script before running it like this:

    wget -q -O - http://goo.gl/q9vdc | bash

Source Code

dev_setup.sh

#!/bin/bash
# Version: 1.0.1
# Author: David Chapman
# Description: A quick script to get a Ubuntu machine up and running.

# The commands we'll be using are here
CURL="curl"
APT="apt-get"

# Checks that the system has apt
if ! $APT -v>/dev/null; then
  echo "You must have apt installed for this script to work."
  exit 1;
fi

# Checks if curl is installed, installs it if not
if ! $CURL --version>/dev/null; then
  echo "Installing curl..."
  sudo $APT install curl -y
fi

# Make sure you do this from time to time:
sudo $APT update && sudo $APT upgrade -y

# Here are the actual packages (feel free to contribute):
sudo $APT install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev nodejs libssl-dev libreadline6-dev libreadline-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev ncurses-term automake libtool bison subversion libcurl4-openssl-dev mysql-client mysql-server postgresql postgresql-contrib imagemagick libmagickwand-dev apache2-mpm-prefork apache2-prefork-dev libapr1-dev libaprutil1-dev redis-server vim -y

# Make default editor vim :)
echo "export EDITOR=vim" >> ~/.bashrc

# Install RVM
$CURL -L https://get.rvm.io | bash -s stable --rails --autolibs=enabled

# Source rvm
source ~/.rvm/scripts/rvm

# Source .bashrc
source ~/.bashrc

# Reload RVM
rvm reload

Building your own framework with Rack and Ruby sounds super complicated, but its suprisingly easy to get off the ground with just a few lines of code. Granted, this isn't going to be the next rails competitor, but hopefully in the process we can learn the basics of rack and expand our knowledge of web requests. I can't promise this architecture is going to handle millions of requests, but I can say that it WILL be able to handle at least YOUR request.

First things first, lets build a gemfile out so we can use bundler with our project.

# Gemfile
source "http://rubygems.org"
ruby '2.0.0'
gem 'rack', '~> 1.5.2'

Don't forget to bundle after you save the Gemfile!

Notice we're going to use Ruby 2.0 for this because its super fast and hip. So lets dive into the basics. Rack is very simple. Give it an object, it will call the "call" method on that object and pass it information about the web request. The return of that method should be an array with information about the response. Thats all there is to it. Simple huh? Ok lets build a simple class to handle this. Open up a file called request_controller.rb and insert the following lines.

class RequestController
  def call(env)
    [200, {}, ["Hello World"]]
  end
end

So lets examine our response. The first index of the array should contain the response code for the response. In this case we're sending a 200 ok back, but we could send anything in the HTTP spec (404, 500, 301, etc). The second index of the array should contain a hash with header information in it. In this case we aren't sending any headers back. The third index in the array is another array containing the response. Keep in mind that at its very core, Rails is responding in a very similar fasion to this. There is a lot of power in these few lines of code.

This class alone doesn't do anything for us though. We need to tell rack about this class, and you do that in a rack up file. We'll build one now, open up a file named brain_rack.ru and insert the following:

#!/usr/bin/env ruby
require 'rack'
load 'request_controller.rb'

Rack::Handler::WEBrick.run(
  RequestController.new,
  :Port => 9000
)

This is fairly straight forward if you're familar with ruby. The first 3 lines are the shebang, and requiring the various files we need. The last lines instantiate our RequestHandler class and tell Rack that we want to use port 9000. In this case we're using WEBrick which is built into ruby.

Now lets run the thing:

rackup brain_rack.ru

and the output should be

[2013-04-29 22:50:59] INFO  WEBrick 1.3.1
[2013-04-29 22:50:59] INFO  ruby 2.0.0 (2013-02-24) [x86_64-darwin12.3.0]
[2013-04-29 22:50:59] INFO  WEBrick::HTTPServer#start: pid=9931 port=9000

Now going to localhost:9000 in a browser should give the output Hello World! We built our first simple rack app. Stay tuned for the next parts of the series and we'll flesh out our framework more.

About_josh_adams

Linkdump of Awesome Stuff #23

by: Josh Adams

April 26th, 2013 15:04

Adam Gamble says this is an apt description of some projects

I'm constantly reinstalling my OS, not because I have problems with it, but because i'm constantly trying something new.

Remembering all the packages I needed to quickly get up and running was a pain. So awhile back, David made a script to easily install everything. A couple of things changed since then so I've updated it for anyone that is interested.

Its simple just copy this into a terminal.

curl -#L http://goo.gl/7bcjb | bash

I recommend that you always be skeptical of bash scripts that are downloaded off the tubes so feel free to look at it before you install.

It will install a bunch of packages, rvm, and ruby 2.

This should work on anything that supports apt.

Enjoy.


Meteor is an exciting and different way to build web applications. The documentation at docs.meteor.com is difficult to beat in learning to get started, but for those needing to start now, just do this:

Install Meteor:

$ curl https://install.meteor.com | /bin/sh


Install the sample leaderboard application:

$ meteor create --example leaderboard


Then read this page, and try to make changes to the app as recommended toward the bottom of the article.

Note: It might be tempting to install meteor via npm, but resist the urge. Packages within the registry aren't supported, they are outdated, and none of the example apps are included. Another recommendation: use meteorite for managing versions of meteorite, and if you're in need of a package, check out atmosphere.

I made Click! Click! Click! after playing around with the leaderboard example application mentioned above; source at github.com/heath/click-click-click.

As an example of how simple Meteor development is, here's all of the server code:


if (Meteor.isServer) {                                                                                                                                                                                                             
  Meteor.methods({
    restartGame: function() { 
      Teams.remove({});
      for (var i = 0; i < teamNames.length; i++) { 
        Teams.insert({name: teamNames[i], score: 0, winner: false});
      } 
    } 
  });
 
  Meteor.startup(function () { 
    Meteor.call("restartGame");
  });
}

I defined just a single method for manipulating server-side data: restartGame. Anytime a new game starts, this method is used, including when starting an initial game. And really, that's all there is. No worrying about data synchronization, it just happens for free.

Playing with Meteor is definitely an eye-opening experience in just how pleasant app development can be for a developer, so grab some Kool-Aid and jump onboard the Meteor ship.

About_josh_adams

Linkdump of Awesome Stuff #22

by: Josh Adams

April 22nd, 2013 08:06

Dueling Giraffes

Just getting right into it:

Did you just do your weekly or monthly brew update && brew upgrade commands and now you're getting Python ImportErrors (something like: ImportError: cannot import name MAXREPEAT)?

You're not alone, it's frustrating, and it breaks Vim and pretty much everything that uses Python.

Unfortunately Python 2.7.4 introduced some breaking changes. No worries though, I wasted time looking for the answers on how to fix it, so, let's do this.

UPDATE (5/3/2013):

So it turns out that the maintainer of the Python 2.x formulae is on it and has a fix in the pipeline. Will update this post again once an update to this formulae is final and readily available via brew. I've gone ahead and changed the steps below to just apply the patched version of 2.7.4.

First:

brew unlink python
brew remove python
brew unlink vim
brew remove -force vim

Second:

cd `brew --prefix` #should put you in /usr/local
git checkout 70e9252bd1c17a874631e030ad009b510cd72d54 Library/Formula/python.rb
brew install python #it will install a patched version of 2.7.4

Third:

brew install -v -force vim

Forth (for good measure):

brew doctor

That should do the trick. This got everything running again real nice like.

Big up to the following:

Hope this helps others that don't want to waste time troubleshooting this detremental issue.