Skip to content

Commit

Permalink
Remove identity map
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 30, 2013
1 parent e5fc292 commit ec7c2df
Show file tree
Hide file tree
Showing 41 changed files with 79 additions and 348 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 0 additions & 46 deletions etc/sqlite_identity_map.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/twitter/action/favorite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/action/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/twitter/action/list_member_added.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/twitter/action/mention.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/action/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/action/retweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/action/tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/action_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/api/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 2 additions & 48 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/twitter/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +20,6 @@ def keys
:bearer_token,
:endpoint,
:connection_options,
:identity_map,
:middleware,
]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions lib/twitter/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/direct_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions lib/twitter/error/identity_map_key_error.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/twitter/exceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions lib/twitter/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/geo_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit ec7c2df

Please sign in to comment.