Skip to content

Commit

Permalink
Make all accessors lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 18, 2011
1 parent 6732706 commit 1de9147
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 100 deletions.
2 changes: 2 additions & 0 deletions lib/twitter/base.rb
@@ -1,5 +1,7 @@
module Twitter
class Base
attr_accessor :attributes
alias :to_hash :attributes

def self.lazy_attr_reader(*attributes)
attributes.each do |attribute|
Expand Down
9 changes: 3 additions & 6 deletions lib/twitter/configuration.rb
Expand Up @@ -4,16 +4,13 @@

module Twitter
class Configuration < Twitter::Base
attr_reader :photo_sizes
lazy_attr_reader :characters_reserved_per_media, :max_media_per_upload,
:non_username_paths, :photo_size_limit, :short_url_length, :short_url_length_https

def initialize(attributes={})
attributes = attributes.dup
@photo_sizes = attributes.delete('photo_sizes').each_with_object({}) do |(key, value), object|
def photo_sizes
@photo_sizes ||= Array(@attributes['photo_sizes']).each_with_object({}) do |(key, value), object|
object[key] = Twitter::Size.new(value)
end unless attributes['photo_sizes'].nil?
super(attributes)
end
end

end
Expand Down
7 changes: 2 additions & 5 deletions lib/twitter/creatable.rb
Expand Up @@ -2,12 +2,9 @@

module Twitter
module Creatable
attr_reader :created_at

def initialize(attributes={})
attributes = attributes.dup
@created_at = Time.parse(attributes.delete('created_at')) unless attributes['created_at'].nil?
super(attributes)
def created_at
@created_at ||= Time.parse(@attributes['created_at']) unless @attributes['created_at'].nil?
end

end
Expand Down
7 changes: 3 additions & 4 deletions lib/twitter/cursor.rb
Expand Up @@ -9,18 +9,17 @@ class Cursor < Twitter::Base
alias :previous :previous_cursor

def initialize(attributes, method, klass=nil)
attributes = attributes.dup
@collection = attributes.delete(method.to_s).map do |item|
super(attributes)
@collection = Array(attributes[method.to_s]).map do |item|
if klass
klass.new(item)
else
item
end
end unless attributes[method.to_s].nil?
end
singleton_class.class_eval do
alias_method method.to_sym, :collection
end
super(attributes)
end

def first?
Expand Down
16 changes: 8 additions & 8 deletions lib/twitter/direct_message.rb
Expand Up @@ -5,19 +5,19 @@
module Twitter
class DirectMessage < Twitter::Base
include Twitter::Creatable
attr_reader :recipient, :sender
lazy_attr_reader :id, :text

def initialize(attributes={})
attributes = attributes.dup
@recipient = Twitter::User.new(attributes.delete('recipient')) unless attributes['recipient'].nil?
@sender = Twitter::User.new(attributes.delete('sender')) unless attributes['sender'].nil?
super(attributes)
end

def ==(other)
super || (other.class == self.class && other.id == self.id)
end

def recipient
@recipient ||= Twitter::User.new(@attributes['recipient']) unless @attributes['recipient'].nil?
end

def sender
@sender ||= Twitter::User.new(@attributes['sender']) unless @attributes['sender'].nil?
end

end
end
11 changes: 4 additions & 7 deletions lib/twitter/list.rb
Expand Up @@ -3,20 +3,17 @@

module Twitter
class List < Twitter::Base
attr_reader :user
lazy_attr_reader :description, :following, :full_name, :id, :member_count,
:mode, :name, :slug, :subscriber_count, :uri
alias :following? :following

def initialize(attributes={})
attributes = attributes.dup
@user = Twitter::User.new(attributes.delete('user')) unless attributes['user'].nil?
super(attributes)
end

def ==(other)
super || (other.class == self.class && other.id == self.id)
end

def user
@user ||= Twitter::User.new(@attributes['user']) unless @attributes['user'].nil?
end

end
end
15 changes: 6 additions & 9 deletions lib/twitter/photo.rb
Expand Up @@ -4,21 +4,18 @@

module Twitter
class Photo < Twitter::Base
attr_reader :sizes
lazy_attr_reader :display_url, :expanded_url, :id, :indices, :media_url,
:media_url_https, :url

def initialize(attributes={})
attributes = attributes.dup
@sizes = attributes['sizes'].each_with_object({}) do |(key, value), object|
object[key] = Twitter::Size.new(value)
end unless attributes['sizes'].nil?
super(attributes)
end

