Permalink
Browse files

Move Twitter::Exceptable#new_without_self into Twitter::Base

  • Loading branch information...
sferik committed Jun 30, 2013
1 parent ec7c2df commit ae91e0c58981bb2520dd88d547304a103da36e1a
@@ -9,7 +9,7 @@
module Twitter
class ActionFactory < Twitter::Factory
- # Instantiates a new action object
+ # Construct a new action object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
View
@@ -1,5 +1,14 @@
+require 'forwardable'
+
module Twitter
class Base
+ extend Forwardable
+ attr_reader :attrs
+ alias to_h attrs
+ alias to_hash attrs
+ alias to_hsh attrs
+ def_delegators :attrs, :delete, :update
+
# Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
#
# @param attrs [Array, Set, Symbol]
@@ -18,7 +27,7 @@ def self.attr_reader(*attrs)
include mod
end
- # Returns a new object based on the response hash
+ # Construct an object from the response hash
#
# @param response [Hash]
# @return [Twitter::Base]
@@ -43,24 +52,20 @@ def [](method)
nil
end
- # Retrieve the attributes of an object
- #
- # @return [Hash]
- def attrs
- @attrs
- end
- alias to_hash attrs
-
- # Update the attributes of an object
+ # Create a new object from another object
#
- # @param attrs [Hash]
- # @return [Twitter::Base]
- def update(attrs)
- @attrs.update(attrs)
- self
+ # @param klass [Class]
+ # @param key1 [Symbol]
+ # @param key2 [Symbol]
+ def new_without_self(klass, key1, key2)
+ if @attrs[key1]
+ attrs = @attrs.dup
+ value = attrs.delete(key1)
+ klass.new(value.update(key2 => attrs))
+ end
end
- protected
+ private
# @param attr [Symbol]
# @param other [Twitter::Base]
View
@@ -2,7 +2,9 @@ module Twitter
class Cursor
include Enumerable
attr_reader :attrs, :collection
+ alias to_h attrs
alias to_hash attrs
+ alias to_hsh attrs
# Initializes a new Cursor object
#
View
@@ -1,36 +0,0 @@
-module Twitter
- module Exceptable
-
- private
-
- # Return a hash that includes everything but the given keys.
- #
- # @param klass [Class]
- # @param hash [Hash]
- # @param key1 [Symbol]
- # @param key2 [Symbol]
- 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.
- #
- # @param hash [Hash]
- # @param key [Symbol]
- # @return [Hash]
- def except(hash, key)
- except!(hash.dup, key)
- end
-
- # Replaces the hash without the given keys.
- #
- # @param hash [Hash]
- # @param key [Symbol]
- # @return [Hash]
- def except!(hash, key)
- hash.delete(key)
- hash
- end
-
- end
-end
View
@@ -1,7 +1,7 @@
module Twitter
class Factory
- # Instantiates a new action object
+ # Construct a new action object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an :action key.
@@ -5,7 +5,7 @@
module Twitter
class GeoFactory < Twitter::Factory
- # Instantiates a new geo object
+ # Construct a new geo object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
@@ -4,7 +4,7 @@
module Twitter
class MediaFactory < Twitter::Factory
- # Instantiates a new media object
+ # Construct a new media object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing a :type key.
View
@@ -1,12 +1,5 @@
module Twitter
- class RateLimit
- attr_reader :attrs
- alias to_hash attrs
-
- # @return [Twitter::RateLimit]
- def initialize(attrs={})
- @attrs = attrs
- end
+ class RateLimit < Twitter::Base
# @return [Integer]
def limit
@@ -32,14 +25,5 @@ def reset_in
end
alias retry_after reset_in
- # Update the attributes of a RateLimit
- #
- # @param attrs [Hash]
- # @return [Twitter::RateLimit]
- def update(attrs)
- @attrs.update(attrs)
- self
- end
-
end
end
View
@@ -12,7 +12,7 @@ def ==(other)
super || size_equal(other) || attrs_equal(other)
end
- protected
+ private
# @param other [Twitter::Size]
# @return [Boolean]
View
@@ -1,13 +1,11 @@
require 'forwardable'
require 'twitter/creatable'
-require 'twitter/exceptable'
require 'twitter/identity'
module Twitter
class Tweet < Twitter::Identity
extend Forwardable
include Twitter::Creatable
- include Twitter::Exceptable
attr_reader :favorite_count, :favorited, :from_user_id, :from_user_name,
:in_reply_to_screen_name, :in_reply_to_attrs_id, :in_reply_to_status_id,
:in_reply_to_user_id, :lang, :retweet_count, :retweeted, :source, :text,
@@ -108,7 +106,7 @@ def urls
# @return [Twitter::User]
def user
- @user ||= new_without_self(Twitter::User, @attrs, :user, :status)
+ @user ||= new_without_self(Twitter::User, :user, :status)
end
# @note Must include entities in your request for this method to work
View
@@ -1,12 +1,10 @@
require 'twitter/basic_user'
require 'twitter/creatable'
-require 'twitter/exceptable'
module Twitter
class User < Twitter::BasicUser
PROFILE_IMAGE_SUFFIX_REGEX = /_normal(\.gif|\.jpe?g|\.png)$/i
include Twitter::Creatable
- include Twitter::Exceptable
attr_reader :connections, :contributors_enabled, :default_profile,
:default_profile_image, :description, :favourites_count,
:follow_request_sent, :followers_count, :friends_count, :geo_enabled,
@@ -89,7 +87,7 @@ def profile_image_url?
# @return [Twitter::Tweet]
def status
- @status ||= new_without_self(Twitter::Tweet, @attrs, :status, :user)
+ @status ||= new_without_self(Twitter::Tweet, :status, :user)
end
def status?
@@ -24,6 +24,14 @@
end
end
+ describe "#delete" do
+ it 'deletes an attribute and returns its value' do
+ base = Twitter::Base.new(id: 1)
+ expect(base.delete(:id)).to eq(1)
+ expect(base.attrs[:id]).to be_nil
+ end
+ end
+
describe "#update" do
it 'returns a hash of attributes' do
base = Twitter::Base.new(id: 1)

0 comments on commit ae91e0c

Please sign in to comment.