Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 276b3e5ada821bb78375e3917a16584a705d18fd @pjdavis committed Mar 6, 2010
@@ -0,0 +1,2 @@
+spec/debug.log
+.DS_Store
@@ -0,0 +1,9 @@
+CC0 1.0 Universal
+http://creativecommons.org/publicdomain/zero/1.0/
+
+The person who associated a work with this document has dedicated the work to the Commons by waiving all of his or her rights to the work worldwide under copyright law and all related or neighboring legal rights he or she had in the work, to the extent allowable by law.
+
+Works under CC0 do not require attribution. When citing the work, you should not imply endorsement by the author.
+Other Rights — In no way are any of the following rights affected by CC0:
+Patent or trademark rights held by the person who associated this document with a work.
+Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
39 README
@@ -0,0 +1,39 @@
+IdentityMap
+===========
+
+1. Install the Plugin (ruby script/plugin install http://github.com/pjdavis/identity_map.git)
+
+2. Start Your Application
+
+3. ???
+
+4. Profit!
+
+Example
+=======
+
+Rehearsal ------------------------------------
+ 0.540000 0.020000 0.560000 ( 0.631249)
+--------------------------- total: 0.560000sec
+
+ user system total real
+ 0.410000 0.010000 0.420000 ( 0.465812)
+
+Objects:
+Warmup: 30332 allocations | 1237329 bytes
+Actual: 2405 allocations | 84129 bytes
+
+===============================================
+Rehearsal ------------------------------------
+ 1.540000 0.070000 1.610000 ( 1.937447)
+--------------------------- total: 1.610000sec
+
+ user system total real
+ 1.430000 0.070000 1.500000 ( 1.823009)
+
+Objects:
+Warmup: 27709 allocations | 1100864 bytes
+Actual: 14430 allocations | 502673 bytes
+
+For the inspiration/base code, check out: http://www.harukizaemon.com/2006/09/activerecord-identity-map-for-rails.html
+Copyright (c) 2010 [PJ Davis], released under the CC0 1.0 Universal license.
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the identity_map plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the identity_map plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'IdentityMap'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+require File.join(File.dirname(__FILE__), "lib", "identity_map")
@@ -0,0 +1 @@
+puts IO.read(File.join(File.dirname(__FILE__), 'README'))
@@ -0,0 +1,5 @@
+$:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
+
+Autotest.add_discovery do
+ "rspec"
+end
@@ -0,0 +1,3 @@
+require "identity_map/cache"
+require "identity_map/action_controller/dispatcher"
+require "identity_map/active_record/base"
@@ -0,0 +1,20 @@
+module IdentityMap
+
+ def self.included(base)
+ base.before_dispatch :build_cache
+ base.after_dispatch :remove_cache
+ end
+
+ def build_cache
+ Thread.current['identity_map'] ||= Cache.new
+ end
+
+ def remove_cache
+ Thread.current['identity_map'] = nil
+ end
+
+end
+if Object.const_defined?("ActionController")
+ ActionController::Dispatcher.send :include, IdentityMap
+ RAILS_DEFAULT_LOGGER.warn "CACHING OBJECTS"
+end
@@ -0,0 +1,32 @@
+module IdentityMap
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ class << base
+ alias_method_chain :instantiate, :identity_map
+ end
+ end
+
+ module ClassMethods
+
+ private
+
+ def instantiate_with_identity_map(record)
+ enlist_in_transaction(instantiate_without_identity_map(record))
+ end
+
+ def enlist_in_transaction(object)
+ identity_map = Thread.current['identity_map']
+ unless identity_map
+ return object
+ end
+ identity_map.put(object)
+ end
+
+ end
+
+end
+
+if Object.const_defined?("ActiveRecord")
+ ActiveRecord::Base.send :include, IdentityMap
+end
@@ -0,0 +1,19 @@
+class Cache
+
+ def initialize
+ @objects = {}
+ end
+
+ def put(object)
+ objects = @objects[object.class] ||= {}
+ if objects[object.id]
+ RAILS_DEFAULT_LOGGER.warn "Called From Cache #{objects[object.id].__id__}"
+ objects[object.id]
+ else
+ RAILS_DEFAULT_LOGGER.warn "Not Found In Cache #{object.__id__}"
+ objects[object.id] = object
+ object
+ end
+ end
+
+end
@@ -0,0 +1,5 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "NewFu" do
+ it "should have a pending spec"
+end
@@ -0,0 +1,4 @@
+--colour
+--format progress
+--loadby mtime
+--reverse
@@ -0,0 +1,10 @@
+begin
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
+rescue LoadError
+ puts "You need to install rspec in your base app"
+ exit
+end
+
+plugin_spec_dir = File.dirname(__FILE__)
+ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
+

0 comments on commit 276b3e5

Please sign in to comment.