I'm writing my first rails plugin and could use a little help. In a very simplified way, I'd like to do allow the developer to specify a value which I can count through a rake task. I'm thinking of something like this...
class User < ActiveRecord::Base
monitor "Users", count
monitor "Active Users", count("activated_at != NULL")
end
- I guess monitor needs to be a class method of ActiveRecord::Base but how/where do I specify it in my plugin?
- The argument to the
monitor
function shouldn't be the value but a block of code to execute. I'm not quite sure of the best way to specify this and keep the syntax simple. Perhaps it'll have to bemonitor "Active Users", {count "activated_at != NULL"}
? - I'd prefer if the developer didn't have to specify
User.count
, justcount
, i.e. it would pick up the Class automatically (and the blocks will be called on the class not the instance). If this isn't possible, I guess there's no reason to put the monitor statements into the model (see #5). - The actual counting of these values (i.e., execution of the blocks) will be done by a rake task offline. What should the
monitor
function do to make these blocks available to the rake task? Store them in a class variable? - Perhaps the monitor statements don't need to be specified in the model at all. Maybe it clutters it up so I'd welcome any alternative places to put them.
I'm just sketching out my ideas at the moment and trying to figure out what is/isn't possible in Ruby. Any help appreciated.
Update: I'll try to be clearer on the plugin's purpose. I want the developer to be able to define metrics which should be monitored by the rake task. The rake task will iterate over those metrics and write the values to a file (I've simplified this a bit). The rake task will be very simple, something like rake monitors:update
(i.e., no params required)