diff --git a/lib/merb_resource_controller.rb b/lib/merb_resource_controller.rb index 76b85a7..6093a8e 100644 --- a/lib/merb_resource_controller.rb +++ b/lib/merb_resource_controller.rb @@ -1,9 +1,10 @@ # make sure we're running inside Merb if defined?(Merb::Plugins) - # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it + # Merb gave me a Merb::Plugins.config hash + # i felt free to put my stuff in my piece of it Merb::Plugins.config[:merb_resource_controller] = { - :orm => :datamapper + :identity_map => true } Merb::BootLoader.before_app_loads do @@ -12,6 +13,9 @@ require DIR / 'resource_proxy' require DIR / 'actions' require DIR / 'resource_controller' + if Merb::Plugins.config[:merb_resource_controller][:identity_map] + require DIR / 'identity_map_support' + end end Merb::BootLoader.after_app_loads do diff --git a/lib/merb_resource_controller/identity_map_support.rb b/lib/merb_resource_controller/identity_map_support.rb new file mode 100644 index 0000000..d1346f2 --- /dev/null +++ b/lib/merb_resource_controller/identity_map_support.rb @@ -0,0 +1,41 @@ +module Merb + module ResourceController + module DM + + # to be extended into controller + module IdentityMapSupport + + def enable_identity_map(options = {}) + @action_timeout = options[:action_timeout] || 0 + # maybe also useful for retries + class_inheritable_accessor :action_timeout + include InstanceMethods + end + + module InstanceMethods + + # taken from: + # http://datamapper.org/doku.php?id=docs:identity_map + def _call_action(*) + repository do |r| # enable identity_map + Merb.logger.info "Inside #{r.name} repository block" + if self.class.respond_to?(:action_timeout) + return super if self.class.action_timeout < 1 + # more information on system_timer: + # http://adam.blog.heroku.com/past/2008/6/17/battling_wedged_mongrels_with_a/ + require 'system_timer' # laziness + SystemTimer.timeout(self.class.action_timeout) do + super + end + else + super + end + end + end + + end + + end + end + end +end \ No newline at end of file diff --git a/spec/integration/app/controllers/application.rb b/spec/integration/app/controllers/application.rb index 74cb1e0..6f2d0e6 100644 --- a/spec/integration/app/controllers/application.rb +++ b/spec/integration/app/controllers/application.rb @@ -1,3 +1,5 @@ class Application < Merb::Controller extend Merb::ResourceController::Mixin::ClassMethods + extend Merb::ResourceController::DM::IdentityMapSupport + enable_identity_map :action_timeout => 1 end \ No newline at end of file