Use regular expression patterns to match one or more attributes of an object with a single method: pattern_match(regex).
I owe thanks to bsampietro (Bruno Sampietro) for his example on ActsAsSearchable. See: github.com/bsampietro/acts_as_searchable
ActsAsMatchable is similar to ActsAsSearchable with the following differences:
-
Rather than full or partial text matching, ActsAsMatchable is designed to support pattern and regular expression matching against one or more attributes of a ruby object. So, you can find patterns like phone numbers, email addresses,tax ids, etc that may appear in various attributes of an object.
-
ActsAsMatchable is used to extend ActiveRecord objects however it does not utilize any SQL thus making it database agnostic.
-
Matched attributes (if any) are returned from the pattern_match method call as a hash in the format {:col_name => value}
Before you install this plugin, make sure you have the rspec-plugin-generator and rspec-rails plugins if you don’t already (and you really should ;)
script/plugin install git://github.com/dchelimsky/rspec-rails.git script/plugin install git://github.com/patmaddox/rspec-plugin-generator.git
Currently, this only exists as a Rails plugin, so:
script/plugin install git://github.com/terryjray/acts_as_matchable
class BlogComment < ActiveRecord::Base acts_as_matchable :all end # we can find phone numbers in any of the blog comment fields phone_numbers = comment.pattern_match(/\d\d\d[-]\d\d\d[-]\d\d\d\d/) # to specify certain fields (not all) just include the last after the declaration acts_as_matchable :first_name,:last_name,:description
Feel free to pitch in. You know the drill:
1. Fork the project. 2. Create a topic branch. 3. Implement your feature or bug fix. 4. Please at least add a test in the rspec file that demonstrates your addition 5. Commit and push your changes. 6. Submit a pull request.
I’ll do my best to keep up. Thx!
For an extended example, updates, and probably more detail than you’d ever need outside the source itself, you can see my announcement for the project at terryjray.blogspot.com/2011/02/introducing-actsasmatchable-plugin.html
Copyright © 2011 Centrix Information Solutions, LLC, released under the MIT license