persistize 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c628a759126e73b58718944a87dcecf5f15f39b
4
+ data.tar.gz: edbb6f4a1bab6a391ba34e373208e2fdf019d284
5
+ SHA512:
6
+ metadata.gz: d7e290b5072d42695ea29ef60dad7991e1f1db649835d6375a72125bfce4007b133c8160a89095b19b05124e97d69884d31e6b5084b01eb868dea6d6775dacac
7
+ data.tar.gz: ab8f6e3d274336ab68c14c4add91af4795e3578d676b79082ba2b19f006b93f5628cb58794de477eced36cb153f140b16a378eea26a26e917c4171009eb41e35
@@ -64,4 +64,4 @@ And run:
64
64
 
65
65
  * 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)
66
66
 
67
- Copyright (c) 2008-2011 Luismi Cavallé & Sergio Gil, released under the MIT license
67
+ Copyright (c) 2008-2014 Luismi Cavallé & Sergio Gil & Paco Guzmán, released under the MIT license
data/Rakefile CHANGED
@@ -7,8 +7,8 @@ end
7
7
 
8
8
  task :default => ["test"]
9
9
 
10
- require "rake/rdoctask"
11
- Rake::RDocTask.new do |rd|
10
+ require "rdoc/task"
11
+ RDoc::Task.new do |rd|
12
12
  rd.main = "README.rdoc"
13
13
  rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
14
14
  rd.rdoc_dir = "rdoc"
