yaml_conditions

This is a tool for allowing queries based on serialized objects (via YAML) on relational databases (currently MySQL and Postrgres are supported).

Frameworks supported

The idea is to support multiple ORMs. Currently we are only supporting ActiveRecord (version 2.x). We are working on extending support for ActiveRecord version 3.x, and then working on the integration with Datamapper.

Usage (for ActiveRecord 2.x only for now)

Basically, we extend AR#find to allow a new option :yaml_conditions. If :yaml_contitions is not present, #find will behave exactly as it used to.

yaml_conditions parameter expects a Hash with all the key/values you want to filter the serialized object.

Below I summarized some samples so you can see how I use it.

Company.find(:all, :yaml_conditions => { :address => { :street => '5551 LEOPARD ST' } })

Company.find(:all, :yaml_conditions => { :status => :active })

Company.find(:first, :yaml_conditions => { :data => { { :street => '5551 LEOPARD ST' }, :status => :active } })

Company.find(:first, :yaml_conditions => { :data => { :owner => { :class => User, :name => 'Marcelo' } } })

Company.find(:first, :yaml_conditions => { :data => {  { :street => '5551 LEOPARD ST' }, :status => :active }, :conditions => [ 'branches_counter > ?', 3] )

As you can see from the last sample, both yaml_conditions and conditions will be merged in order to find the expected results.

Company.find(:last, :yaml_conditions => { :data => { :user => { :last_name => 'Marcelo', :address => { :city => Yaml::NotSerializedField.new('Rio Negro') } } } })

On this last sample, the city attribute can be traversed on memory but it is not saved on the serialized field (i.e. handler), so we want to indicate that to avoid building an invalid SQL.

BTW: All methods ActiveRecord::Base#first, all rely on ActiveRecord::Base#find, so we can use yaml_conditions with these methods too. As an example: Company.all(:yaml_conditions => { :data => { :address => { :street => ‘5551 LEAOPARD ST’ } } }) behaves the same way as the first sample explained below.

delayed_job

Just to give you a little background, I build this gem/plugin just to filter my YAML objects created via delayed_job plugin ;)

So, I added some custom magic for filtering Delayed::Job objects, like this:

Delayed::Job.first(:yaml_conditions => { :class => NotificationMailer, :handler => { :method => :new_registration, :args => [ User.find(1), '*', '*' ] } })

Delayed::Job.first(:yaml_conditions => { :handler => { :args => [ { :user => { :last_name => 'Marcelo', :address => { :city => 'Rio Negro' } } } ] } })

As you probably realize, ‘*’ is interpreted (for Delayed::Job instances) as a wildcard.

Installation

$ ruby script/plugin install git://github.com/marklazz/yaml_conditions.git)

OR install it as a gem

$ [sudo] gem install yaml_conditions

Enjoy!

License

Copyright © 2010 Marcelo Giorgi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.