ajorgensen
ajorgensen

Reputation: 5151

Ruby event triggers

I am looking for a data structure or design pattern to work with rails and active record to provide a way to make configurable events and event triggers based on real time events.

While this is not the end usage for this sort of system, the follow example I believe demonstrates what I am trying to do. Similar to an log monitoring system like splunk, essentially what I am trying to do is create a system where I can take some attribute from an object and then compare it to a desired value and take perform an action if the evaluation is true.

Is there a library to do this or would something need to be rolled out from scratch. The way I was thinking about designing this would be similar to the following:

I would have an Actor (not in the traditional concurrency sense) which would house the attributes that I want to compare to. Then I would have a Trigger model which would have a pointer to the actor_id, attribute (IE count), comparator (<, <=, ==, >=, >), value, and action_id. The action_id would point to an object with a perform method that would just house the code that needs to run when the trigger is fired.

So in the end the trigger would evaluate to something like:

action.perform if actor.send(attribute) comparator value

Another option, possibly a more standard one, seems to develope a DSL (IE FQL for facebook). Would this be a better and more flexible approach.

Is this something that a library can handle or if not is this a decent structure for a system like the one I am proposing?

EDIT: Looks like a DSL might be the most flexible way to go. Tutorials for writing DSL in Ruby

Upvotes: 1

Views: 752

Answers (1)

Mark Tabler
Mark Tabler

Reputation: 1429

If I understand the question correctly, what you have written is nearly correct as it stands. You can send comparison operators as symbols, like so:

comparator = :>
action.perform if actor.send(attribute).send(comparator, value)
# action will perform if actor's attribute is greater than value

Upvotes: 1

Related Questions