Giter Site home page Giter Site logo

persistize's Introduction

Persistize

Persistize is a Rails plugin for easy denormalization. It works just like memoize but it stores the value as an attribute in the database. You only need to write a method with the denormalization logic and a field in the database with the same name of the method. The field will get updated each time the record is saved:

class Person < ActiveRecord::Base
  def full_name
    "#{first_name} #{last_name}"
  end

  persistize :full_name
end

...

Person.create(:first_name => 'Jimi', :last_name => 'Hendrix')
Person.find_by_full_name('Jimi Hendrix') # #<Person id:1, first_name:"Jimi", last_name:"Hendrix", full_name:"Jimi Hendrix" ...>

Dependency

Sometimes you want to update the field not when the record is changed, but when some other associated records are. For example:

class Project < ActiveRecord::Base
  has_many :tasks

  def completed?
    tasks.any? && tasks.all?(&:completed?)
  end

  persistize :completed?, :depending_on => :tasks

  named_scope :completed, :conditions => { :completed => true }
end

class Task < ActiveRecord::Base
  belongs_to :project
end

...

project = Project.create(:name => 'Rails')
task = project.tasks.create(:name => 'Make it scale', :completed => false)    
Project.completed  # []

task.update_attributes(:completed => true)
Project.completed  # [#<Project id:1, name:"Rails", completed:true ...>]

You can add more than one dependency using an array:

persistize :summary, :depending_on => [:projects, :people, :tasks]

These examples are just some of the possible applications of this pattern, your imagination is the limit =;-) If you can find better examples, please send them to us.

Install

Just add it to your Gemfile:

gem "persistize"

And run:

$ bundle install

To-do

  • Make cache optional (cache can cause records to be inconsistent if changed and not saved so it would be nice to be able to deactivate it)

Copyright © 2008-2011 Luismi Cavallé & Sergio Gil, released under the MIT license

persistize's People

Contributors

cavalle avatar porras avatar

Watchers

James Cloos avatar Andy Hayden avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.