forked from drewblas/aws-ses
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only include memoized method into classes where it is really used
This way we do not change anything in the global Module and Kernel namespace.
- Loading branch information
Showing
6 changed files
with
62 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters