Class: Twitter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Twitter::Base

Initializes a new object

Parameters:

  • attrs (Hash) (defaults to: {})


81
82
83
# File 'lib/twitter/base.rb', line 81

def initialize(attrs={})
  @attrs = attrs
end

Instance Attribute Details

#attrsObject (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

Overloads:

  • .self.attr_reader(attr) ⇒ Object

    Parameters:

    • attr (Symbol)
  • .self.attr_reader(attrs) ⇒ Object

    Parameters:



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.

Parameters:

Returns:

Raises:



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.

Parameters:

  • attrs (Hash) (defaults to: {})

Returns:



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

Parameters:

  • response (Hash) (defaults to: {})

Returns:



58
59
60
# File 'lib/twitter/base.rb', line 58

def self.from_response(response={})
  fetch_or_new(response[:body])
end

.identity_mapObject

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.

Parameters:

  • object (Object)

Returns:



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

Parameters:

  • method (String, Symbol)

    Message to send to the object



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

Parameters:

Returns:



98
99
100
101
# File 'lib/twitter/base.rb', line 98

def update(attrs)
  @attrs.update(attrs)
  self
end