Permalink
Browse files

Remove identity map

  • Loading branch information...
1 parent e5fc292 commit ec7c2df78a200e2b0b1cd3a40983c6ce9dee552d @sferik committed Jun 30, 2013
Showing with 79 additions and 348 deletions.
  1. +0 −13 README.md
  2. +0 −46 etc/sqlite_identity_map.rb
  3. +1 −1 lib/twitter/action/favorite.rb
  4. +2 −2 lib/twitter/action/follow.rb
  5. +3 −3 lib/twitter/action/list_member_added.rb
  6. +3 −3 lib/twitter/action/mention.rb
  7. +2 −2 lib/twitter/action/reply.rb
  8. +2 −2 lib/twitter/action/retweet.rb
  9. +1 −1 lib/twitter/action/tweet.rb
  10. +1 −1 lib/twitter/action_factory.rb
  11. +1 −1 lib/twitter/api/utils.rb
  12. +2 −48 lib/twitter/base.rb
  13. +1 −2 lib/twitter/configurable.rb
  14. +1 −1 lib/twitter/configuration.rb
  15. +1 −1 lib/twitter/cursor.rb
  16. +0 −5 lib/twitter/default.rb
  17. +2 −2 lib/twitter/direct_message.rb
  18. +0 −8 lib/twitter/error/identity_map_key_error.rb
  19. +2 −2 lib/twitter/exceptable.rb
  20. +2 −3 lib/twitter/factory.rb
  21. +1 −1 lib/twitter/geo_factory.rb
  22. +0 −22 lib/twitter/identity.rb
  23. +0 −22 lib/twitter/identity_map.rb
  24. +1 −1 lib/twitter/list.rb
  25. +1 −1 lib/twitter/media/photo.rb
  26. +1 −1 lib/twitter/media_factory.rb
  27. +1 −1 lib/twitter/place.rb
  28. +1 −1 lib/twitter/profile_banner.rb
  29. +2 −2 lib/twitter/relationship.rb
  30. +1 −1 lib/twitter/search_results.rb
  31. +1 −1 lib/twitter/settings.rb
  32. +1 −1 lib/twitter/suggestion.rb
  33. +6 −6 lib/twitter/tweet.rb
  34. +2 −2 lib/twitter/user.rb
  35. +0 −1 spec/helper.rb
  36. +7 −7 spec/twitter/action_factory_spec.rb
  37. +18 −100 spec/twitter/base_spec.rb
  38. +3 −4 spec/twitter/client_spec.rb
  39. +3 −3 spec/twitter/geo_factory_spec.rb
  40. +0 −21 spec/twitter/identifiable_spec.rb
  41. +2 −2 spec/twitter/media_factory_spec.rb
View
@@ -435,19 +435,6 @@ library will raise `Twitter::Error::TooManyRequests` for all rate limit errors.
The `Twitter::Error::EnhanceYourCalm` class has been aliased to
`Twitter::Error::TooManyRequests`.
-#### Identity Map
-In version 4, the identity map is [disabled by default][disabled]. If you want
-to enable this feature, you can use the [default identity map][default] or
-[write a custom identity map][custom].
-
-```ruby
-Twitter.identity_map = Twitter::IdentityMap
-```
-
-[disabled]: https://github.com/sferik/twitter/commit/c6c5960bea998abdc3e82cbb8dd68766a2df52e1
-[default]: lib/twitter/identity_map.rb
-[custom]: etc/sqlite_identity_map.rb
-
## Copyright
Copyright (c) 2006-2013 John Nunemaker, Wynn Netherland, Erik Michaels-Ober, Steve Richert.
See [LICENSE][] for details.
View
@@ -1,46 +0,0 @@
-require 'sqlite3'
-
-module Twitter
- class SqliteIdentityMap
- include SQLite3
-
- # It should be painfully obvious that this code is purely experimental and
- # should never be used in a production environment.
- GC.disable
-
- def initialize
- @database = Database.new(":memory:")
- end
-
- # @param id
- # @return [Object]
- def fetch(id)
- create_table
- @select ||= @database.prepare("SELECT object_id FROM identity_map WHERE id = :id")
- row = @select.execute!(id: id).first
- ObjectSpace._id2ref(row.first) unless row.nil?
- end
-
- # @param id
- # @param object
- # @return [Object]
- def store(id, object)
- create_table
- @insert ||= @database.prepare("INSERT INTO identity_map(id, object_id) VALUES (:id, :object_id)")
- @insert.execute(id: id, object_id: object.object_id)
- object
- rescue SQLite3::ConstraintException
- @delete ||= @database.prepare("DELETE FROM identity_map WHERE id = :id")
- @delete.execute(id: id)
- retry
- end
-
- private
-
- def create_table
- @database.execute("CREATE TABLE IF NOT EXISTS identity_map(id VARCHAR NOT NULL, object_id INTEGER)")
- @database.execute("CREATE UNIQUE INDEX IF NOT EXISTS index_identity_map_on_id ON identity_map(id)")
- end
-
- end
-end
@@ -10,7 +10,7 @@ class Favorite < Twitter::Action::Tweet
# @return [Array<Twitter::Tweet>]
def targets
@targets = Array(@attrs[:targets]).map do |tweet|
- Twitter::Tweet.fetch_or_new(tweet)
+ Twitter::Tweet.new(tweet)
end
end
@@ -12,7 +12,7 @@ class Follow < Twitter::Base
# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs[:sources]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -21,7 +21,7 @@ def sources
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs[:targets]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -12,7 +12,7 @@ class ListMemberAdded < Twitter::Base
# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs[:sources]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -21,7 +21,7 @@ def sources
# @return [Array<Twitter::List>]
def target_objects
@target_objects = Array(@attrs[:target_objects]).map do |list|
- Twitter::List.fetch_or_new(list)
+ Twitter::List.new(list)
end
end
@@ -30,7 +30,7 @@ def target_objects
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs[:targets]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -12,7 +12,7 @@ class Mention < Twitter::Base
# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs[:sources]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -28,7 +28,7 @@ def source
# @return [Array<Twitter::Tweet>]
def target_objects
@target_objects = Array(@attrs[:target_objects]).map do |tweet|
- Twitter::Tweet.fetch_or_new(tweet)
+ Twitter::Tweet.new(tweet)
end
end
@@ -37,7 +37,7 @@ def target_objects
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs[:targets]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -9,7 +9,7 @@ class Reply < Twitter::Action::Tweet
# @return [Array<Twitter::Tweet>]
def target_objects
@target_objects = Array(@attrs[:target_objects]).map do |tweet|
- Twitter::Tweet.fetch_or_new(tweet)
+ Twitter::Tweet.new(tweet)
end
end
@@ -18,7 +18,7 @@ def target_objects
# @return [Array<Twitter::Tweet>]
def targets
@targets = Array(@attrs[:targets]).map do |tweet|
- Twitter::Tweet.fetch_or_new(tweet)
+ Twitter::Tweet.new(tweet)
end
end
@@ -9,7 +9,7 @@ class Retweet < Twitter::Action::Tweet
# @return [Array<Twitter::Tweet>]
def target_objects
@target_objects = Array(@attrs[:target_objects]).map do |tweet|
- Twitter::Tweet.fetch_or_new(tweet)
+ Twitter::Tweet.new(tweet)
end
end
@@ -18,7 +18,7 @@ def target_objects
# @return [Array<Twitter::User>]
def targets
@targets = Array(@attrs[:targets]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -10,7 +10,7 @@ class Tweet < Twitter::Base
# @return [Array<Twitter::User>]
def sources
@sources = Array(@attrs[:sources]).map do |user|
- Twitter::User.fetch_or_new(user)
+ Twitter::User.new(user)
end
end
@@ -14,7 +14,7 @@ class ActionFactory < Twitter::Factory
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
# @return [Twitter::Action]
- def self.fetch_or_new(attrs={})
+ def self.new(attrs={})
super(:action, Twitter::Action, attrs)
end
View
@@ -57,7 +57,7 @@ def objects_from_response(klass, request_method, path, options={})
# @return [Array]
def objects_from_array(klass, array)
array.map do |element|
- klass.fetch_or_new(element)
+ klass.new(element)
end
end
View
@@ -1,5 +1,3 @@
-require 'twitter/error/identity_map_key_error'
-
module Twitter
class Base
# Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
@@ -20,64 +18,20 @@ def self.attr_reader(*attrs)
include mod
end
- # return [Twitter::IdentityMap]
- def self.identity_map
- return unless Twitter.identity_map
- @identity_map = Twitter.identity_map.new unless defined?(@identity_map) && @identity_map.class == Twitter.identity_map
- @identity_map
- end
-
- # Retrieves an object from the identity map.
- #
- # @param attrs [Hash]
- # @return [Twitter::Base]
- def self.fetch(attrs)
- return unless identity_map
- if object = identity_map.fetch(Marshal.dump(attrs))
- return object
- end
- return yield if block_given?
- raise Twitter::Error::IdentityMapKeyError, "key not found"
- end
-
- # Stores an object in the identity map.
- #
- # @param object [Object]
- # @return [Twitter::Base]
- def self.store(object)
- return object unless identity_map
- identity_map.store(Marshal.dump(object.attrs), object)
- end
-
# Returns a new object based on the response hash
#
# @param response [Hash]
# @return [Twitter::Base]
def self.from_response(response={})
- fetch_or_new(response[:body])
- end
-
- # Retrieves an object from the identity map, or stores it in the
- # identity map if it doesn't already exist.
- #
- # @param attrs [Hash]
- # @return [Twitter::Base]
- def self.fetch_or_new(attrs={})
- return unless attrs
- return new(attrs) unless identity_map
-
- fetch(attrs) do
- object = new(attrs)
- store(object)
- end
+ new(response[:body])
end
# Initializes a new object
#
# @param attrs [Hash]
# @return [Twitter::Base]
def initialize(attrs={})
- @attrs = attrs
+ @attrs = attrs || {}
end
# Fetches an attribute of an object using hash notation
@@ -6,7 +6,7 @@ module Twitter
module Configurable
extend Forwardable
attr_writer :consumer_secret, :oauth_token, :oauth_token_secret, :bearer_token
- attr_accessor :consumer_key, :endpoint, :connection_options, :identity_map, :middleware
+ attr_accessor :consumer_key, :endpoint, :connection_options, :middleware
def_delegator :options, :hash
class << self
@@ -20,7 +20,6 @@ def keys
:bearer_token,
:endpoint,
:connection_options,
- :identity_map,
:middleware,
]
end
@@ -10,7 +10,7 @@ class Configuration < Twitter::Base
# @return [Array<Twitter::Size>]
def photo_sizes
@photo_sizes ||= Array(@attrs[:photo_sizes]).each_with_object({}) do |(key, value), object|
- object[key] = Twitter::Size.fetch_or_new(value)
+ object[key] = Twitter::Size.new(value)
end
end
View
@@ -33,7 +33,7 @@ def initialize(attrs, collection_name, klass, client, method_name, method_option
@method_options = method_options
@collection = Array(attrs[collection_name.to_sym]).map do |item|
if klass
- klass.fetch_or_new(item)
+ klass.new(item)
else
item
end
View
@@ -21,7 +21,6 @@ module Default
timeout: 10,
},
} unless defined? Twitter::Default::CONNECTION_OPTIONS
- IDENTITY_MAP = false unless defined? Twitter::Default::IDENTITY_MAP
MIDDLEWARE = Faraday::Builder.new do |builder|
# Convert file uploads to Faraday::UploadIO objects
builder.use Twitter::Request::MultipartWithFile
@@ -85,10 +84,6 @@ def connection_options
CONNECTION_OPTIONS
end
- def identity_map
- IDENTITY_MAP
- end
-
# @note Faraday's middleware stack implementation is comparable to that of Rack middleware. The order of middleware is important: the first middleware on the list wraps all others, while the last middleware is the innermost one.
# @see https://github.com/technoweenie/faraday#advanced-middleware-usage
# @see http://mislav.uniqpath.com/2011/07/faraday-advanced-http/
@@ -9,12 +9,12 @@ class DirectMessage < Twitter::Identity
# @return [Twitter::User]
def recipient
- @recipient ||= Twitter::User.fetch_or_new(@attrs[:recipient])
+ @recipient ||= Twitter::User.new(@attrs[:recipient]) unless @attrs[:recipient].nil?
end
# @return [Twitter::User]
def sender
- @sender ||= Twitter::User.fetch_or_new(@attrs[:sender])
+ @sender ||= Twitter::User.new(@attrs[:sender]) unless @attrs[:sender].nil?
end
end
@@ -1,8 +0,0 @@
-require 'twitter/error'
-
-module Twitter
- class Error
- class IdentityMapKeyError < ::KeyError
- end
- end
-end
@@ -9,8 +9,8 @@ module Exceptable
# @param hash [Hash]
# @param key1 [Symbol]
# @param key2 [Symbol]
- def fetch_or_new_without_self(klass, hash, key1, key2)
- klass.fetch_or_new(hash.dup[key1].merge(key2 => except(hash, key1))) unless hash[key1].nil?
+ def new_without_self(klass, hash, key1, key2)
+ klass.new(hash.dup[key1].merge(key2 => except(hash, key1))) unless hash[key1].nil?
end
# Return a hash that includes everything but the given keys.
View
@@ -6,12 +6,11 @@ class Factory
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
# @return [Twitter::Action::Favorite, Twitter::Action::Follow, Twitter::Action::ListMemberAdded, Twitter::Action::Mention, Twitter::Action::Reply, Twitter::Action::Retweet]
- def self.fetch_or_new(method, klass, attrs={})
- return unless attrs
+ def self.new(method, klass, attrs={})
type = attrs.delete(method.to_sym)
if type
const_name = type.gsub(/\/(.?)/){"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/){$1.upcase}
- klass.const_get(const_name.to_sym).fetch_or_new(attrs)
+ klass.const_get(const_name.to_sym).new(attrs)
else
raise ArgumentError, "argument must have :#{method} key"
end
@@ -10,7 +10,7 @@ class GeoFactory < Twitter::Factory
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
# @return [Twitter::Geo]
- def self.fetch_or_new(attrs={})
+ def self.new(attrs={})
super(:type, Twitter::Geo, attrs)
end
Oops, something went wrong.

0 comments on commit ec7c2df

Please sign in to comment.