0.0
No commit activity in last 3 years
No release in over 3 years
Cache method outputs and automatically create chainable scopes based on their outputs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0.4.0
~> 1.6.3
~> 2.6.0
~> 2.6.1
~> 0.6.0

Runtime

>= 3.0.0
 Project Readme

denormalizer¶ ↑

Run and cache methods so they can be used later. Create chainable scopes using the cached outputs of the methods.

Installation¶ ↑

Add to your Gemfile

gem "denormalizer"

Run the installer

rails generate denormalizer:install
rake db:migrate

Denormalize a method in a model. This will run the method and save the result every time the model is updated.

class Widget < ActiveRecord::Base
  def active?
    do_something_complex
  end
  denormalize :active?
end

Denormalize associations. This will run the denormalization method on each of the objects in the association.

class Item < ActiveRecord::Base
  has_many :widgets
  also_denormalize :widgets
end

Use the denormalized data

Book.denormalized_actives
Book.denormalized_not_actives

Book.first.denormalized_active?

Thought Process¶ ↑

I am trying to avoid duplicating complex code in instance methods and scopes. Given this method,

def active?
  received_at >= Time.now && items.expired.empty?
end

You have two choices to get all active records,

def self.active
  all.select(&:active?)
end

scope :active, lambda { where(["widgets.received_at >= ?", Time.now]).where("(select count(*) from items where items.widget_id=widgets.id and (expired_at is null or expired_at >= ?)) = 0", Time.now]) }

The first minimizes code duplication but is not chainable and often slower than the pure sql version. The second solution is fast and chainable but requires two updates every time the active? criteria needs to be updated. Additionally, it requires an updated whenever the Item model’s expired scope is updated.

Todo¶ ↑

  • Add code to make sure association denormalization does loop infinitely

  • Improve testing on the Denormalizer::MethodOutput model

  • Refactor dome of the denormalize mixin code to the MethodOutput model

  • Figure out how to replace mysql with a different database/memory store system

Contributing to denormalizer¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2012 Jeremiah Hemphill. See LICENSE.txt for further details.