Archive for the 'Ruby' Category


Updates for Retaste.me 0

Retaste.me was a few weeks not delivering any emails. I apologize for it. The site was constantly after 5 requests throttled by delicous. Last week I was finally able talk to delicious developer. He explained me why I got throttled that many times and what to do to prevent it. I applied all their advice and deployed new version of retaste.me app. I also added new look and feel for email and implemented delete functionality. Enjoy!

StackBuffer.com – awesome StackOverflow interface 0

I have just released my new “exprimental” project. Its name is StackBuffer.com. StackBuffer.com is web interface for StackOverflow.com API based only on HTML5, CSS3 and JavaScript. There’s no backend. Here’s screenshot how it looks like:

stackbuffer.com

StackBuffer | awesome StackOverflow interface

StackBuffer is open source project providing HTML5 and CSS3 interface for StackOverflow API. This project is actually experiment trying to prove that you can write only HTML5, CSS3 and JavaScript without need to do any backend programming. This app is targeted mostly to developers to see what can be done in short period of time with new HTML5 and CSS3. Consider it example application.

You can also find the description at stackapps.com: http://stackapps.com/questions/972/stackbuffer-awesome-stackoverflow-interface-html5-css3

I would appreciate very much any kind of feedback.

Bootstraping Chef Server/Client with Sprinkle 0

I like very much Chef tool for server configuration. I think it’s very good tool and every company doing web development should use it to bootstrap or configure their servers. But the sad truth is that before you can use Chef you need to prepare the machine with manual typing and configuration. And that’s what I don’t want to do. So I decided to change that by automating the Chef bootstrap with Sprinkle.

1) Download the code:

1
git clone git://github.com/lacomartincik/chef-bootstrap.git

2) Have ready your server or virtual machine
3) Create config file

1
cp config/config.yml.example config/config.yml

4) Set up the config with IP and user credentials

1
2
3
4
5
6
7
8
9
10
server:
  host: chef.server.local
  ip: 192.168.0.1
  user: deploy
  password: deploy

client:
  ip: 192.168.0.2
  user: deploy
  password: deploy

5) Run server/client bootstrap

1
./bin/chef-server
1
./bin/chef-client

Hope you find it useful. Any comments, recommendations always welcomed!

Replacing Ruby on Rails fixtures with Factories and Builders 1

In my current job we were facing a well known problem called Fixtures. The term Fixtures in context of Ruby On Rails framework is the default way how the testing data are prepared/created before the tests run.

Here’s the list of important reasons why Fixtures are pain:

  1. Hard to read (YAML format)
  2. Hard to define edge cases
  3. Almost impossible to modify after reaching certain size of fixtures

The only real reason for using fixtures which I can find is speed. So if you’re on small project or you really need to have tests running fast then the fixtures are good choice. But if you need to have easy to read tests with test data, possibility to easily modify them and having be able to define a lot of edge cases here’s one nice possible way to go called Design Patterns.

Factory patterns (http://en.wikipedia.org/wiki/Factory_pattern)
In these days people are already using Factory pattern as replacement for Fixtures and you can find plenty libraries to help you simplify the work:

At the end we decided to go for FactoryGirl because it’s widely used which means there’s a lot of people to help us with answering questions and fixing bugs.

Example of FactoryGirl:

1
2
3
4
5
6
7
8
9
Factory.define :invoice, :class => Invoice do |u|
  u.created_at 2.days.ago
  u.started_at 2.months.ago.beginning_of_month
  u.ended_at 2.months.ago.end_of_month
end

Factory.define :invoice_with_billing_cycle, :parent => :invoice do |u|
  u.association :billing_cycle, :factory => :billing_cycle_2_months_ago
end

Personally I also like very much Foundry by Jeremy McAnally. The only disadvantage is that it’s closely linked with Ruby On Rails named_scopes implementation.

Builder pattern (http://en.wikipedia.org/wiki/Builder_pattern)
This pattern isn’t yet widely used as Factory in Ruby On Rails community. I really don’t know why, but I didn’t find any article about using it so here we go.

I like to use this design pattern in situations when:

  • You need to create complex object structures (together with Factory)
  • You need to be flexible with creating many edge cases

The best thing about using Builder pattern is that you don’t need any library but just pure Ruby objects.

Example of Builder pattern in Ruby combined with Factory girl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class InvoiceBuilder
 
  def initialize(invoice_factory, options = {})
    @invoice_factory = invoice_factory
    @options = options
    self
  end
 
  def with_line_items(line_items)
    @line_items = line_items
    self
  end
 
  def create
    invoice = Factory(@invoice_factory.to_sym, @options)
    @line_items.each do |li|
      li_factory, li_options = li
      invoice.line_items << Factory(li_factory.to_sym,
       li_options.merge(:invoice => invoice))
    end
    invoice
  end
 
end

The code above shows very flexible way of using Builder pattern to chain your criteria as you need them. You can than write something like this:

1
InvoiceBuilder.new(:invoice_with_billing_cycle).with_line_items([ item1, item2 ]).create

Interesting articles to read:

Things to consider:

  • Try to keep your tests and Factories simple
  • Keep your data very close to your tests
  • Create complex structures with Builder pattern

Thanks for reading. Let me know what you think in comments. Any kind of feedback appreciated!

Using Null Object pattern with Ruby on Rails 1

While back ago I’ve released plugin to implement Null Object Pattern. And I’ve decided to write about it. Mostly because people are not using it or don’t know it.

What it does?

Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), one uses an object which implements the expected interface, but whose method body is empty. The advantage of this approach over a working default implementation is that a Null Object is very predictable and has no side effects.

Why should I use it?

  • You will not have to prevent your code from having null reference. For example you will not have to do this:
    1
      print post.user.nil? ? 'Null user' : post.user.name

    With Null Object pattern you write just print post.user.name and you’ll get default value for Null object defined for this attribute.

  • It’s very handy if you’re doing some kind of reporting that you don’t have to deal with null reference and you have object (in SQL row reference) instead.

To install the plugin

1
ruby script/plugin install git://github.com/lacomartincik/acts_as_nullobject.git

Let’s have a look how to use it
First you’ll need to define model to acts as null object:

1
2
3
class User < ActiveRecord::Base
  acts_as_nullobject :login => 'lacomartincik', :name => 'Ladislav Martincik'
end

Than whenever you call on User class method null_object you will get instance of NullUser class with default attributes defined in User:

1
2
>> User.null_object
=> NullUser...

Now if you define relationship between say Project and User like this:

1
2
3
class Project < ActiveRecord::Base
   belongs_to_with_nullobject :user
end

Than whenever you create new Project object with null reference to User you will get NullUser instance instead of nil:

1
2
3
>> p = Project.new
>> p.user
=> NullUser

The project is available as plugin on http://github.com/lacomartincik/acts_as_nullobject.

Enjoy! Any suggestions, bug reports and comments welcomed!

Update: I had to rename plugin name from acts-as-nullobject to acts_as_nullobject otherwise it’s not going to work in Rails!

Next Page »