Thoughts on Software and Technology

A Redis-backed acts_as_follower gem

This entry is part 1 of 1 in the series Redis Gems
  • A Redis-backed acts_as_follower gem

I usually start blog-based introductions to gems and libraries that I write with a narrative. Lately, however, I’ve been getting busier keeping up with the work I need to do for clients and that I need to do for my startup. So I’ll leave the narrative to a minimum, a very short question.

You love the acts_as_follower gem, right? Sure, everyone does. Wouldn’t it be better if it was based in Redis? Sure, of course it would.

This weekend I found the great Amico gem, a low-level Ruby gem for Redis backed followers, and I decided to create Acts_as_Amico, a Rails injectable gem that uses that back-end ability, so now you can have a followers gem that is completely in Redis right in your Rails app.

The code has full-ish, but ever expanding, documentation on the Github page, but here’s a quick rundown

ActiveRecord Objects

class User < ActiveRecord::Base
 acts_as_amico
end

usera = User.create
userb = user.create

usera.follow! userb
=> nil

usera.following? userb
 => true

Advanced Usage

class Admin < ActiveRecord::Base
  acts_as_amico :amico_key => :name
  validates_uniqueness_of :name  # -> do this or be sorry
  validates_presence_of :name # -> this too, you've been warned
end

usera = User.create

puts usera.id
 => 18

admin = Admin.create :name => "frank"

usera.follow! admin
 => nil

admin.follow! usera
 => [1, 1]

admin.followers
 => ["18"]

usera.followers
 => ["frank"]

ActiveResource Objects

class RestObject < ActiveResource::Base
  self.site = "http://mettadore.com/junk"
  acts_as_amico :amico_key => :title
end

usera = User.create

rest_object = RestObject.find(123)

rest_object.title
 => "Bread and Circus"

usera.follow! rest_object

usera.following? rest_object
 => true

usera.following
 => ["Bread and Circus"]

Issues

I have every expectation that there will be problems and bugs. If you find one, please don’t hesitate to file an issue if you’re on Github, or send me a message on Google+ if you’re not.

Comments are closed.

Powered by WordPress | Designed by Elegant Themes