Skip to content
Browse files

IdentityMap - Adding Weakling and IM Base as concern

  • Loading branch information...
1 parent 902ae14 commit 3df4460a742c18af8cc71e41009c9028467d16b5 @swistak swistak committed with miloops Aug 28, 2010
Showing with 69 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +2 −0 Gemfile
  3. +2 −0 activerecord/lib/active_record.rb
  4. +64 −0 activerecord/lib/active_record/identity_map.rb
View
1 .gitignore
@@ -20,3 +20,4 @@ railties/test/initializer/root/log
railties/doc
railties/guides/output
railties/tmp
+nbproject
View
2 Gemfile
@@ -25,6 +25,8 @@ gem "memcache-client", ">= 1.8.5"
# AM
gem "text-format", "~> 1.0.0"
+gem "weakling", :git => "git://github.com/swistak/weakling.git"
+
platforms :mri_18 do
gem "system_timer"
gem "ruby-debug", ">= 0.10.3"
View
2 activerecord/lib/active_record.rb
@@ -32,6 +32,7 @@
require 'active_support/i18n'
require 'active_model'
require 'arel'
+require 'weakling'
require 'active_record/version'
@@ -79,6 +80,7 @@ module ActiveRecord
autoload :Timestamp
autoload :Transactions
autoload :Validations
+ autoload :IdentityMap
end
module AttributeMethods
View
64 activerecord/lib/active_record/identity_map.rb
@@ -0,0 +1,64 @@
+module ActiveRecord
+ module IdentityMap
+ extend ActiveSupport::Concern
+
+ class << self
+ attr_accessor :repositories
+ attr_accessor :current_repository_name
+ attr_accessor :enabled
+
+ def current
+ repositories[current_repository_name] ||= Weakling::WeakHash.new
+ end
+
+ def with_repository(name = :default, &block)
+ old_repository = self.current_repository_name
+ self.current_repository_name = name
+
+ block.call(current)
+ ensure
+ self.current_repository_name = old_repository
+ end
+
+ def without(&block)
+ old, self.enabled = self.enabled, false
+
+ block.call
+ ensure
+ self.enabled = old
+ end
+
+ def get(class_name, primary_key)
+ current[[class_name, primary_key]]
+ end
+
+ def add(record)
+ current[[record.class.name, record.id]] = record
+ end
+
+ def remove(record)
+ current.delete([record.class.name, record.id])
+ end
+
+ def clear
+ current.clear
+ end
+
+ alias enabled? enabled
+ end
+
+ self.repositories ||= Hash.new
+ self.current_repository_name ||= :default
+ self.enabled = true
+
+ module InstanceMethods
+
+ end
+
+ module ClassMethods
+ def identity_map
+ ActiveRecord::IdentityMap
+ end
+ end
+ end
+end

0 comments on commit 3df4460

Please sign in to comment.
Something went wrong with that request. Please try again.