Class: FakeSNS::TopicCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fake_sns/topic_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ TopicCollection

Returns a new instance of TopicCollection.



6
7
8
9
# File 'lib/fake_sns/topic_collection.rb', line 6

def initialize(store)
  @store = store
  @store["topics"] ||= []
end

Instance Method Details

#collectionObject



11
12
13
# File 'lib/fake_sns/topic_collection.rb', line 11

def collection
  @store["topics"]
end

#create(attributes) ⇒ Object



32
33
34
# File 'lib/fake_sns/topic_collection.rb', line 32

def create(attributes)
  collection << attributes
end

#delete(arn) ⇒ Object



36
37
38
# File 'lib/fake_sns/topic_collection.rb', line 36

def delete(arn)
  collection.delete(fetch(arn))
end

#each(*args, &block) ⇒ Object



19
20
21
22
# File 'lib/fake_sns/topic_collection.rb', line 19

def each(*args, &block)
  p @store.to_yaml
  collection.map { |item| Topic.new(item) }.each(*args, &block)
end

#fetch(arn, &default) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/fake_sns/topic_collection.rb', line 24

def fetch(arn, &default)
  default ||= -> { raise InvalidParameterValue, "Unknown topic #{arn}" }
  found = collection.find do |topic|
    topic["arn"] == arn
  end
  found || default.call
end

#resetObject



15
16
17
# File 'lib/fake_sns/topic_collection.rb', line 15

def reset
  @store["topics"] = []
end