Class: Spy::Collection
- Inherits:
-
Object
- Object
- Spy::Collection
- Includes:
- Enumerable, SpyHelper
- Defined in:
- lib/spy/collection.rb,
lib/spy/collection/entry.rb
Defined Under Namespace
Modules: SpyHelper Classes: Entry
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize ⇒ Collection
constructor
A new instance of Collection.
- #insert(entry) ⇒ Object
- #remove(entry) ⇒ Object
-
#remove_all ⇒ Object
Removes each element from the collection and calls the block with each deleted element.
Methods included from SpyHelper
Constructor Details
#initialize ⇒ Collection
Returns a new instance of Collection.
7 8 9 |
# File 'lib/spy/collection.rb', line 7 def initialize @store = {} end |
Instance Method Details
#each ⇒ Object
27 28 29 |
# File 'lib/spy/collection.rb', line 27 def each @store.keys.each {|k| yield Entry.parse(k)} end |
#insert(entry) ⇒ Object
11 12 13 14 |
# File 'lib/spy/collection.rb', line 11 def insert(entry) raise Errors::AlreadySpiedError if include?(entry) @store[entry.key] = entry end |
#remove(entry) ⇒ Object
16 17 18 19 |
# File 'lib/spy/collection.rb', line 16 def remove(entry) raise Errors::MethodNotSpiedError unless include?(entry) @store.delete(entry.key).value end |
#remove_all ⇒ Object
Removes each element from the collection and calls the block with each deleted element
23 24 25 |
# File 'lib/spy/collection.rb', line 23 def remove_all map {|e| yield remove(e)} end |