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 on this 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 will be used to filter the objects on memory but it is not used to build the SQL query. This can be handy if we want to filter based on a field (i.e. city) that is not serialized within the yaml object within the database, but can be traversed through messaging between objects.

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.

Company.find(:last, :yaml_conditions => { :data => { :last_name => 'Marcelo' }, :flat_check => true}))

Also when you don’t want to check the hierarchy (for best performance), you can use the option :flat_check => true. Using this, you will receive the records that have the proper key/value for the leafs of the Hash structure (and it’s all handled by the database by a mix of SQL’s LIKE statements).

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.find(:first, :yaml_conditions => { :class => Delayed::PerformableMethod, :args => [ QuoteRequest.find(435), Supplier.find(57) ] })

Delayed::Job.first(:yaml_conditions => { :handler => { :class => Delayed::PerformableMethod, :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 as a wildcard.

If :handler is not provided as top level key of the hash, then the hash will be wrapped inside a handler key. For example, the following queries return the same results:

Delayed::Job.last(:yaml_conditions => { :handler => { :class => SimpleJob, :name => 'job_name', :kind => 'some_kind' }})
Delayed::Job.last(:yaml_conditions => { :class => SimpleJob , :name => 'job_name', :kind => 'some_kind' })

This wrapping magic it’s only available for Delayed::Job for now.

BTW: You can check more details about the API by looking on the specs.

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.