We're hiring!

From the monthly archives:

April 2010

Monk updated to use Sinatra 1.0

by Michel Martens on April 13, 2010

Monk’s default skeleton now ships with Sinatra 1.0 and the latest versions of Webrat and Ohm.

It also includes tasks for starting and stopping Redis, and overall should provide more hints about what to configure and where.

To try it, just run monk init myapp, then cd myapp and monk to check the available options.

If you never installed Monk, run sudo gem install monk. If you already have it, there’s no need to update the gem.

{ 1 comment }

Mixing Ohm with ActiveRecord, DataMapper and Sequel

by Michel Martens on April 12, 2010

There’s one nice side effect in the way Ohm and Sequel find records: they use [](id), which is also a way to call procs in Ruby.

So, how would you declare a reference to a DataMapper model or a set of ActiveRecord instances within Ohm?

class User < ActiveRecord::Base
  def posts
    Post.find(user: self)
  end
end

class Comment
  include DataMapper::Resource

  property :id,          Serial
  property :body,        String
end

class Post < Ohm::Model
  attribute :body
  reference :user, lambda { |id| User.find(id) }
  list      :comments, lambda { |id| Comment.get(id) }
end

Each time Ohm needs to refer to User, it will call the lambda the way it calls other Ohm models.

>> user = User.create :name => "foo"
>> post = Post.create :user => user, :body => "bar"

>> post.user.name
=> "foo"

>> comment = Comment.new
>> comment.attributes = { :body => "baz" }
>> comment.save
=> true

>> post.comments << comment
>> post.comments.first.body
>> "baz"

And what about Sequel? Because Sequel behaves exactly like Ohm, there’s no need for a lambda. You can mix them directly.

{ 0 comments }