La Maquina
Model graph traversal
Installation
Add this line to your application's Gemfile:
gem 'la_maquina'
And then execute:
$ bundle
Or install it yourself as:
$ gem install la_maquina
Usage
Pistons
Once the Engine is called it fires the Pistons, the behavior of which is entirely up to you.
So, let's say, we have a couple objects that look like:
class DannyTrejo < ActiveRecord::Base
has_many :machetes
searchable do
double :machete_sharpness, multiple: true do
machetes.map(&:sharpness)
end
end
end
class Machete < ActiveRecord::Base
belongs_to :danny_trejo
include LaMaquina::Notifier
notifies_about :danny_trejo
end
and we want to let DannyTrejo know when his Machete has been updated so that we can reindex him.
Machete is set up to fire on update, so we'll set up a listener Piston that looks like this:
class SunspotPiston < LaMaquina::Piston::Base
class << self
def fire!( notified_class, id, notifier_class = "" )
indexed_class(notified_class).find(id).index!
end
private
def indexed_class(klass)
{"danny_trejo" => DannyTrejo}[klass] or raise "can't index class #{klass}!"
end
end
end
Which finds the klass, does a find on it and fires off Sunspot index!
Setup
The setup is pretty straightforward: you do all the setting up in config/initializers/la_maquina.rb.
The 3 things you have to do are: set up the pistons(if they need configuring), install them, and configure the error_handler.
For example, if you're using the CachePiston and need to set up Redis, here's how your la_maquina.rb will look
LaMaquina::Piston::CachePiston.redis = Redis::Namespace.new(:cache_piston, redis: Redis.new)
LaMaquina::Engine.install LaMaquina::Piston::CachePiston, TestPiston
LaMaquina.error_notifier = LaMaquina::ErrorNotifier::HoneybadgerNotifier
ErrorNotifier
LaMaquina by default comes with an ErrorNotifier::Base that will explode in a very unhelpful manner. To override it, you need to change it in the config above and roll a new ErrorNotifier that responds to notify(error, details). For example, if you're using Honeybadger, you can use the included `LaMaquina::ErrorNotifiers::HoneybadgerNotifier, which looks like:
class HoneybadgerNotifier < LaMaquina::ErrorNotifier::Base
self.notify(error = nil, details = {})
Honeybadger.notify( :error_class => "CacheMachineError: #{error.class.name}",
:error_message => error.,
:parameters => details
)
end
end
If you don't care about your exceptions and want to ignore them, there's a notifier you can use, SilentNotifier, making that last line in your config/initializers/la_maquina.rb be
LaMaquina::Cinegual.error_notifier = LaMaquina::ErrorNotifier::SilentNotifier
Contributing
- Fork it ( https://github.com/[my-github-username]/la_maquina/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request