Tuesday, 22 January 2013

Git Alias & Ignore

Quick Reference for Git Aliasing and Ignoring

Aliasing:


$ cd ~
$ vim .gitconfig

Add the following (and any additional shortcuts you want):


[alias]
ci = commit
st = status
co = checkout
lc = log ORIG_HEAD.. --stat --no-merges

Ignoring

(This is mine for a rubymine project)

Thursday, 10 January 2013

Network JSON request in iOS


I had to dig a little to bring all the components together necessary for something like this so I thought it best to document it.

For the request you need: 

1. Create an NSMutableRequest
2. Create an NSURLConnection with the above request
3. Set the class encapsulating this behavior as the delegate to the connection
4. Implement the call back methods the connection makes based on the result it gets (so you can decide what to do)

Monday, 7 January 2013

iOS 6 Facebook Integration

<script src="https://gist.github.com/4161726.js?file=AppDelegate.m"></script>

<script src="https://gist.github.com/4161735.js?file=AppDelegate.m"></script>

Add the plist item 'FacebookAppID'

Add the entry to URL types.

Knock this into a delegate method.

<script src="https://gist.github.com/4161746.js?file=gistfile1.txt"></script>

<script src="https://gist.github.com/4161750.js?file=gistfile1.m"></script>

Thursday, 3 January 2013

Configuring Environment Variables iOS

Say you've got an iOS app talking to a backend api. Maybe this api runs as localhost on your machine when you're in development and maybe you also have staging and production environments out in the cloud somewhere. What's the best way to set this up?

I've gone with an encapsulated pList... setup as follows.

This is my pList file.
I encapsulate that in a class called EnvironmentVars.

After which, I can access it as follows from anywhere. This is an example of using it to create a NSUrl object.
Objective C code can be terse. I find it's best to exercise encapsulation to the extremist point to maintain sanity.

Monday, 29 October 2012

Rails App from Scratch (On fresh Lion installation)

I'm setting up a new Mac Mini for Ruby on Rails development. It was pretty much headache free. I knew I needed my app to use MySQL so I wanted to set things up to incorporate this. Here's the list of components that need to be installed in the order shown.
  1. Xcode (with Command Line Tools)
  2. Homebrew
  3. MySQL & MySQL Workbench
  4. RVM (installing lastest Ruby 1.9.3)
  5. Install gems to against 1.9.3 gemset: rails, bundler, mysql2, activerecord-mysql2-adapter
  6. Rubymine (My editor/IDE of choice for RoR development)
Once all of these are installed, you can use Rubymine to create a new rails project, configure your database.yml file, generate a model (with a migration), and run rake db:migrate to confirm that all connections are up and running. We'll step through this at the end.

Install Xcode


Best done through the Mac App Store. Launch the App Store and search for xcode. Follow directions to install it.

Once Xcode is installed you need to install the Command Line Tools. This gets you lots of command line goodies we'll need for ruby and rails development. (One main one being the gcc compiler, integral for running Ruby.)

Within Xcode, go to Preferences > Downloads > Componenents. From here, you can install the 'Command Line Tools'.

Install Homebrew


Homebrew is a package manager for Mac OSX. It's currently quite popular and I like it as it seems to work the most smoothly with the latest components new in Lion. Here's a comparison between the different options in this area.

Lion ships with Ruby installed, so we can use this default version to install Homebrew. Run this in your terminal: 

$ ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

Install MySQL


Super easy now that we have Homebrew.

$ brew install mysql

When this finishes it gives you some instructions of a few things to do to set up the root account and different configuration options for startup, etc... read these and do what you think you need to do. I'm going to configure it to start when the system starts

$ mysql_install_db --verbose --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.15/com.mysql.mysqld.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

You may still need to manually start it at this point before you can log in.

$ cd /usr/local/opt/mysql ; /usr/local/opt/mysql/bin/mysqld_safe &

Now, login.

$ mysql -uroot

If this is successful, you're almost there, the only thing left is to install the MySQL WorkBench!

Create a connection using the default configurations: user: root, host: 127.0.0.1, port: 3306. Now you can create databases against this connection through this interface. We'll do this later though!

Install RVM


I used this as a step-by-step installation guideline.

But, basically... for installation. You just need to run this from your terminal:

$ curl -L get.rvm.io | bash -s stable

Now we can install the latest Ruby (or which any you need) First, list all the ruby versions available for installation:

$ rvm list known

I'm going to install the latest version, 1.9.3.

$ rvm install 1.9.3

Before we start installing gems, lets tell RVM the version of ruby we want to install gems against.

rvm use 1.9.3

Installing Gems for Rails Development using MySQL


$ gem install rails
$ gem install bundler 
$ gem install mysql2
$ gem install activerecord-mysql2-adapter

Install RubyMine create a new Rails project


Once RubyMine is installed, launch it and create a new project. 
  • Make sure to specify the project type as 'Rails Application'.
  • Select the ruby version you want to use. 
  • If RubyMine doesn't know the Rails version it should use, you will need to click the '...' box next to the Rails Version dropdown. RubyMine will go and get it!
  • Click 'ok' and we're ready to go!

Configure your database.yml File


It should look something like this:

Test that everything is up and running.


$ rails g model user
      invoke  active_record
      create    db/migrate/20121029132052_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/unit/user_test.rb
      create      test/fixtures/users.yml

Edit your migration file to reflect the columns you'll need in your table. If all this doesn't make sense, spend some time researching ActiveRecord and Rails database migrations.

If you need a cheat sheet to help with defining your columns, check this out.

Now you should be able to run this:

$ rake db:migrate


BOOM. Easy Peasy.


Friday, 26 October 2012

Post 4.3 Xcode & Command line tools

I'm currently getting new mac mini ready for Ruby on Rails, iOS and Android development. I had Xcode 4.3 installed by default but I didn't have the standard /Developer directory where you get all the lovely command line tools used for Ruby development. Some of my gem installs were working, but some were not. Was seeing stuff like this from the terminal:

$ make
-bash: make: command not found

Weird, right? Where are my command line tools?

Turns out, in Xcode 4.3 and later, you have to specifically install these and they aren't in the /Developer  directory.

Once Xcode is installed. Go into Preferences > Downloads Tab > Components Pane

From there install Command Line Tools.

Boom. Terminal works.

Tuesday, 11 September 2012

Guava's EventBus and Android's async tasks

Android requires that you do all network processing on non-UI threads. This is good business as it means unexpected exceptions aren't going to crash your app. It can be a little problematic when you need to be notified of the different outcomes of your various network activity. You need to notify an activity, it needs to notify one of it's components.

The Callback Approach

This is essentially like handing a reference to the caller of the process. The process then calls an appropriate method on the caller, depending on the outcome.


The Guava EventBus Approach

This approach decouples the caller from the process. The caller can register for different events on the event bus after which, any process can put that event on the event bus. The registered processes with get notified.

You have to define those events but they can be empty or you can wrap anything you need in them.
Then all you have to do is subscribe for the event from the activity. It doesn't matter what you call the method... what matters is the parameter must match an event that will be put on the message bus.

Why is the event bus better? 

Well it decouples the method responding to the outcome of the network activity or whatever from the task doing this. You don't have to pass a reference of the thing doing the responding to the task. Another benefit here is that anything can register to respond to this event. Be it the activity, or one of it's fragments, it's action bar, or whatever. Nice!