Skip to content

Commit

Permalink
Deprecate predicate methods without question marks
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 21, 2014
1 parent a4fa473 commit 0305a85
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ParameterLists:
CountKeywordArgs: true

ClassLength:
Max: 90
Max: 100

LineLength:
Enabled: false
Expand Down
20 changes: 18 additions & 2 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def attr_reader(*attrs)
end
end

def predicate_attr_reader(*attrs)
attrs.each do |attr|
define_predicate_method(attr)
deprecate_attribute_method(attr)
end
end

# Define object methods from attributes
#
# @param klass [Symbol]
Expand Down Expand Up @@ -59,8 +66,6 @@ def display_uri_attr_reader
alias_method(:display_url?, :display_uri?)
end

private

# Dynamically define a method for a URI
#
# @param key1 [Symbol]
Expand Down Expand Up @@ -93,6 +98,17 @@ def define_attribute_method(key1, klass = nil, key2 = nil)
memoize(key1)
end

# Dynamically define a method for an attribute
#
# @param key [Symbol]
def deprecate_attribute_method(key)
define_method(key) do ||
warn "#{Kernel.caller.first}: [DEPRECATION] ##{key} is deprecated. Use ##{key}? instead."
@attrs[key]
end
memoize(key)
end

# Dynamically define a predicate method for an attribute
#
# @param key1 [Symbol]
Expand Down
10 changes: 6 additions & 4 deletions lib/twitter/basic_user.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require 'twitter/identity'
require 'twitter/utils'

module Twitter
class BasicUser < Twitter::Identity
attr_reader :following, :screen_name
alias_method :handle, :screen_name
alias_method :username, :screen_name
alias_method :user_name, :screen_name
attr_reader :screen_name
deprecate_alias :handle, :screen_name
deprecate_alias :username, :screen_name
deprecate_alias :user_name, :screen_name
predicate_attr_reader :following
end
end
5 changes: 3 additions & 2 deletions lib/twitter/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
module Twitter
class List < Twitter::Identity
include Twitter::Creatable
attr_reader :description, :following, :full_name, :member_count,
:mode, :name, :slug, :subscriber_count
attr_reader :description, :full_name, :member_count, :mode, :name, :slug,
:subscriber_count
object_attr_reader :User, :user
predicate_attr_reader :following

# @return [Addressable::URI] The URI to the list members.
def members_uri
Expand Down
7 changes: 4 additions & 3 deletions lib/twitter/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

module Twitter
class Settings < Twitter::Base
attr_reader :always_use_https, :discoverable_by_email, :geo_enabled,
:language, :protected, :screen_name, :show_all_inline_media,
:sleep_time, :time_zone
attr_reader :language, :screen_name, :sleep_time, :time_zone
object_attr_reader :Place, :trend_location
predicate_attr_reader :always_use_https, :discoverable_by_email,
:geo_enabled, :protected, :show_all_inline_media,
:use_cookie_personalization
end
end
4 changes: 2 additions & 2 deletions lib/twitter/source_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Twitter
class SourceUser < Twitter::BasicUser
attr_reader :all_replies, :blocking, :can_dm, :followed_by, :marked_spam,
:notifications_enabled, :want_retweets
predicate_attr_reader :all_replies, :blocking, :can_dm, :followed_by,
:marked_spam, :notifications_enabled, :want_retweets
end
end
2 changes: 1 addition & 1 deletion lib/twitter/target_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Twitter
class TargetUser < Twitter::BasicUser
attr_reader :followed_by
predicate_attr_reader :followed_by
end
end
3 changes: 2 additions & 1 deletion lib/twitter/trend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
module Twitter
class Trend < Twitter::Base
include Equalizer.new(:name)
attr_reader :events, :name, :promoted_content, :query
attr_reader :events, :name, :query
predicate_attr_reader :promoted_content
uri_attr_reader :uri
end
end
8 changes: 4 additions & 4 deletions lib/twitter/tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ module Twitter
class Tweet < Twitter::Identity
include Twitter::Creatable
include Twitter::Entities
attr_reader :favorite_count, :favorited, :filter_level,
: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, :truncated
attr_reader :favorite_count, :filter_level, :in_reply_to_screen_name,
:in_reply_to_attrs_id, :in_reply_to_status_id, :in_reply_to_user_id,
:lang, :retweet_count, :source, :text
deprecate_alias :favorites_count, :favorite_count
deprecate_alias :favoriters_count, :favorite_count
alias_method :in_reply_to_tweet_id, :in_reply_to_status_id
Expand All @@ -23,6 +22,7 @@ class Tweet < Twitter::Identity
alias_method :retweet?, :retweeted_status?
alias_method :retweeted_tweet?, :retweeted_status?
object_attr_reader :User, :user, :status
predicate_attr_reader :favorited, :retweeted, :truncated

# @note May be > 140 characters.
# @return [String]
Expand Down
24 changes: 13 additions & 11 deletions lib/twitter/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ class User < Twitter::BasicUser
include Twitter::Creatable
include Twitter::Profile
include Memoizable
attr_reader :connections, :contributors_enabled, :default_profile,
:default_profile_image, :description, :favourites_count,
:follow_request_sent, :followers_count, :friends_count,
:geo_enabled, :is_translator, :lang, :listed_count, :location,
:name, :notifications, :profile_background_color,
attr_reader :connections, :description, :favourites_count,
:followers_count, :friends_count, :lang, :listed_count,
:location, :name, :profile_background_color,
:profile_background_image_url,
:profile_background_image_url_https, :profile_background_tile,
:profile_link_color, :profile_sidebar_border_color,
:profile_sidebar_fill_color, :profile_text_color,
:profile_use_background_image, :protected, :statuses_count,
:time_zone, :utc_offset, :verified
:profile_background_image_url_https, :profile_link_color,
:profile_sidebar_border_color, :profile_sidebar_fill_color,
:profile_text_color, :statuses_count, :time_zone, :utc_offset
alias_method :favorites_count, :favourites_count
remove_method :favourites_count
alias_method :profile_background_image_uri, :profile_background_image_url
alias_method :profile_background_image_uri_https, :profile_background_image_url_https
alias_method :translator?, :is_translator
alias_method :tweets_count, :statuses_count
object_attr_reader :Tweet, :status, :user
alias_method :tweet, :status
alias_method :tweet?, :status?
alias_method :tweeted?, :status?
predicate_attr_reader :contributors_enabled, :default_profile,
:default_profile_image, :follow_request_sent,
:geo_enabled, :needs_phone_verification,
:notifications, :protected, :profile_background_tile,
:profile_use_background_image, :suspended, :verified
define_predicate_method :translator, :is_translator
define_predicate_method :translation_enabled, :is_translation_enabled

class << self
private
Expand Down
2 changes: 1 addition & 1 deletion spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

SimpleCov.start do
add_filter '/spec/'
minimum_coverage(99.39)
minimum_coverage(99.3)
end

require 'twitter'
Expand Down

0 comments on commit 0305a85

Please sign in to comment.