def ==(other)
super || (other.class == self.class && other.id == self.id)
end

def sizes
@sizes ||= Array(@attributes['sizes']).each_with_object({}) do |(key, value), object|
object[key] = Twitter::Size.new(value)
end
end

end
end
26 changes: 16 additions & 10 deletions lib/twitter/place.rb
Expand Up @@ -3,22 +3,28 @@

module Twitter
class Place < Twitter::Base
attr_reader :bounding_box, :country_code, :parent_id, :place_type
lazy_attr_reader :attributes, :country, :full_name, :id, :name, :url,
:woeid

def initialize(attributes={})
attributes = attributes.dup
@bounding_box = Twitter::GeoFactory.new(attributes['bounding_box']) unless attributes['bounding_box'].nil?
@country_code = attributes['country_code'] || attributes['countryCode']
@parent_id = attributes['parentid']
@place_type = attributes['place_type'] || attributes['placeType'] && attributes['placeType']['name']
super(attributes)
end

def ==(other)
super || (other.class == self.class && other.id == self.id)
end

def bounding_box
@bounding_box ||= Twitter::GeoFactory.new(@attributes['bounding_box']) unless @attributes['bounding_box'].nil?
end

def country_code
@country_code ||= @attributes['country_code'] || @attributes['countryCode']
end

def parent_id
@parent_id ||= @attributes['parentid']
end

def place_type
@place_type ||= @attributes['place_type'] || @attributes['placeType'] && @attributes['placeType']['name']
end

end
end
7 changes: 2 additions & 5 deletions lib/twitter/rate_limit_status.rb
Expand Up @@ -2,13 +2,10 @@

module Twitter
class RateLimitStatus < Twitter::Base
attr_reader :reset_time
lazy_attr_reader :hourly_limit, :remaining_hits, :reset_time_in_seconds

def initialize(attributes={})
attributes = attributes.dup
@reset_time = Time.parse(attributes['reset_time']) unless attributes['reset_time'].nil?
super(attributes)
def reset_time
@reset_time ||= Time.parse(@attributes['reset_time']) unless @attributes['reset_time'].nil?
end

end
Expand Down
12 changes: 6 additions & 6 deletions lib/twitter/relationship.rb
Expand Up @@ -3,13 +3,13 @@

module Twitter
class Relationship < Twitter::Base
attr_reader :source, :target

def initialize(attributes={})
attributes = attributes.dup
@source = Twitter::User.new(attributes.delete('source')) unless attributes['source'].nil?
@target = Twitter::User.new(attributes.delete('target')) unless attributes['target'].nil?
super(attributes)
def source
@source ||= Twitter::User.new(@attributes['source']) unless @attributes['source'].nil?
end

def target
@target ||= Twitter::User.new(@attributes['target']) unless @attributes['target'].nil?
end

end
Expand Down
7 changes: 2 additions & 5 deletions lib/twitter/settings.rb
Expand Up @@ -3,16 +3,13 @@

module Twitter
class Settings < Twitter::Base
attr_reader :trend_location
lazy_attr_reader :always_use_https, :discoverable_by_email, :geo_enabled,
:language, :protected, :screen_name, :show_all_inline_media, :sleep_time,
:time_zone
alias :protected? :protected

def initialize(attributes={})
attributes = attributes.dup
@trend_location = Twitter::Place.new(attributes.delete('trend_location').first) unless attributes['trend_location'].nil? || attributes['trend_location'].empty?
super(attributes)
def trend_location
@trend_location ||= Twitter::Place.new(@attributes['trend_location'].first) unless @attributes['trend_location'].nil? || @attributes['trend_location'].empty?
end

end
Expand Down
51 changes: 34 additions & 17 deletions lib/twitter/status.rb
Expand Up @@ -10,35 +10,52 @@
module Twitter
class Status < Twitter::Base
include Twitter::Creatable
attr_reader :geo, :hashtags, :media, :metadata, :place, :urls, :user,
:user_mentions
lazy_attr_reader :favorited, :from_user, :from_user_id, :id,
:in_reply_to_screen_name, :in_reply_to_attributes_id, :in_reply_to_user_id,
:iso_language_code, :profile_image_url, :retweet_count, :retweeted,
:source, :text, :to_user, :to_user_id, :truncated
alias :favorited? :favorited
alias :mentions :user_mentions
alias :retweeted? :retweeted
alias :truncated? :truncated

