Class: Wayback::Identity

Inherits:
Base
  • Object
show all
Defined in:
lib/wayback/identity.rb

Direct Known Subclasses

Archive, Availability, Page

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #attr_equal, attr_reader, #attrs, #attrs_equal, fetch_or_new, from_response, identity_map, #update

Constructor Details

#initialize(attrs = {}) ⇒ Wayback::Identity

Initializes a new object

Parameters:

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

Raises:

  • (ArgumentError)

    Error raised when supplied argument is missing an :id key.


33
34
35
36
# File 'lib/wayback/identity.rb', line 33

def initialize(attrs={})
  super
  raise ArgumentError, "argument must have an :id key" unless id
end

Class Method Details

.fetch(attrs) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wayback/identity.rb', line 7

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 Wayback::Error::IdentityMapKeyError, "key not found"
end

.store(object) ⇒ Wayback::Identity

Stores an object in the identity map.

Parameters:

  • object (Object)

Returns:


23
24
25
26
# File 'lib/wayback/identity.rb', line 23

def self.store(object)
  return object unless identity_map
  identity_map.store(object.id, object)
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

40
41
42
# File 'lib/wayback/identity.rb', line 40

def ==(other)
  super || attr_equal(:id, other) || attrs_equal(other)
end

#idInteger

Returns:

  • (Integer)

45
46
47
# File 'lib/wayback/identity.rb', line 45

def id
  @attrs[:id]
end