cache_column 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 [name of plugin creator]
1
+ Copyright (c) 2010 Brian Malinconico
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Manifest CHANGED
@@ -2,6 +2,8 @@ MIT-LICENSE
2
2
  Manifest
3
3
  README.rdoc
4
4
  Rakefile
5
+ generators/cache_column/cache_column_generator.rb
6
+ generators/cache_column/templates/cache_column_migration.rb.erb
5
7
  init.rb
6
8
  install.rb
7
9
  lib/cache_column.rb
data/README.rdoc CHANGED
@@ -6,10 +6,56 @@ First add the following code to set-up the before_save filter and include the re
6
6
 
7
7
  has_cached_columns
8
8
 
9
- Next add a cache column
9
+ Next, we need to set-up the columns that hold our cached values. This is accomplished through a migration
10
+
11
+ script/generate cache_column OBJECT/TABLENAME feild:type
12
+
13
+ For example
14
+ script/generate cache_column listing high_price:float low_price:float number_of_price_points:integer
15
+
16
+ The above migration will add cache columns of the defined time to the listings table.
17
+
18
+ Now we need to add our cache column deffinations to the model.
19
+
20
+ The general format is
10
21
  cache_column :column_name, options
11
22
 
12
- === Valid Options
23
+ So for our example we want to add
24
+ cache_column :high_price
25
+ cache_column :low_price
26
+ cache_column :number_of_price_points
27
+
28
+ Now this dosn't do much so far other then map the virtual attribute
29
+ Object.high_price
30
+ to the database field
31
+ Ojbect.high_price_cache
32
+
33
+ No big deal, but the point is to store complex functions in the database. Lets say the high price attribute isn't so strait forward, and is calculated in the find_high_price function
34
+
35
+ def find_high_price
36
+ sleep(10)
37
+ return '10.05'
38
+ end
39
+
40
+ We can now tell the gem to update the cached value every time the object is saved by changing the cache_column deffination to
41
+
42
+ cache_column :high_price, :on_save => :recheck, :action => :find_high_price
43
+
44
+ Its fairly self explanitory, but this just tells the gem to recheck the price stored in high_price_cache when the model is saved (or touched). The stored value is retreived from the function named in the :action key
45
+
46
+ There are a few other options like
47
+ cache_column :high_price, :on_save => :blank
48
+ and
49
+ cache_column :high_price, :on_save => :recheck, :action => :find_high_price, :update_validation => :high_price_changed
50
+
51
+ The :on_save => :blank just clears the value when it gets saved... not sure what the point of that is yet.
52
+
53
+ :update_validation should be set to the name of a function, if that function returns false then the cache column will not be updated.
54
+
55
+ I would love feedback, and bugs reports.
13
56
 
57
+ == Limitations / Bugs
58
+ *Its my first gem.... so sorry for everything I've done wrong :)
59
+ *It dosn't store variables yet... Mabey next version. Till then use sanitize
14
60
 
15
- Copyright (c) 2010 [name of plugin creator], released under the MIT license
61
+ Copyright (c) 2010 Brian Malinconico, released under the MIT license
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('cache_column', '0.1.1') do |p|
5
+ Echoe.new('cache_column', '0.1.2') do |p|
6
6
  p.description = "Utilize the database to cache complex operations."
7
7
  p.url = "http://github.com/arjes/Cache-Column"
8
8
  p.author = "Brian Malinconico"
data/cache_column.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{cache_column}
5
- s.version = "0.1.1"
5
+ s.version = "0.1.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Brian Malinconico"]
9
- s.date = %q{2010-08-13}
9
+ s.date = %q{2010-08-21}
10
10
  s.description = %q{Utilize the database to cache complex operations.}
11
11
  s.email = %q{arjesins@gmail.com}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/cache_column.rb", "lib/tasks/cache_column.rake"]