def initialize(attributes={})
attributes = attributes.dup
@geo = Twitter::GeoFactory.new(attributes.delete('geo')) unless attributes['geo'].nil?
@hashtags = Twitter::Extractor.extract_hashtags(attributes['text']) unless attributes['text'].nil?
@media = attributes['entities'].delete('media').map do |media|
def ==(other)
super || (other.class == self.class && other.id == self.id)
end

def geo
@geo ||= Twitter::GeoFactory.new(@attributes['geo']) unless @attributes['geo'].nil?
end

def hashtags
@hashtags ||= Twitter::Extractor.extract_hashtags(@attributes['text']) unless @attributes['text'].nil?
end

def media
@media ||= Array(@attributes['entities']['media']).map do |media|
Twitter::MediaFactory.new(media)
end unless attributes['entities'].nil? || attributes['entities']['media'].nil?
@metadata = Twitter::Metadata.new(attributes.delete('metadata')) unless attributes['metadata'].nil?
@place = Twitter::Place.new(attributes.delete('place')) unless attributes['place'].nil?
@urls = Twitter::Extractor.extract_urls(attributes['text']) unless attributes['text'].nil?
@user = Twitter::User.new(attributes.delete('user')) unless attributes['user'].nil?
@user_mentions = Twitter::Extractor.extract_mentioned_screen_names(attributes['text']) unless attributes['text'].nil?
super(attributes)
end unless @attributes['entities'].nil?
end

def ==(other)
super || (other.class == self.class && other.id == self.id)
def metadata
@metadata ||= Twitter::Metadata.new(@attributes['metadata']) unless @attributes['metadata'].nil?
end

def place
@place ||= Twitter::Place.new(@attributes['place']) unless @attributes['place'].nil?
end

def urls
@urls ||= Twitter::Extractor.extract_urls(@attributes['text']) unless @attributes['text'].nil?
end

def user
@user ||= Twitter::User.new(@attributes['user']) unless @attributes['user'].nil?
end

def user_mentions
@user_mentions ||= Twitter::Extractor.extract_mentioned_screen_names(@attributes['text']) unless @attributes['text'].nil?
end
alias :mentions :user_mentions

end
end
15 changes: 6 additions & 9 deletions lib/twitter/suggestion.rb
Expand Up @@ -3,20 +3,17 @@

module Twitter
class Suggestion < Twitter::Base
attr_reader :users
lazy_attr_reader :name, :size, :slug

def initialize(attributes={})
attributes = attributes.dup
@users = attributes.delete('users').map do |user|
Twitter::User.new(user)
end unless attributes['users'].nil?
super(attributes)
end

def ==(other)
super || (other.class == self.class && other.slug == self.slug)
end

def users
@users = Array(@attributes['users']).map do |user|
Twitter::User.new(user)
end
end

end
end
11 changes: 4 additions & 7 deletions lib/twitter/user.rb
Expand Up @@ -7,7 +7,6 @@ module Twitter
class User < Twitter::Base
include Twitter::Authenticatable
include Twitter::Creatable
attr_reader :status
lazy_attr_reader :all_replies, :blocking, :can_dm, :connections,
:contributors_enabled, :default_profile, :default_profile_image,
:description, :favourites_count, :follow_request_sent, :followed_by,
Expand Down Expand Up @@ -50,15 +49,13 @@ class User < Twitter::Base
alias :verified? :verified
alias :want_retweets? :want_retweets

def initialize(attributes={})
attributes = attributes.dup
@status = Twitter::Status.new(attributes.delete('status')) unless attributes['status'].nil?
super(attributes)
end

def ==(other)
super || (other.class == self.class && other.id == self.id)
end

def status
@status ||= Twitter::Status.new(@attributes['status']) unless @attributes['status'].nil?
end

end
end
2 changes: 1 addition & 1 deletion spec/twitter/configuration_spec.rb
Expand Up @@ -10,7 +10,7 @@
end
it "should return nil when photo_sizes is not set" do
photo_sizes = Twitter::Configuration.new.photo_sizes
photo_sizes.should be_nil
photo_sizes.should be_empty
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/twitter/photo_spec.rb
Expand Up @@ -28,7 +28,7 @@
end
it "should return nil when sizes is not set" do
sizes = Twitter::Photo.new.sizes
sizes.should be_nil
sizes.should be_empty
end
end

Expand Down

0 comments on commit 1de9147

Please sign in to comment.