Skip to content

Commit

Permalink
Implement lazy attribute setting
Browse files Browse the repository at this point in the history
Hat-tip: @dkubb and @phiggins
  • Loading branch information
sferik committed Oct 18, 2011
1 parent ca8495d commit 6fb7b3f
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 116 deletions.
18 changes: 11 additions & 7 deletions lib/twitter/base.rb
@@ -1,18 +1,22 @@
module Twitter
class Base

def initialize(hash={})
hash.each do |key, value|
instance_variable_set(:"@#{key}", value)
def self.lazy_attr_reader(*attributes)
attributes.each do |attribute|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{attribute}
@#{attribute} ||= @attributes[#{attribute.to_s.inspect}]
end
RUBY
end
end

def [](method)
self.__send__(method.to_sym)
def initialize(attributes = {})
@attributes = attributes.dup
end

def to_hash
Hash[instance_variables.map{|ivar| [ivar[1..-1].to_sym, instance_variable_get(ivar)]}]
def [](method)
self.__send__(method.to_sym)
end

end
Expand Down
15 changes: 8 additions & 7 deletions lib/twitter/configuration.rb
Expand Up @@ -4,15 +4,16 @@

module Twitter
class Configuration < Twitter::Base
attr_reader :characters_reserved_per_media, :max_media_per_upload,
:non_username_paths, :photo_size_limit, :photo_sizes, :short_url_length,
:short_url_length_https
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(configuration={})
@photo_sizes = configuration.delete('photo_sizes').each_with_object({}) do |(key, value), object|
def initialize(attributes={})
attributes = attributes.dup
@photo_sizes = attributes.delete('photo_sizes').each_with_object({}) do |(key, value), object|
object[key] = Twitter::Size.new(value)
end unless configuration['photo_sizes'].nil?
super(configuration)
end unless attributes['photo_sizes'].nil?
super(attributes)
end

end
Expand Down
7 changes: 4 additions & 3 deletions lib/twitter/creatable.rb
Expand Up @@ -4,9 +4,10 @@ module Twitter
module Creatable
attr_reader :created_at

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

end
Expand Down
16 changes: 9 additions & 7 deletions lib/twitter/cursor.rb
Expand Up @@ -3,31 +3,33 @@

module Twitter
class Cursor < Twitter::Base
attr_reader :collection, :next_cursor, :previous_cursor
attr_reader :collection
lazy_attr_reader :next_cursor, :previous_cursor
alias :next :next_cursor
alias :previous :previous_cursor

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

def first?
@previous_cursor.zero?
previous_cursor.zero?
end
alias :first :first?

def last?
@next_cursor.zero?
next_cursor.zero?
end
alias :last :last?

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

def initialize(direct_message={})
@recipient = Twitter::User.new(direct_message.delete('recipient')) unless direct_message['recipient'].nil?
@sender = Twitter::User.new(direct_message.delete('sender')) unless direct_message['sender'].nil?
super(direct_message)
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.instance_variable_get('@id'.to_sym) == @id)
super || (other.class == self.class && other.id == self.id)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/language.rb
Expand Up @@ -2,6 +2,6 @@

module Twitter
class Language < Twitter::Base
attr_reader :code, :name, :status
lazy_attr_reader :code, :name, :status
end
end
14 changes: 8 additions & 6 deletions lib/twitter/list.rb
Expand Up @@ -3,17 +3,19 @@

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

def initialize(list={})
@user = Twitter::User.new(list.delete('user')) unless list['user'].nil?
super(list)
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.instance_variable_get('@id'.to_sym) == @id)
super || (other.class == self.class && other.id == self.id)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/metadata.rb
Expand Up @@ -2,6 +2,6 @@

module Twitter
class Metadata < Twitter::Base
attr_reader :result_type
lazy_attr_reader :result_type
end
end
16 changes: 9 additions & 7 deletions lib/twitter/photo.rb
Expand Up @@ -4,18 +4,20 @@

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

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

def ==(other)
super || (other.class == self.class && other.instance_variable_get('@id'.to_sym) == @id)
super || (other.class == self.class && other.id == self.id)
end

end
Expand Down
19 changes: 11 additions & 8 deletions lib/twitter/place.rb
Expand Up @@ -3,18 +3,21 @@

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

def initialize(place={})
@bounding_box = Twitter::GeoFactory.new(place.delete('bounding_box')) unless place['bounding_box'].nil?
@country_code = place.delete('country_code') || place.delete('countryCode')
@place_type = place.delete('place_type') || place['placeType'] && place['placeType'].delete('name')
super(place)
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.instance_variable_get('@id'.to_sym) == @id)
super || (other.class == self.class && other.id == self.id)
end

end
Expand Down
8 changes: 4 additions & 4 deletions lib/twitter/point.rb
Expand Up @@ -2,19 +2,19 @@

module Twitter
class Point < Twitter::Base
attr_reader :coordinates
lazy_attr_reader :coordinates

def ==(other)
super || (other.class == self.class && other.instance_variable_get('@coordinates'.to_sym) == @coordinates)
super || (other.class == self.class && other.coordinates == self.coordinates)
end

def latitude
@coordinates[0]
coordinates[0]
end
alias :lat :latitude

def longitude
@coordinates[1]
coordinates[1]
end
alias :long :longitude
alias :lng :longitude
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/polygon.rb
Expand Up @@ -2,10 +2,10 @@

module Twitter
class Polygon < Twitter::Base
attr_reader :coordinates
lazy_attr_reader :coordinates

def ==(other)
super || (other.class == self.class && other.instance_variable_get('@coordinates'.to_sym) == @coordinates)
super || (other.class == self.class && other.coordinates == self.coordinates)
end

end
Expand Down
10 changes: 6 additions & 4 deletions lib/twitter/rate_limit_status.rb
Expand Up @@ -2,11 +2,13 @@

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

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

end
Expand Down
9 changes: 5 additions & 4 deletions lib/twitter/relationship.rb
Expand Up @@ -5,10 +5,11 @@ module Twitter
class Relationship < Twitter::Base
attr_reader :source, :target

def initialize(relationship={})
@source = Twitter::User.new(relationship.delete('source')) unless relationship['source'].nil?
@target = Twitter::User.new(relationship.delete('target')) unless relationship['target'].nil?
super(relationship)
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)
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/saved_search.rb
Expand Up @@ -4,10 +4,10 @@
module Twitter
class SavedSearch < Twitter::Base
include Twitter::Creatable
attr_reader :id, :name, :position, :query
lazy_attr_reader :id, :name, :position, :query

def ==(other)
super || (other.class == self.class && other.instance_variable_get('@id'.to_sym) == @id)
super || (other.class == self.class && other.id == self.id)
end

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

module Twitter
class Settings < Twitter::Base
attr_reader :always_use_https, :discoverable_by_email, :geo_enabled,
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, :trend_location
:time_zone
alias :protected? :protected

def initialize(settings={})
@trend_location = Twitter::Place.new(settings.delete('trend_location').first) unless settings['trend_location'].nil? || settings['trend_location'].empty?
super(settings)
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)
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/size.rb
Expand Up @@ -2,12 +2,12 @@

module Twitter
class Size < Twitter::Base
attr_reader :h, :resize, :w
lazy_attr_reader :h, :resize, :w
alias :height :h
alias :width :w

def ==(other)
super || (other.class == self.class && other.instance_variable_get('@h'.to_sym) == @h && other.instance_variable_get('@w'.to_sym) == @w)
super || (other.class == self.class && other.h == self.h && other.w == self.w)
end

end
Expand Down

0 comments on commit 6fb7b3f

Please sign in to comment.