@@ -1,102 +1,8 @@
1
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? || changed? # if new_record? || changed?
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}, :if => :changed? # 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
- new_#{attribute} = #{original_method} # new_full_name = _unpersistized_full_name
32
- return if new_#{attribute} == self[:#{attribute}] # return if new_full_name == self[:full_name]
33
- update_attribute :#{attribute}, new_#{attribute} # update_attribute :full_name, new_full_name
34
- end # end
35
- RUBY
36
-
37
- if options && options[:depending_on]
38
- dependencies = [options[:depending_on]].flatten
39
-
40
- dependencies.each do |dependency|
41
- generate_callback(reflections[dependency], update_method)
42
- end
43
- end
44
-
45
- end
46
- end
47
-
48
- private
49
-
50
- def generate_callback(association, update_method)
51
- callback_name = :"#{update_method}_in_#{self.to_s.underscore.gsub(/\W/, '_')}_callback"
52
- association_type = "#{association.macro}#{'_through' if association.through_reflection}"
53
- generate_method = :"generate_#{association_type}_callback"
54
- unless respond_to?(generate_method, true)
55
- raise "#{association_type} associations are not supported by persistize"
56
- end
57
- send(generate_method, association, update_method, callback_name)
58
- end
59
-
60
- def generate_has_many_callback(association, update_method, callback_name)
61
- association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
62
- def #{callback_name} # def _update_completed_in_project_callback
63
- return true unless parent_id = self[:#{association.primary_key_name}] # return true unless parent_id = self[:project_id]
64
- parent = #{self.name}.find(parent_id) # parent = Project.find(parent_id)
65
- parent.#{update_method}! # parent._update_completed!
66
- end # end
67
- after_save :#{callback_name} # after_save :_update_completed_in_project_callback
68
- after_destroy :#{callback_name} # after_destroy :_update_completed_in_project_callback
69
- RUBY
70
- end
71
-
72
- alias_method :generate_has_one_callback, :generate_has_many_callback # implementation is just the same :)
73
-
74
- def generate_has_many_through_callback(association, update_method, callback_name)
75
- association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
76
- def #{callback_name} # def _update_completed_in_person_callback
77
- return true unless through_id = self[:#{association.through_reflection.association_foreign_key}] # return true unless through_id = self[:project_id]
78
- through = #{association.through_reflection.class_name}.find(through_id) # through = Project.find(through_id)
79
- return true unless parent_id = through[:#{association.primary_key_name}] # return true unless parent_id = self[:person_id]
80
- parent = #{self.name}.find(parent_id) # parent = Person.find(person_id)
81
- parent.#{update_method}! # parent._update_completed!
82
- end # end
83
- after_save :#{callback_name} # after_save :_update_completed_in_person_callback
84
- after_destroy :#{callback_name} # after_destroy :_update_completed_in_person_callback
85
- RUBY
86
- end
87
-
88
- def generate_belongs_to_callback(association, update_method, callback_name)
89
- association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
90
- def #{callback_name} # def _update_project_name_in_task_callback
91
- childs = #{self.name}.all(:conditions => {:#{association.primary_key_name} => id}) # childs = Task.all(:conditions => {:project_id => id})
92
- childs.each(&:"#{update_method}!") # childs.each(&:"_update_project_name!")
93
- end # end
94
- after_save :#{callback_name} # after_save :_update_project_name_in_task_callback
95
- after_destroy :#{callback_name} # after_destroy :_update_project_name_in_task_callback
96
- RUBY
97
- end
98
-
99
- end
100
2
  end
101
3
 
102
- ActiveRecord::Base.extend(Persistize::ClassMethods)
4
+ if defined?(Rails::Railtie)
5
+ require 'persistize/railtie'
6
+ elsif defined?(Rails::Initializer)
7
+ raise "persistize is not compatible with Rails 2.3 or older"
8
+ end
@@ -0,0 +1,113 @@
1
+ module Persistize
2
+ mattr_accessor :performant
3
+ self.performant = true
4
+
5
+ module ActiveRecord
6
+ module ClassMethods
7
+ def persistize(*args)
8
+ options = args.extract_options!
9
+
10
+ performant = {:performant => ::Persistize.performant}.merge(options)[:performant]
11
+
12
+ args.each do |method|
13
+ attribute = method.to_s.sub(/\?$/, '')
14
+
15
+ original_method = :"_unpersistized_#{attribute}"
16
+ update_method = :"_update_#{attribute}"
17
+
18
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
19
+ alias #{original_method} #{method} # alias _unpersistized_full_name full_name
20
+ #
21
+ def #{method} # def full_name
22
+ if new_record? || changed? # if new_record? || changed?
23
+ #{original_method} # _unpersistized_full_name
24
+ else # else
25
+ self[:#{attribute}] # self[:full_name]
26
+ end # end
27
+ end # end
28
+ #
29
+ before_create :#{update_method} # before_create :_update_full_name
30
+ before_update :#{update_method}, :if => :changed? # before_update :_update_full_name, :if => :changed?
31
+ #
32
+ def #{update_method} # def _update_full_name
33
+ self[:#{attribute}] = #{original_method} # self[:full_name] = _unpersistized_full_name
34
+ true # return true to avoid canceling the save # true
35
+ end # end
36
+ #
37
+ def #{update_method}! # def _update_full_name!
38
+ if #{performant} # if performant
39
+ new_#{attribute} = #{original_method} # new_full_name = _unpersistized_full_name
40
+ return if new_#{attribute} == self[:#{attribute}] # return if new_full_name == self[:full_name]
41
+ update_attribute :#{attribute}, new_#{attribute} # update_attribute :full_name, new_full_name
42
+ else # else
43
+ #{update_method} # _update_full_name
44
+ save! if #{attribute}_changed? # save! if full_name_changed?
45
+ end # end
46
+ end # end
47
+ RUBY
48
+
49
+ if options && options[:depending_on]
50
+ dependencies = [options[:depending_on]].flatten
51
+
52
+ dependencies.each do |dependency|
53
+ generate_callback(reflections[dependency], update_method)
54
+ end
55
+ end
56
+
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def generate_callback(association, update_method)
63
+ callback_name = :"#{update_method}_in_#{self.to_s.underscore.gsub(/\W/, '_')}_callback"
64
+ association_type = "#{association.macro}#{'_through' if association.through_reflection}"
65
+ generate_method = :"generate_#{association_type}_callback"
66
+ unless respond_to?(generate_method, true)
67
+ raise "#{association_type} associations are not supported by persistize"
68
+ end
69
+ send(generate_method, association, update_method, callback_name)
70
+ end
71
+
72
+ def generate_has_many_callback(association, update_method, callback_name)
73
+ association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
74
+ def #{callback_name} # def _update_completed_in_project_callback
75
+ return true unless parent_id = self[:#{association.foreign_key}] # return true unless parent_id = self[:project_id]
76
+ parent = #{self.name}.find(parent_id) # parent = Project.find(parent_id)
77
+ parent.#{update_method}! # parent._update_completed!
78
+ end # end
79
+ after_save :#{callback_name} # after_save :_update_completed_in_project_callback
80
+ after_destroy :#{callback_name} # after_destroy :_update_completed_in_project_callback
81
+ RUBY
82
+ end
83
+
84
+ alias_method :generate_has_one_callback, :generate_has_many_callback # implementation is just the same :)
85
+
86
+ def generate_has_many_through_callback(association, update_method, callback_name)
87
+ association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
88
+ def #{callback_name} # def _update_completed_in_person_callback
89
+ return true unless through_id = self[:#{association.through_reflection.association_foreign_key}] # return true unless through_id = self[:project_id]
90
+ through = #{association.through_reflection.class_name}.find(through_id) # through = Project.find(through_id)
91
+ return true unless parent_id = through[:#{association.through_reflection.foreign_key}] # return true unless parent_id = self[:person_id]
92
+ parent = #{self.name}.find(parent_id) # parent = Person.find(person_id)
93
+ parent.#{update_method}! # parent._update_completed!
94
+ end # end
95
+ after_save :#{callback_name} # after_save :_update_completed_in_person_callback
96
+ after_destroy :#{callback_name} # after_destroy :_update_completed_in_person_callback
97
+ RUBY
98
+ end
99
+
100
+ def generate_belongs_to_callback(association, update_method, callback_name)
101
+ association.klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
102
+ def #{callback_name} # def _update_project_name_in_task_callback
103
+ childs = #{self.name}.where({:#{association.foreign_key} => id}) # childs = Task.where({:project_id => id})
104
+ childs.each(&:"#{update_method}!") # childs.each(&:"_update_project_name!")
105
+ end # end
106
+ after_save :#{callback_name} # after_save :_update_project_name_in_task_callback
107
+ after_destroy :#{callback_name} # after_destroy :_update_project_name_in_task_callback
108
+ RUBY
109
+ end
110
+
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,12 @@
1
+ require 'persistize/active_record'
2
+
3
+ module Persistize
4
+ class Railtie < Rails::Railtie
5
+ initializer "persistize" do |app|
6
+ ActiveSupport.on_load :active_record do
7
+ extend Persistize::ActiveRecord::ClassMethods
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,3 @@
1
+ module Persistize
2
+ VERSION = '0.3.0'
3
+ end
@@ -1,12 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
+ $:.unshift File.expand_path('../lib', __FILE__)
4
+ require 'persistize/version'
5
+
3
6
  Gem::Specification.new do |s|
4
7
  s.name = "persistize"
5
- s.version = "0.2.0"
8
+ s.version = Persistize::VERSION
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Sergio Gil", "Luismi Cavallé"]
9
- s.date = Date.today.strftime("%Y-%m-%d")
11
+ s.authors = ["Sergio Gil", "Luismi Cavallé", "Paco Guzmán"]
10
12
  s.email = "ballsbreaking@bebanjo.com"
11
13
  s.extra_rdoc_files = ["README.rdoc"]
12
14
  s.files = %w(init.rb MIT-LICENSE persistize.gemspec Rakefile README.rdoc) + Dir.glob("{test,lib/**/*}")
@@ -17,10 +19,10 @@ Gem::Specification.new do |s|
17
19
  s.summary = "Easy denormalization for your ActiveRecord models"
18
20
  s.specification_version = 3 if s.respond_to? :specification_version
19
21
 
20
- s.add_dependency("activerecord")
21
-
22
- s.add_development_dependency("ruby-debug")
22
+ s.add_dependency("activerecord", ">= 3.1.0", "< 4.0.0")
23
+
23
24
  s.add_development_dependency("shoulda")
24
25
  s.add_development_dependency("rake")
26
+ s.add_development_dependency("rdoc")
25
27
  s.add_development_dependency("sqlite3")
26
28
  end
metadata CHANGED
@@ -1,143 +1,132 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: persistize
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 0
10
- version: 0.2.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Sergio Gil
14
- - "Luismi Cavall\xC3\xA9"
8
+ - Luismi Cavallé
9
+ - Paco Guzmán
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2011-05-06 00:00:00 +02:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
13
+ date: 2014-07-04 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
23
16
  name: activerecord
24
- prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 3
31
- segments:
32
- - 0
33
- version: "0"
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: 4.0.0
34
25
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: ruby-debug
38
26
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- version: "0"
48
- type: :development
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - '>='
30
+ - !ruby/object:Gem::Version
31
+ version: 3.1.0
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ version: 4.0.0
35
+ - !ruby/object:Gem::Dependency
51
36
  name: shoulda
52
- prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
54
- none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
62
42
  type: :development
63
- version_requirements: *id003
64
- - !ruby/object:Gem::Dependency
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ - !ruby/object:Gem::Dependency
65
50
  name: rake
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
66
57
  prerelease: false
67
- requirement: &id004 !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 0
75
- version: "0"
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rdoc
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
76
70
  type: :development
77
- version_requirements: *id004
78
- - !ruby/object:Gem::Dependency
79
- name: sqlite3
80
71
  prerelease: false
81
- requirement: &id005 !ruby/object:Gem::Requirement
82
- none: false
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- hash: 3
87
- segments:
88
- - 0
89
- version: "0"
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ - !ruby/object:Gem::Dependency
78
+ name: sqlite3
79
+ requirement: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
90
84
  type: :development
91
- version_requirements: *id005
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
92
91
  description:
93
92
  email: ballsbreaking@bebanjo.com
94
93
  executables: []
95
-
96
94
  extensions: []
97
-
98
- extra_rdoc_files:
95
+ extra_rdoc_files:
99
96
  - README.rdoc
100
- files:
97
+ files:
101
98
  - init.rb
102
99
  - MIT-LICENSE
103
100
  - persistize.gemspec
104
101
  - Rakefile
105
102
  - README.rdoc
103
+ - lib/persistize/active_record.rb
104
+ - lib/persistize/railtie.rb
105
+ - lib/persistize/version.rb
106
106
  - lib/persistize.rb
107
- has_rdoc: true
108
107
  homepage: http://github.com/bebanjo/persistize
109
108
  licenses: []
110
-
109
+ metadata: {}
111
110
  post_install_message:
112
- rdoc_options:
111
+ rdoc_options:
113
112
  - --main
114
113
  - README.rdoc
115
- require_paths:
114
+ require_paths:
116
115
  - lib
117
- required_ruby_version: !ruby/object:Gem::Requirement
118
- none: false
119
- requirements:
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- hash: 3
123
- segments:
124
- - 0
125
- version: "0"
126
- required_rubygems_version: !ruby/object:Gem::Requirement
127
- none: false
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- hash: 3
132
- segments:
133
- - 0
134
- version: "0"
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
135
126
  requirements: []
136
-
137
127
  rubyforge_project:
138
- rubygems_version: 1.6.2
128
+ rubygems_version: 2.0.14
139
129
  signing_key:
140
130
  specification_version: 3
141
131
  summary: Easy denormalization for your ActiveRecord models
142
132
  test_files: []
143
-