Call Montréal, Canada 524-949-6869

Blog

Thursday, May 28, 2015

Learning OO Design through Design Patterns

Last week at Montreal.rb there was an open discussion on RailsConf held in Atlanta which I had attended this year. We discussed favorite talks, themes and general experiences. I prepared a few notes prompting folks to explore design patterns after seeing Sandi Metz's talk, Nothing is Something. Why?

In the first half of Sandi's presentation she presented an example of using the Null Object Pattern as an example of sending messages to resolve her adversion to conditionals. During this portion of the presentation, she made an interesting comment explaining how she (re)discovered this pattern herself and then presented it to her co-worker. Sandi's co-worker described as "being a little more well read", told Sandi that this pattern had a name, the "Null Object Pattern". (OK, so I've cheated a little here. Sandi discusses this in her Bath Ruby version of her talk, not in the RailsConf version.)

Sandi goes on to suggest that we don't need to solve specific problems ourselves, as others have already solved these for us. She doesn't mention this explicitly but I interpreted this as Design Patterns. It just requires a little bit of research and reading. But where to start?

Two Book Recommendations

The classic Gang of Four Design Patterns book maybe a little intermediating for some, I know I've never found it appealing. However, there are some good alternatives. I recommend two.

The first book that really helped enter the world of design patterns was Design Patterns Explained by Shalloway and Trott. As the sub-title suggests, "A New Perspective on Object-Oriented Design", explains design patterns in the context of good OO design.

Likewise, Head First Design Patterns, also describes design patterns in the context of OO design, connecting design patterns with principles such as "Encapsulate what varies" and "Favor composition over inheritance". The first chapter is a great example of presenting these principles first in an example, and then identifying it as the Strategy Pattern. It removes the notion of using a pattern for patterns sake and focuses the discussion on solving problems, using principles and proven solutions.

The Head First series of books maybe off putting to some. Personally I've never been attracted to the series, but the Design Patterns book they have published, I feel, is first class. The examples they use really stick in your head and a great lever for accessing the patterns in the future. I've also heard good things about the Object-Oriented Analysis and Design book as well, although I haven't read it. (If I remember correctly, I think Sandi mentioned this title when I attended her course in Durham; however I not suggesting this is an endorsement of the book by her.)

Don't fear patterns, use appropriately

So rather trying to solve problems that have already been solved, I recommend investigating Design Patterns and these books are a great introduction. Design Patterns in the past do have a bad rap in the Ruby community, but I think that is no longer the case as the community matures. The original adversion to design patterns maybe due to the overuse within the Java community. Please remember, each design pattern needs to be used appropriately, and always consider a simpler approach.

I believe these book recommendations would be a good follow-up to Sandi's book Practical Object-Oriented Design in Ruby, to help triangulate your understanding of OO design.

Let me know how it goes!

Sunday, May 24, 2015

Rebecca Wirfs-Brock on Object Design

Building an object-oriented application means inventing appropriate machinery. We represent real-world information, processes, interactions, relationships, even errors, by inventing objects that don't exist in the real world. We give life and intelligence to inanimate things. We take difficult-to-comprehend real-world objects and split them into simpler, more manageable software ones. We invent new objects. Each has a specific role to play in the application. Our measure of success lies in how clearly we invent a software reality that satisfies our application's requirements -- and not in how closely it remembers the real world.

-- Rebecca Wirfs-Brock, Object Design

I pulled this quote from Avdi Grimm's excellent keynote presentation, The Soul of Software as a reminder this very important idea on object-oriented design: that we invent new objects that don't have a corresponding object in the real world. If we limit our domain objects to those that are represented in the real world, we miss opportunities to encapsulate variation in behaviour within our application.

Friday, June 20, 2014

Talk: Don't Commit Your Secrets

On Tuesday this week, I presented a talk to Montreal.rb on keeping your application secrets safe. Posted below is the video, slides and some follow-up questions during and after the presentation. Enjoy!

Video


Slides


Questions

How can I remove a secret from your Git repository?
GitHub has an excellent post on how to do this: Remove sensitive data. However, if you have committed a secret to your repository which has been push to a remote, then consider that secret compromised. You will need to reset the secret (i.e. create a new API key) and configure your application using the techniques described above.

How can I keep my secrets safe with an application pre-Rails 4.1?
Check out these options:

  • foreman: recommended - any Rack app
  • dotenv: great for plain old Ruby Gems too
  • figaro: YAML
  • capistrano: linking to a configuration file in production

Tuesday, April 22, 2014

Modeling on the Right Side of the Brain

Update: The presentation is now online. Unfortunately the color has been washed out when the slides were filmed, so I highly recommend you follow along with the slide deck below. Enjoy!




If you attended my talk at RailsConf, Modeling on the Right Side of the Brain, thank you so much for your support and I hope you found the talk helpful. Embedded below are the slides for my talk and resources listed below that you will find helpful in learning more about Object Modeling.

If you have no idea what I'm talking about, here is the abstract from my talk that I gave at RailsConf 2014 in Chicago.

"Since your first web application, you have struggled with identifying domain objects. Assigning business rules and services appears to be a talent that only other developers are born with. Fear not! Object Modeling is a learnable, teachable skill. This talk demonstrates the five essential skills you need for modeling objects and their responsibilities. Think beyond ActiveRecord and your database, and learn how color and patterns will help you explain, maintain and extend your application."


While the book I referenced in the talk Java Modeling in Color with UML is no longer in print, it can be sourced from Amazon if you wish to purchase a used copy. However, there is an excellent on-line resource I highly recommend authored by Stephen R. Palmer titled Peter Coad's 'Modeling in Color' which will provide all you need to get started immediately.

Books

Listed below are the three key books that got me excited (and continue to do so) about Object Modeling:

Java Modeling in Color with UML

Object Models: Strategies, Patterns, and Applications

Streamlined Object Modeling: Patterns, Rules, and Implementation

Online Resources

Peter Coad's 'Modeling in Color' authored by Stephen R. Palmer. As mentioned above, this is a great online resource to get you started today.

Streamlined Object Modeling is the book site hosted by one of the authors Mike Abney. While this is not a stand alone resource, as it does require the book to introduced some of the concepts discussed, it does provide you some insight on this specific approach buidling on Peter Coad's techniques.

Please keep in touch

I plan on expanding on this topic in the future. If you are interested please sign up so I may keep you abreast of new developments.






If there is anything else you need, please feel free to contact me directly or leave a comment below.

Wednesday, October 23, 2013

Installation instructions for Quarto on OSX

I was trying out Avdi Grimm's Quarto a week or so back for ebook generation. The toolchain looks very promising, but has a few dependencies that need to be installed before you can start publishing. These are listed in the README.

Here's a quick guide in getting those installed on OSX (Mountain Lion). Note I already had the following setup:

  • Git
  • HomeBrew
  • pygentize
  • xmllinit

Command line instructions:




The big one here is to ensure ~/.cabal/bin is in your path when you run the rake task to publish.

Tuesday, October 8, 2013

Vendoring Binaires on Heroku: An Example with Aspell

Recently I needed to install the Aspell library on Heroku for a side project. By default a Heroku Dyno doesn't include any non-essential libraries so your left to install these on your own. This introduced me to the world of buildpacks and an awesome build tool that Heroku provides to get the job done.

Objective

The objective is to have a Rails application running with FFI::Aspell, a FFI binding for the Aspell library on Heroku.

Heroku Buildpacks and Vendoring Binaries

First, we need to configure Heroku to vendor binaries. The easiest way to do this is use the Vendor Binaries buildpack which allows you to extract a tarball stored on S3 into your /app directory when the application build process is triggered. This is all configured in a .vendor_urls located in the root of your application.

As Heroku only supports a single buildpack by default, we need to configure it to support multiple buildpacks. In our case, we need the Ruby buildpack in addition to the Vendor Binaries buildpack. Fortunately this is relatively easy to do when we create our new application (and can be done after the fact as well):

heroku create --stack cedar --buildpack https://github.com/dollar/heroku-buildpack-multi.git

Now we have our multi buildpack application, we can configure those buildpacks in .buildpacks:

https://github.com/peterkeen/heroku-buildpack-vendorbinaries.git
https://github.com/heroku/heroku-buildpack-ruby.git

For our vendored binaries, although we haven't built it yet, we can configure it's location on S3 in .vendor_urls:

http://your-bucket.s3.amazonaws.com/aspell-0.60.6.1.tar.gz

Commit both the .buildpacks and the .vendor_urls files to your Rails application.

Build the binary

Next we need to build the binary. First, let's download and extract the source into a temporary working directory (not in your Rails application).

mkdir ~/Code/temp && cd ~/Code/temp
curl -o aspell-0.60.6.1.tar.gz ftp://ftp.gnu.org/gnu/aspell/aspell-0.60.6.1.tar.gz
tar -xvzf aspell-0.60.6.1.tar.gz

Now, let's create a build server on Heroku. The Vulcan gem automates the process of creating the server and building the library.

gem install vulcan
vulcan create vulcan-yourname

Now we're ready to build the binary:

vulcan build -s ~/Code/temp/aspell-0.60.6.1 -p /tmp/aspell -c "./configure --prefix=/tmp/aspell && make install"

You will notice that we configure the library to be installed in /tmp/aspell using the prefix option. We also need to tell Vulcan using the -p option where the compiled library will be located. At the completion of the build, a tarball is then download to your /tmp directory.

You can now copy the tarball from /tmp/aspell-0.60.6.tgz to the S3 bucket specified in .vendor_urls. Ensure the read permission on the uploaded file can viewed by "world", otherwise it won't be accessible when you deploy your application and you will receive the following errors:

-----> Found a .vendor_urls file
       Vendoring http://yourbucket-heroku.s3.amazonaws.com/aspell-0.60.6.tgz

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Exiting with failure status due to previous errors

 !     Push rejected, failed to compile Multipack app

Build the supporting dictionary files

Typically we would be done at this point, but we need to compile the dictionary files that Aspell uses separately. This makes the process a little more complicated.

We're going to use our Vulcan build server to compile the dictionary files. This requires starting up a shell, downloading the Aspell tarbar we built previously and extracting it into the original installation directory.

heroku run bash --app vulcan-yourname
cd /tmp
mkdir aspell && cd aspell
curl -o aspell-0.60.6.tgz http://yourbucket-heroku.s3.amazonaws.com/aspell-0.60.6.tgz
tar -xzvf aspell-0.60.6.tgz

Now we're ready to build the dictionary files:

export PATH=$PATH:/tmp/aspell/bin
curl -o aspell6-en-7.1-0.tar.bz2 ftp://ftp.gnu.org/gnu/aspell/dict/en/aspell6-en-7.1-0.tar.bz2
tar -xvf aspell6-en-7.1-0.tar.bz2
cd aspell6-en-7.1-0
./configure && make install

At this point you will see output similar to this:

/tmp/aspell/bin/prezip-bin -d < en-common.cwl | /tmp/aspell/bin/aspell  --lang=en create master ./en-common.rws
/tmp/aspell/bin/prezip-bin -d < en-variant_0.cwl | /tmp/aspell/bin/aspell  --lang=en create master ./en-variant_0.rws
/tmp/aspell/bin/prezip-bin -d < en-variant_1.cwl | /tmp/aspell/bin/aspell  --lang=en create master ./en-variant_1.rws
/tmp/aspell/bin/prezip-bin -d < en-variant_2.cwl | /tmp/aspell/bin/aspell  --lang=en create master ./en-variant_2.rws
...

Once the build is completed you can test Aspell is working correctly with:

echo "helloz" | aspell -a

We're almost there. Now just copy the dictionary files over to our original build for Aspell in /lib/aspell-0.60/ We will create a new tarball which includes the dictionary files:

cp *.rws *.alias *.multi *.dat ../lib/aspell-0.60/.
cd ..
tar -czvf aspell-0.60.6.tgz bin include lib share

Finally, transfer the new tarball aspell-0.60.6.tgz to your local machine and upload to S3 again replacing our original tarball.

Deployment

Before deploying you will need to configure LD_LIBRARY_PATH, the path used by the dynamic loader to load libraries into dynamically linked executables. Otherwise, the following error is raised:

aspell: error while loading shared libraries: libaspell.so.15: cannot open shared object file: No such file or directory

This is easy to do:

heroku config:add LD_LIBRARY_PATH=/app/lib

Now let's deploy our application. Remember to include the FFI::Aspell in our Gemfile and bundle. As long as you have your application setup correctly with Heroku you can then:

git push heroku

On a successful build your output will begin with the following (note the vendoring of http://your-bucket.s3.amazonaws.com/aspell-0.60.6.tgz):

-----> Fetching custom git buildpack... done
-----> Multipack app detected
=====> Downloading Buildpack: https://github.com/peterkeen/heroku-buildpack-vendorbinaries.git
=====> Detected Framework: VendorBinaries
-----> Found a .vendor_urls file
       Vendoring http://your-bucket.s3.amazonaws.com/aspell-0.60.6.tgz
=====> Downloading Buildpack: https://github.com/heroku/heroku-buildpack-ruby.git
=====> Detected Framework: Ruby/Rails
-----> Using Ruby version: ruby-2.0.0
-----> Installing dependencies using Bundler version 1.3.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
       Fetching gem metadata from https://rubygems.org/..........
       Fetching gem metadata from https://rubygems.org/..
       Installing rake (10.1.0)

Test the FFI::Aspell Gem

The moment of truth! Let's run a Rails console to test the FFI:Aspell gem.

heroku run console

At the Rails console:

speller = FFI::Aspell::Speller.new('en_US', 'dict-dir' => '/app/lib/aspell-0.60')
speller.correct?('cookie') # => true

Note we need to specify the dict-dir option since the Aspell library looks for it in /tmp/aspell/lib/aspell-0.60 by default (based on the prefix option we used when we compiled it). If you were to execute the binary directly from the shell you would also need to include this option. For example:

echo "helloz" | aspell -a --dict-dir /app/lib/aspell-0.60

If you don't include the dict-dir option, the FFI:Aspell library will crash.

Troubleshooting

If you have problems, it best to try to run the Aspell library directly from the shell using the example command above.

If you receive the following error:

Error: No word lists can be found for the language "en_US".

Then the path to the dictionary files is not correct.

References

Talk: Meet Trello

A couple of months ago I gave a presentation on Trello for a lunch and learn at Dynamo. Trello is a collaborative project planning tool that I was trying out at the time. This presentation shares my findings on how to use Trello for project management in an agile development context. Enjoy!

Meet Trello from DynamoMTL on Vimeo.

Developing Ruby on Rails applications and custom Content Management Systems in Montréal. Contact Firsthand.