/
identity.rb
50 lines (41 loc) · 1.14 KB
/
identity.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'twitter/base'
require 'twitter/error/identity_map_key_error'
module Twitter
class Identity < Twitter::Base
def self.fetch(attrs)
return unless identity_map
id = attrs[:id]
if id && object = identity_map.fetch(id)
return object.update(attrs)
end
return yield if block_given?
raise Twitter::Error::IdentityMapKeyError, "key not found"
end
# Stores an object in the identity map.
#
# @param object [Object]
# @return [Twitter::Identity]
def self.store(object)
return object unless identity_map
identity_map.store(object.id, object)
end
# Initializes a new object
#
# @param attrs [Hash]
# @raise [ArgumentError] Error raised when supplied argument is missing an :id key.
# @return [Twitter::Identity]
def initialize(attrs={})
super
raise ArgumentError, "argument must have an :id key" unless id
end
# @param other [Twitter::Identity]
# @return [Boolean]
def ==(other)
super || attr_equal(:id, other) || attrs_equal(other)
end
# @return [Integer]
def id
@attrs[:id]
end
end
end