Class: Twitter::Base
- Inherits:
-
Object
- Object
- Twitter::Base
- Defined in:
- lib/twitter/base.rb
Direct Known Subclasses
Action::Follow, Action::ListMemberAdded, Action::Mention, Action::Tweet, Configuration, Entity, Geo, Identity, Language, Metadata, OEmbed, Relationship, SearchResults, Settings, Size, Suggestion, Trend
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_hash)
readonly
Returns the value of attribute attrs.
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key.
-
.fetch(attrs) ⇒ Twitter::Base
Retrieves an object from the identity map.
-
.fetch_or_new(attrs = {}) ⇒ Twitter::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.
-
.from_response(response = {}) ⇒ Twitter::Base
Returns a new object based on the response hash.
-
.identity_map ⇒ Object
return [Twitter::IdentityMap].
-
.store(object) ⇒ Twitter::Base
Stores an object in the identity map.
Instance Method Summary collapse
-
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation.
-
#initialize(attrs = {}) ⇒ Twitter::Base
constructor
Initializes a new object.
-
#update(attrs) ⇒ Twitter::Base
Update the attributes of an object.
Constructor Details
#initialize(attrs = {}) ⇒ Twitter::Base
Initializes a new object
78 79 80 |
# File 'lib/twitter/base.rb', line 78 def initialize(attrs={}) @attrs = attrs end |
Instance Attribute Details
#attrs ⇒ Object (readonly) Also known as: to_hash
Returns the value of attribute attrs.
6 7 8 |
# File 'lib/twitter/base.rb', line 6 def attrs @attrs end |
Class Method Details
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
12 13 14 15 16 17 18 19 20 |
# File 'lib/twitter/base.rb', line 12 def self.attr_reader(*attrs) attrs.each do |attribute| class_eval do define_method attribute do @attrs[attribute.to_sym] end end end end |
.fetch(attrs) ⇒ Twitter::Base
Retrieves an object from the identity map.
33 34 35 36 37 38 39 40 |
# File 'lib/twitter/base.rb', line 33 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 |
.fetch_or_new(attrs = {}) ⇒ Twitter::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.
64 65 66 67 68 69 70 71 72 |
# File 'lib/twitter/base.rb', line 64 def self.fetch_or_new(attrs={}) return unless attrs return new(attrs) unless identity_map fetch(attrs) do object = new(attrs) store(object) end end |
.from_response(response = {}) ⇒ Twitter::Base
Returns a new object based on the response hash
55 56 57 |
# File 'lib/twitter/base.rb', line 55 def self.from_response(response={}) fetch_or_new(response[:body]) end |
.identity_map ⇒ Object
return [Twitter::IdentityMap]
23 24 25 26 27 |
# File 'lib/twitter/base.rb', line 23 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 |
.store(object) ⇒ Twitter::Base
Stores an object in the identity map.
46 47 48 49 |
# File 'lib/twitter/base.rb', line 46 def self.store(object) return object unless identity_map identity_map.store(Marshal.dump(object.attrs), object) end |
Instance Method Details
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation
85 86 87 88 89 |
# File 'lib/twitter/base.rb', line 85 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#update(attrs) ⇒ Twitter::Base
Update the attributes of an object
95 96 97 98 |
# File 'lib/twitter/base.rb', line 95 def update(attrs) @attrs.update(attrs) self end |