CompanyBook / massive_record

HBase ruby client
130 stars 39 forks source link

extract_and_cache :attribute, :from => referenced_object (delegation with cache) #70

Open thhermansen opened 13 years ago

thhermansen commented 13 years ago

Multiple models in Companybook's Rails application caches some attributes from referenced records. For instance, let's say you have a person which has one car and multiple addresses. We want to be able to read the car's color to present in the view without actually loading the car. We could have something like:

class Person < MassiveRecord::ORM::Table
  references_many :addresses
  references_one :car

  #
  # This generates reader methods like person.car_color, which reads from a cache,
  # not loading up the actual car object to read color from it.
  #
  extract_and_cache :color, :from => :car

  #
  # Since addresses is representing a collection this would iterate over
  # all addresses-records on save and collect zip codes and cache them
  # so when you call person.addresses_zip_codes you'll accessing that
  # cached array.
  #
  extract_and_cache :zip_codes, :postal_areas, :from => :addresses
end

So, this will result in setting up some save-hooks to serialize the cache data. Also setting up some read methods to read from the cache. Good idea? Need to check if it exists a gem for delegation with cache already, even though it should not be a huge job to implement.