Class: Shark::RSpec::FakeSubscriptionService::ObjectCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shark/rspec/fake_subscription_service/object_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectCache

Returns a new instance of ObjectCache.



10
11
12
# File 'lib/shark/rspec/fake_subscription_service/object_cache.rb', line 10

def initialize
  @objects = []
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects.



8
9
10
# File 'lib/shark/rspec/fake_subscription_service/object_cache.rb', line 8

def objects
  @objects
end

Class Method Details

.clearObject



14
15
16
# File 'lib/shark/rspec/fake_subscription_service/object_cache.rb', line 14

def self.clear
  instance.objects = []
end

Instance Method Details

#add(payload_data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/shark/rspec/fake_subscription_service/object_cache.rb', line 18

def add(payload_data)
  id = payload_data.delete('id')

  if id && !objects.find { |subscription| subscription['id'] == id }
    object = {
      id: id,
      attributes: payload_data
    }

    objects << object
  else
    objects << {
      id: SecureRandom.uuid,
      attributes: payload_data
    }
  end
  objects.last
end

#add_multiple(payload_data) ⇒ Object



37
38
39
40
41
# File 'lib/shark/rspec/fake_subscription_service/object_cache.rb', line 37

def add_multiple(payload_data)
  added = []
  payload_data.each { |subscription| added << add(subscription) }
  added.compact
end

#remove(id) ⇒ Object



43
44
45
# File 'lib/shark/rspec/fake_subscription_service/object_cache.rb', line 43

def remove(id)
  objects.delete_if { |subscription| subscription[:id] == id }
end

#remove_multiple(payload_data) ⇒ Object



47
48
49
50
# File 'lib/shark/rspec/fake_subscription_service/object_cache.rb', line 47

def remove_multiple(payload_data)
  payload_data.each { |subscription| remove(subscription['id']) }
  objects
end