Browse files

Use memoizable

  • Loading branch information...
1 parent 5977aaa commit 4a67d2575ca46c5462c9ecc5c3ca3dd1a42b4e7b @sferik committed Nov 18, 2013
Showing with 7 additions and 105 deletions.
  1. +2 −2 lib/twitter/base.rb
  2. +2 −2 lib/twitter/creatable.rb
  3. +0 −98 lib/twitter/memoizable.rb
  4. +2 −2 lib/twitter/trend_results.rb
  5. +1 −1 twitter.gemspec
View
4 lib/twitter/base.rb
@@ -1,12 +1,12 @@
require 'forwardable'
-require 'twitter/memoizable'
+require 'memoizable'
require 'twitter/null_object'
require 'uri'
module Twitter
class Base
extend Forwardable
- include Twitter::Memoizable
+ include Memoizable
attr_reader :attrs
alias to_h attrs
alias to_hash attrs
View
4 lib/twitter/creatable.rb
@@ -1,9 +1,9 @@
require 'time'
-require 'twitter/memoizable'
+require 'memoizable'
module Twitter
module Creatable
- include Twitter::Memoizable
+ include Memoizable
# Time when the object was created on Twitter
#
View
98 lib/twitter/memoizable.rb
@@ -1,98 +0,0 @@
-require 'thread_safe'
-
-module Twitter
-
- Memory = Class.new(ThreadSafe::Cache)
-
- module Memoizable
-
- class << self
-
- def included(base)
- base.extend(ClassMethods)
- end
-
- end
-
- module ClassMethods
-
- # Memoize a list of methods
- #
- # @example
- # memoize :hash
- #
- # @param [Array<#to_s>] methods
- # a list of methods to memoize
- def memoize(*methods)
- for method in methods
- memoize_method(method)
- end
- end
-
- private
-
- # Memoize the named method
- #
- # @param [#to_s] method_name
- # a method name to memoize
- def memoize_method(method_name)
- method = instance_method(method_name)
- raise ArgumentError, 'Cannot memoize method with nonzero arity' if method.arity > 0
- memoized_methods[method_name] = method
- visibility = method_visibility(method_name)
- define_memoize_method(method)
- send(visibility, method_name)
- end
-
- # Return original method registry
- #
- # @return [Hash<Symbol, UnboundMethod>]
- def memoized_methods
- @memoized_methods ||= ThreadSafe::Cache.new do |_, name|
- raise ArgumentError, "No method #{name.inspect} was memoized"
- end
- end
-
- # Return the method visibility of a method
- #
- # @param [String, Symbol] method
- # the name of the method
- #
- # @return [Symbol]
- def method_visibility(method)
- if private_method_defined?(method)
- :private
- elsif protected_method_defined?(method)
- :protected
- else
- :public
- end
- end
-
- # Define a memoized method that delegates to the original method
- #
- # @param [UnboundMethod] method
- # the method to memoize
- def define_memoize_method(method)
- method_name = method.name.to_sym
- undef_method(method_name)
- define_method(method_name) do
- memory.fetch(method_name) do
- memory[method_name] ||= method.bind(self).call
- end
- end
- end
-
- end
-
- private
-
- # The memoized method results
- #
- # @return [Hash]
- def memory
- @__memory ||= Memory.new
- end
-
- end
-end
View
4 lib/twitter/trend_results.rb
@@ -1,13 +1,13 @@
require 'twitter/creatable'
require 'twitter/enumerable'
-require 'twitter/memoizable'
+require 'memoizable'
require 'twitter/null_object'
module Twitter
class TrendResults
include Twitter::Creatable
include Twitter::Enumerable
- include Twitter::Memoizable
+ include Memoizable
attr_reader :attrs
alias to_h attrs
alias to_hash attrs
View
2 twitter.gemspec
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
spec.add_dependency 'http', '~> 0.5.0'
spec.add_dependency 'http_parser.rb', '~> 0.5.0'
spec.add_dependency 'json', '~> 1.8'
+ spec.add_dependency 'memoizable', '~> 0.2.0'
spec.add_dependency 'simple_oauth', '~> 0.2.0'
- spec.add_dependency 'thread_safe', '~> 0.1.3'
spec.add_development_dependency 'bundler', '~> 1.0'
spec.authors = ["Erik Michaels-Ober", "John Nunemaker", "Wynn Netherland", "Steve Richert", "Steve Agalloco"]
spec.cert_chain = ['certs/sferik.pem']

0 comments on commit 4a67d25

Please sign in to comment.