bebanjo-persistize 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +73 -0
- data/lib/persistize.rb +84 -0
- metadata +58 -0
data/README.rdoc
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
=Persistize
|
|
2
|
+
|
|
3
|
+
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:
|
|
4
|
+
|
|
5
|
+
class Person < ActiveRecord::Base
|
|
6
|
+
def full_name
|
|
7
|
+
"#{first_name} #{last_name}"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
persistize :full_name
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
...
|
|
14
|
+
|
|
15
|
+
Person.create(:first_name => 'Jimi', :last_name => 'Hendrix')
|
|
16
|
+
Person.find_by_full_name('Jimi Hendrix') # #<Person id:1, first_name:"Jimi", last_name:"Hendrix", full_name:"Jimi Hendrix" ...>
|
|
17
|
+
|
|
18
|
+
==Dependency
|
|
19
|
+
|
|
20
|
+
Sometimes you want to update the field not when the record is changed, but when some other associated records are. For example:
|
|
21
|
+
|
|
22
|
+
class Project < ActiveRecord::Base
|
|
23
|
+
has_many :tasks
|
|
24
|
+
|
|
25
|
+
def completed?
|
|
26
|
+
tasks.any? && tasks.all?(&:completed?)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
persistize :completed?, :depending_on => :tasks
|
|
30
|
+
|
|
31
|
+
named_scope :completed, :conditions => { :completed => true }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class Task < ActiveRecord::Base
|
|
35
|
+
belongs_to :project
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
...
|
|
39
|
+
|
|
40
|
+
project = Project.create(:name => 'Rails')
|
|
41
|
+
task = project.tasks.create(:name => 'Make it scale', :completed => false)
|
|
42
|
+
Project.completed # []
|
|
43
|
+
|
|
44
|
+
task.update_attributes(:completed => true)
|
|
45
|
+
Project.completed # [#<Project id:1, name:"Rails", completed:true ...>]
|
|
46
|
+
|
|
47
|
+
You can add more than one dependency using an array:
|
|
48
|
+
|
|
49
|
+
persistize :summary, :depending_on => [:projects, :people, :tasks]
|
|
50
|
+
|
|
51
|
+
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.
|
|
52
|
+
|
|
53
|
+
==Install
|
|
54
|
+
|
|
55
|
+
===As a plugin (Rails >= 2.1.0)
|
|
56
|
+
|
|
57
|
+
$ script/plugin install git://github.com/porras/persistize.git
|
|
58
|
+
|
|
59
|
+
===As a gem (Rails >= 2.1.0)
|
|
60
|
+
|
|
61
|
+
# in config/environment.rb
|
|
62
|
+
config.gem "porras-persistize", :lib => "persistize", :source => "http://gems.github.com"
|
|
63
|
+
|
|
64
|
+
And then:
|
|
65
|
+
|
|
66
|
+
$ [sudo] rake gems:install
|
|
67
|
+
|
|
68
|
+
==To-do
|
|
69
|
+
|
|
70
|
+
* More kinds of dependencies (+has_one+, <code>has_many :through</code>)
|
|
71
|
+
* 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)
|
|
72
|
+
|
|
73
|
+
Copyright (c) 2008 Luismi Cavallé & Sergio Gil, released under the MIT license
|
data/lib/persistize.rb
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
module Persistize
|
|
2
|
+
module ClassMethods
|
|
3
|
+
def persistize(*args)
|
|
4
|
+
options = args.pop if args.last.is_a?(Hash)
|
|
5
|
+
|
|
6
|
+
args.each do |method|
|
|
7
|
+
attribute = method.to_s.sub(/\?$/, '')
|
|
8
|
+
|
|
9
|
+
original_method = :"_unpersistized_#{attribute}"
|
|
10
|
+
update_method = :"_update_#{attribute}"
|
|
11
|
+
|
|
12
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
13
|
+
alias #{original_method} #{method} # alias _unpersistized_full_name full_name
|
|
14
|
+
#
|
|
15
|
+
def #{method} # def full_name
|
|
16
|
+
if new_record? # if new_record?
|
|
17
|
+
#{original_method} # _unpersistized_full_name
|
|
18
|
+
else # else
|
|
19
|
+
self[:#{attribute}] # self[:full_name]
|
|
20
|
+
end # end
|
|
21
|
+
end # end
|
|
22
|
+
#
|
|
23
|
+
before_save :#{update_method} # before_save :_update_full_name
|
|
24
|
+
#
|
|
25
|
+
def #{update_method} # def _update_full_name
|
|
26
|
+
self[:#{attribute}] = #{original_method} # self[:full_name] = _unpersistized_full_name
|
|
27
|
+
true # return true to avoid canceling the save # true
|
|
28
|
+
end # end
|
|
29
|
+
#
|
|
30
|
+
def #{update_method}! # def _update_full_name!
|
|
31
|
+
#{update_method} # _update_full_name
|
|
32
|
+
save! if #{attribute}_changed? # save! if full_name_changed?
|
|
33
|
+
end # end
|
|
34
|
+
RUBY
|
|
35
|
+
|
|
36
|
+
if options && options[:depending_on]
|
|
37
|
+
dependencies = [options[:depending_on]].flatten
|
|
38
|
+
|
|
39
|
+
dependencies.each do |dependency|
|
|
40
|
+
generate_callback(reflections[dependency], update_method)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def generate_callback(association, update_method)
|
|
50
|
+
callback_name = :"#{update_method}_in_#{self.to_s.underscore}_callback"
|
|
51
|
+
generate_method = :"generate_#{association.macro}_callback"
|
|
52
|
+
unless respond_to?(generate_method, true)
|
|
53
|
+
raise "#{association.macro} associations are not supported by persistize"
|
|
54
|
+
end
|
|
55
|
+
send(generate_method, association, update_method, callback_name)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def generate_has_many_callback(association, update_method, callback_name)
|
|
59
|
+
association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
60
|
+
def #{callback_name} # def _update_completed_in_project_callback
|
|
61
|
+
return true unless parent_id = self[:#{association.primary_key_name}] # return true unless parent_id = self[:project_id]
|
|
62
|
+
parent = #{self.name}.find(parent_id) # parent = Project.find(parent_id)
|
|
63
|
+
parent.#{update_method}! # parent._update_completed!
|
|
64
|
+
end # end
|
|
65
|
+
after_save :#{callback_name} # after_save :_update_completed_in_project_callback
|
|
66
|
+
after_destroy :#{callback_name} # after_destroy :_update_completed_in_project_callback
|
|
67
|
+
RUBY
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def generate_belongs_to_callback(association, update_method, callback_name)
|
|
71
|
+
association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
72
|
+
def #{callback_name} # def _update_project_name_in_task_callback
|
|
73
|
+
childs = #{self.name}.all(:conditions => {:#{association.primary_key_name} => id}) # childs = Task.all(:conditions => {:project_id => id})
|
|
74
|
+
childs.each(&:"#{update_method}!") # childs.each(&:"_update_project_name!")
|
|
75
|
+
end # end
|
|
76
|
+
after_save :#{callback_name} # after_save :_update_project_name_in_task_callback
|
|
77
|
+
after_destroy :#{callback_name} # after_destroy :_update_project_name_in_task_callback
|
|
78
|
+
RUBY
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
ActiveRecord::Base.extend(Persistize::ClassMethods)
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bebanjo-persistize
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sergio Gil
|
|
8
|
+
- "Luismi Cavall\xC3\xA9"
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2009-05-16 00:00:00 -07:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies: []
|
|
16
|
+
|
|
17
|
+
description:
|
|
18
|
+
email: ballsbreaking@bebanjo.com
|
|
19
|
+
executables: []
|
|
20
|
+
|
|
21
|
+
extensions: []
|
|
22
|
+
|
|
23
|
+
extra_rdoc_files:
|
|
24
|
+
- README.rdoc
|
|
25
|
+
files:
|
|
26
|
+
- README.rdoc
|
|
27
|
+
- lib/persistize.rb
|
|
28
|
+
has_rdoc: true
|
|
29
|
+
homepage: http://github.com/bebanjo/persistize
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options:
|
|
32
|
+
- --line-numbers
|
|
33
|
+
- --inline-source
|
|
34
|
+
- --main
|
|
35
|
+
- README.rdoc
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: "0"
|
|
43
|
+
version:
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: "0"
|
|
49
|
+
version:
|
|
50
|
+
requirements: []
|
|
51
|
+
|
|
52
|
+
rubyforge_project:
|
|
53
|
+
rubygems_version: 1.2.0
|
|
54
|
+
signing_key:
|
|
55
|
+
specification_version: 2
|
|
56
|
+
summary: Easy denormalization for your ActiveRecord models
|
|
57
|
+
test_files: []
|
|
58
|
+
|