Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
IdentityMap - Adding Weakling and IM Base as concern
Browse files Browse the repository at this point in the history
  • Loading branch information
swistak authored and miloops committed Nov 19, 2010
1 parent 902ae14 commit 3df4460
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ railties/test/initializer/root/log
railties/doc
railties/guides/output
railties/tmp
nbproject
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions activerecord/lib/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
require 'active_support/i18n'
require 'active_model'
require 'arel'
require 'weakling'

require 'active_record/version'

Expand Down Expand Up @@ -79,6 +80,7 @@ module ActiveRecord
autoload :Timestamp
autoload :Transactions
autoload :Validations
autoload :IdentityMap
end

module AttributeMethods
Expand Down
64 changes: 64 additions & 0 deletions activerecord/lib/active_record/identity_map.rb
Original file line number Diff line number Diff line change
@@ -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.