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
81 82 83 |
# File 'lib/twitter/base.rb', line 81 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
.self.attr_reader(attr) ⇒ Object .self.attr_reader(attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
15 16 17 18 19 20 21 22 23 |
# File 'lib/twitter/base.rb', line 15 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.
36 37 38 39 40 41 42 43 |
# File 'lib/twitter/base.rb', line 36 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.
67 68 69 70 71 72 73 74 75 |
# File 'lib/twitter/base.rb', line 67 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
58 59 60 |
# File 'lib/twitter/base.rb', line 58 def self.from_response(response={}) fetch_or_new(response[:body]) end |
.identity_map ⇒ Object
return [Twitter::IdentityMap]
26 27 28 29 30 |
# File 'lib/twitter/base.rb', line 26 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.
49 50 51 52 |
# File 'lib/twitter/base.rb', line 49 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
88 89 90 91 92 |
# File 'lib/twitter/base.rb', line 88 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#update(attrs) ⇒ Twitter::Base
Update the attributes of an object
98 99 100 101 |
# File 'lib/twitter/base.rb', line 98 def update(attrs) @attrs.update(attrs) self end |