Skip to content
This repository
Browse code

IdentityMap - Adding Weakling and IM Base as concern

  • Loading branch information...
commit 3df4460a742c18af8cc71e41009c9028467d16b5 1 parent 902ae14
Marcin Raczkowski authored August 28, 2010 miloops committed November 19, 2010
1  .gitignore
@@ -20,3 +20,4 @@ railties/test/initializer/root/log
20 20
 railties/doc
21 21
 railties/guides/output
22 22
 railties/tmp
  23
+nbproject
2  Gemfile
@@ -25,6 +25,8 @@ gem "memcache-client", ">= 1.8.5"
25 25
 # AM
26 26
 gem "text-format", "~> 1.0.0"
27 27
 
  28
+gem "weakling", :git => "git://github.com/swistak/weakling.git"
  29
+
28 30
 platforms :mri_18 do
29 31
   gem "system_timer"
30 32
   gem "ruby-debug", ">= 0.10.3"
2  activerecord/lib/active_record.rb
@@ -32,6 +32,7 @@
32 32
 require 'active_support/i18n'
33 33
 require 'active_model'
34 34
 require 'arel'
  35
+require 'weakling'
35 36
 
36 37
 require 'active_record/version'
37 38
 
@@ -79,6 +80,7 @@ module ActiveRecord
79 80
     autoload :Timestamp
80 81
     autoload :Transactions
81 82
     autoload :Validations
  83
+    autoload :IdentityMap
82 84
   end
83 85
 
84 86
   module AttributeMethods
64  activerecord/lib/active_record/identity_map.rb
... ...
@@ -0,0 +1,64 @@
  1
+module ActiveRecord
  2
+  module IdentityMap
  3
+    extend ActiveSupport::Concern
  4
+
  5
+    class << self
  6
+      attr_accessor :repositories
  7
+      attr_accessor :current_repository_name
  8
+      attr_accessor :enabled
  9
+
  10
+      def current
  11
+        repositories[current_repository_name] ||= Weakling::WeakHash.new
  12
+      end
  13
+
  14
+      def with_repository(name = :default, &block)
  15
+        old_repository = self.current_repository_name
  16
+        self.current_repository_name = name
  17
+
  18
+        block.call(current)
  19
+      ensure
  20
+        self.current_repository_name = old_repository
  21
+      end
  22
+
  23
+      def without(&block)
  24
+        old, self.enabled = self.enabled, false
  25
+
  26
+        block.call
  27
+      ensure
  28
+        self.enabled = old
  29
+      end
  30
+
  31
+      def get(class_name, primary_key)
  32
+        current[[class_name, primary_key]]
  33
+      end
  34
+
  35
+      def add(record)
  36
+        current[[record.class.name, record.id]] = record
  37
+      end
  38
+
  39
+      def remove(record)
  40
+        current.delete([record.class.name, record.id])
  41
+      end
  42
+
  43
+      def clear
  44
+        current.clear
  45
+      end
  46
+
  47
+      alias enabled? enabled
  48
+    end
  49
+
  50
+    self.repositories ||= Hash.new
  51
+    self.current_repository_name ||= :default
  52
+    self.enabled = true
  53
+
  54
+    module InstanceMethods
  55
+
  56
+    end
  57
+
  58
+    module ClassMethods
  59
+      def identity_map
  60
+        ActiveRecord::IdentityMap
  61
+      end
  62
+    end
  63
+  end
  64
+end

0 notes on commit 3df4460

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