Skip to content

Commit

Permalink
Only include memoized method into classes where it is really used
Browse files Browse the repository at this point in the history
This way we do not change anything in the global Module and Kernel namespace.
  • Loading branch information
hennk committed Oct 28, 2011
1 parent 17d7ab9 commit ba80cee
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
3 changes: 2 additions & 1 deletion lib/aws/ses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
end

$:.unshift(File.dirname(__FILE__))
require 'ses/extensions'

require 'ses/expirable_memoize'

require 'ses/response'
require 'ses/send_email'
Expand Down
45 changes: 45 additions & 0 deletions lib/aws/ses/expirable_memoize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#:stopdoc:
module AWS
module SES
module ExpirableMemoize
module InstanceMethods
def __method__(depth = 0)
caller[depth][/`([^']+)'/, 1]
end if RUBY_VERSION <= '1.8.7'

def __called_from__
caller[1][/`([^']+)'/, 1]
end if RUBY_VERSION > '1.8.7'

def expirable_memoize(reload = false, storage = nil)
current_method = RUBY_VERSION > '1.8.7' ? __called_from__ : __method__(1)
storage = "@#{storage || current_method}"
if reload
instance_variable_set(storage, nil)
else
if cache = instance_variable_get(storage)
return cache
end
end
instance_variable_set(storage, yield)
end
end

module ClassMethods
def memoized(method_name)
original_method = "unmemoized_#{method_name}_#{Time.now.to_i}"
alias_method original_method, method_name
module_eval(<<-EVAL, __FILE__, __LINE__)
def #{method_name}(reload = false, *args, &block)
expirable_memoize(reload) do
send(:#{original_method}, *args, &block)
end
end
EVAL
end
end
end
end
end

#:startdoc:
39 changes: 0 additions & 39 deletions lib/aws/ses/extensions.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/aws/ses/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module SES
# "Complaints"=>"0"}]

module Info
include AWS::SES::ExpirableMemoize::InstanceMethods
extend AWS::SES::ExpirableMemoize::ClassMethods

# Returns quota information provided by SES
#
# The return format inside the response result will look like:
Expand Down
3 changes: 3 additions & 0 deletions lib/aws/ses/response.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module AWS
module SES
class Response < String
include AWS::SES::ExpirableMemoize::InstanceMethods
extend AWS::SES::ExpirableMemoize::ClassMethods

attr_reader :response, :body, :parsed, :action

def initialize(action, response)
Expand Down
9 changes: 9 additions & 0 deletions test/extensions_test.rb → test/expirable_memoize_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

class KerneltExtensionsTest < Test::Unit::TestCase
class Foo
include AWS::SES::ExpirableMemoize::InstanceMethods
extend AWS::SES::ExpirableMemoize::ClassMethods

def foo
__method__
end
Expand All @@ -16,6 +19,9 @@ def baz
end

class Bar
include AWS::SES::ExpirableMemoize::InstanceMethods
extend AWS::SES::ExpirableMemoize::ClassMethods

def foo
calling_method
end
Expand Down Expand Up @@ -45,6 +51,9 @@ def test___method___depth

class ModuleExtensionsTest < Test::Unit::TestCase
class Foo
include AWS::SES::ExpirableMemoize::InstanceMethods
extend AWS::SES::ExpirableMemoize::ClassMethods

def foo(reload = false)
expirable_memoize(reload) do
Time.now
Expand Down

0 comments on commit ba80cee

Please sign in to comment.