Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First implementation

  • Loading branch information...
commit 795a80a9a1f48ada538e3feb5ee8bc036eeffd2c 1 parent bfcd8eb
@porras porras authored
View
27 README
@@ -1,13 +1,24 @@
Persistize
==========
-Introduction goes here.
+Just like ''memoize'' but stores the value as an attribute in the database. Updates the field 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')
+
+This example is just one of the possible applications of this pattern, your imagination is the limit =;-) If you can find a
+better example, please send it to us.
-Example
-=======
-
-Example goes here.
-
-
-Copyright (c) 2008 [name of plugin creator], released under the MIT license
+Copyright (c) 2008 Luismi Cavallé & Sergio Gil, released under the MIT license
View
2  init.rb
@@ -1 +1 @@
-# Include hook code here
+require 'persistize'
View
30 lib/persistize.rb
@@ -1 +1,29 @@
-# Persistize
+class ActiveRecord::Base
+
+ def self.persistize(*methods)
+
+ methods.each do |method|
+
+ alias_method "#{method}_calculation", method
+
+ define_method(method) do
+ if new_record?
+ send("#{method}_calculation")
+ else
+ self[method]
+ end
+ end
+
+ define_method("#{method}=") do
+ raise NoMethodError
+ end
+
+ before_save do |record|
+ record[method] = record.send("#{method}_calculation")
+ end
+
+ end
+
+ end
+
+end
View
4 tasks/persistize_tasks.rake
@@ -1,4 +0,0 @@
-# desc "Explaining what the task does"
-# task :persistize do
-# # Task goes here
-# end
View
104 test/persistize_test.rb
@@ -1,8 +1,106 @@
require 'test/unit'
+require 'rubygems'
+require 'shoulda'
+require 'activerecord'
+require 'activesupport'
+
+require File.dirname(__FILE__) + '/../init'
+
+class Project < ActiveRecord::Base
+ has_many :tasks
+
+ def completed?
+ tasks.all?(&:completed?)
+ end
+
+end
+
+class Task < ActiveRecord::Base
+
+end
+
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
+
+def setup_db
+ ActiveRecord::Schema.define(:version => 1) do
+ create_table :projects do |t|
+ t.string :name
+ t.boolean :completed
+ t.string :hello
+ end
+ create_table :tasks do |t|
+ t.integer :project_id
+ t.boolean :completed
+ end
+ end
+end
+
+def drop_db
+ ActiveRecord::Base.connection.tables.each do |table|
+ ActiveRecord::Base.connection.drop_table(table)
+ end
+end
+
class PersistizeTest < Test::Unit::TestCase
- # Replace this with your real tests.
- def test_this_plugin
- flunk
+
+ context "A project which persistizes Project#hello" do
+
+ setup do
+ setup_db
+
+ Project.class_eval do
+
+ def hello
+ @hello_value ||= 'hi'
+ end
+
+ def bye
+ 'bye'
+ end
+
+ persistize :hello, :bye
+
+ attr_accessor :hello_value
+ end
+
+ @project = Project.create
+ end
+
+ should "update the value of hello in the database when created" do
+ assert_equal('hi', @project[:hello])
+ assert_equal(@project, Project.find_by_hello('hi'))
+ end
+
+ should "update the value of hello in the database when updated" do
+ @project.hello_value = 'bye'
+ @project.update_attributes!(:name => 'new name')
+ assert_equal('bye', @project[:hello])
+ assert_equal(@project, Project.find_by_hello('bye'))
+ end
+
+ should "not call the method when reading after being saved" do
+ @project.hello_value = 'gutten morgen'
+ assert_equal('hi', @project.hello)
+ end
+
+ should "call the method when reading before being created" do
+ project = Project.new
+ assert_equal('hi', project.hello)
+ end
+
+ should "not be able to manually update hello" do
+ assert_raise(NoMethodError) { @project.hello = 'Catacrock' }
+ end
+
+ should "also persistize #bye" do
+ assert_equal('bye', @project[:bye])
+ end
+
+ teardown do
+ drop_db
+ end
+
end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.