Nov 23
I'm not a big fan of ActiveRecord's serialize. But I have kept it together until I learned about DataMapper's custom serializers for custom types. At that point, I was ready to cry. I was saved by the following inspiration.
I've been looking for a fast (no YAML) and framework agnostic (no classname stored) way to serialize a custom object into an ActiveRecord attribute. Here's my simple hack to create custom serializers in Rails using composed_of.
class User < ActiveRecord::Base
composed_of :settings, :class_name => 'Settings', :mapping => %w(settings to_json),
:constructor => Settings.method(:from_json),
:converter => Settings.method(:from_json)
after_validation do |u|
u.settings = u.settings if u.settings.dirty? # Force to serialize
end
end
Yes. It's that simple. To clarify, after_validation self-assignment is needed to make sure that if you edit the Settings object in-place, that it gets serialized into the attribute. Note: I've tried the user.settings_will_change! option and it didn't work -- it forces the save, but not the serialization.
Now you can get crazy like a fox (ie. FriendFeed) and store schema-less data via Rails.
Nov 19
I didn't want to run & monitor a separate system to manage my periodic tasks, so I wrote a few lines of code to reschedule jobs every so often. It goes something like this.
class DjCron
def self.step(action, period, change = {})
next_at = Time.now.change(change) + period
puts "Performing #{action.to_s} and rescheduling at #{next_at.to_s}"
self.reschedule_at(next_at, action, period, change)
end
def self.reschedule_at(send_at, action, period, change = {})
method = Delayed::PerformableMethod.new(self, :step, [action,period, change])
Delayed::Job.enqueue(method, 0, send_at)
end
end
Once a single task is scheduled, it will perform the job and reschedule itself in period time. To keep it robust, rather than performing the whole job instead of the puts statement, I suggest scheduling another job. That will keep it simple and avoid any headaches with retries.
Oct 11
Today I stumbled upon Nintendo’s main North American production facility - quite random.
Aug 22
The process of running jRuby on Rails on Google App Engine is still a bit rough around the edges and I’m discovering new tricks every day. This time, I found how to improve performance of an operation by over 100%.
I was looking at Pager Duty taking unreasonably long to load the home page. Some investigation showed that the bottleneck was loading the list of latencies for the graphs, which are stored as an array of Longs in the Google App Engine Datastore. Particularly, the culprit was the conversion from a Java List to a Ruby Array.
The data comes back from the Datastore as a Java ArrayList. At first, I used:
array = list.to_a
after some experimentation, I instead tried:
array = list.toArray.to_a
Surprisingly, this increased performance of that operation by over 100%!!!
user system total real
list.to_a 1.875000 0.000000 1.875000 ( 1.875000)
list.toArray.to_a 0.916000 0.000000 0.916000 ( 0.916000)
Yes, I agree, it’s strange enough to be reported.
Aug 5
The ‘Safe Bedside Table’ has a removable leg that acts as a club and a top that doubles as a shield for self-defence. This is for people who are willing to take on an intruder, providing an extra sense of security whilst in bed.
This marks the largest threat to Clockys to date. By James McAdam.
Jul 28
The top 5% of programmers probably write 99% of the good software.
http://www.paulgraham.com/wealth.html