I published my very first gem on RubyGems today and the source code is located on GitHub at https://github.com/aboghosian/looksy. Check it out when you get the chance. The gem is the result of a project we were working on in which we wanted a centralized way to cache our look up tables so we didn’t have to hit the database each time to retrieve them (mainly because they changed very infrequently).
Looksy allows you to place a caching layer between your look up tables and ActiveRecord classes. Here is how it works:
class PaymentType < ActiveRecord::Base include Looksy::Cacheable end # retrieves all records from cache or populates cache from db PaymentType.fetch_all # retrieves record matching id PaymentType.fetch_by_id(id)
You also get dynamic methods just like ActiveRecords dynamic finders, except instead of using find to prefix your method calls you use fetch.
class PaymentType < ActiveRecord::Base include Looksy::Cacheable # attributes include :name and :status end # retrieve record by name PaymentType.fetch_by_name("CreditCard") # retrieve all records by status PaymentType.fetch_all_by_status("active")
You can chain multiple attributes together as well.
class Coupon < ActiveRecord::Base include Looksy::Cacheable # attributes include code, status and association_type end # retrieve record by code and status Coupon.fetch_by_code_and_status('100PERCENTOFF', 'active') # retrieve all records by status and association type Coupon.fetch_all_by_status_and_association_type('inactive', 'whitelist')