13
- s.files = ["MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "init.rb", "install.rb", "lib/cache_column.rb", "lib/tasks/cache_column.rake", "test/cache_column_test.rb", "test/test_helper.rb", "uninstall.rb", "cache_column.gemspec"]
13
+ s.files = ["MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "generators/cache_column/cache_column_generator.rb", "generators/cache_column/templates/cache_column_migration.rb.erb", "init.rb", "install.rb", "lib/cache_column.rb", "lib/tasks/cache_column.rake", "test/cache_column_test.rb", "test/test_helper.rb", "uninstall.rb", "cache_column.gemspec"]
14
14
  s.homepage = %q{http://github.com/arjes/Cache-Column}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Cache_column", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
@@ -0,0 +1,33 @@
1
+ class CacheColumnGenerator < Rails::Generator::NamedBase
2
+ attr_accessor :cached_columns, :migration_name
3
+
4
+ def initialize(args, options = {})
5
+ super
6
+ @class_name = args[0]
7
+ @cached_columns = []
8
+ args[1..-1].each do |arg|
9
+ col_name, col_type = arg.split(':')
10
+ col_type ||= 'string'
11
+ @cached_columns.push({:name => col_name, :type => col_type})
12
+ end
13
+ end
14
+
15
+ def manifest
16
+ file_name = generate_file_name
17
+ @migration_name = file_name.camelize
18
+ record do |m|
19
+ m.migration_template "cache_column_migration.rb.erb",
20
+ File.join('db', 'migrate'),
21
+ :migration_file_name => file_name
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def generate_file_name
28
+ names = @cached_columns.map{|a| a[:name].underscore }
29
+ names = names[0..-2] + ["and", names[-1]] if names.length > 1
30
+ "add_cache_column#{(names.length > 1) ? 's' : ''}_#{names.join("_")}_to_#{@class_name.underscore}"
31
+ end
32
+
33
+ end
@@ -0,0 +1,13 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ <% cached_columns.each do |attachment| -%>
4
+ add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment[:name] %>_cache, :<%= attachment[:type] %>
5
+ <% end -%>
6
+ end
7
+
8
+ def self.down
9
+ <% cached_columns.each do |attachment| -%>
10
+ remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment[:name] %>_cache
11
+ <% end -%>
12
+ end
13
+ end
data/init.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  # Include hook code here
2
2
  require 'cache_column'
3
- ActiveRecord::Base.send :include, CacheColumn
3
+
data/lib/cache_column.rb CHANGED
@@ -30,6 +30,7 @@ module CacheColumn
30
30
 
31
31
  cached_columns.each do |key,params|
32
32
  next if params[:update_validation].present? and self.respond_to?(params[:update_validation]) and self.send("#{params[:update_validation]}") == false
33
+ next if params[:on_save].blank?
33
34
 
34
35
  if params[:on_save] == :blank
35
36
  new_value = ""
@@ -39,10 +40,10 @@ module CacheColumn
39
40
  set = true
40
41
  end
41
42
 
42
- self.send("cache_#{key}=".to_sym, new_value) if set == true
43
+ self["#{key}_cache".to_sym] = new_value if set == true
43
44
  end
44
45
 
45
- self.cache_updated_at = Time.now if self.respond_to?(:cached_updated_at)
46
+ #self.cache_updated_at = Time.now if self.respond_to?(:cached_updated_at)
46
47
  end
47
48
 
48
49
 
@@ -58,15 +59,17 @@ module CacheColumn
58
59
  end
59
60
 
60
61
  def method_missing(method, *arguments, &block)
61
- if self.respond_to?("cache_#{method}".to_sym)
62
- return self.send("cache_#{method}".to_sym, *arguments)
63
- elsif self.respond_to?("cache_#{method.to_s.chop}")
64
- return self.send("cache_#{method.to_s.chop}".to_sym)
62
+ cached_columns = self.class.instance_variable_get('@cached_columns')
63
+
64
+ if cached_columns.keys.include?(method) #then its a getter
65
+ return self["#{method}_cache".to_sym]
66
+ elsif method.to_s[-1,1] == '=' and cached_columns.keys.include?(method.to_s.chop.to_sym) and cached_columns[method.to_s.chop.to_sym][:action].blank? #then its a setter
67
+ return self["#{method.to_s.chop}_cache".to_sym] = arguments[0]
68
+ else
69
+ super
65
70
  end
66
- super
67
71
  end
68
72
  end
69
73
  end
70
74
 
71
-
72
-
75
+ ActiveRecord::Base.send :include, CacheColumn
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_column
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brian Malinconico
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-13 00:00:00 -04:00
18
+ date: 2010-08-21 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -34,6 +34,8 @@ files:
34
34
  - Manifest
35
35
  - README.rdoc
36
36
  - Rakefile
37
+ - generators/cache_column/cache_column_generator.rb
38
+ - generators/cache_column/templates/cache_column_migration.rb.erb
37
39
  - init.rb
38
40
  - install.rb
39
41
  - lib/cache_column.rb