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: {})


78
79
80
# File 'lib/twitter/base.rb', line 78

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

.attr_reader(*attrs) ⇒ Object

Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key

Parameters:

  • attrs (Array, Set, Symbol)


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.

Parameters:

Returns:

Raises:



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.

Parameters:

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

Returns:



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

Parameters:

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

Returns:



55
56
57
# File 'lib/twitter/base.rb', line 55

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

.identity_mapObject

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.

Parameters:

  • object (Object)

Returns:



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

Parameters:

  • method (String, Symbol)

    Message to send to the object



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

Parameters:

Returns:



95
96
97
98
# File 'lib/twitter/base.rb', line 95

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