class Fog::Storage::Rackspace::Mock::MockContainer
An in-memory container for use with Rackspace
storage mocks. Includes many ‘objects` mapped by (escaped) object name. Tracks container metadata.
Attributes
Public Class Methods
Create a new container. Generally, you should call {Fog::Rackspace::Storage#add_container} instead.
# File lib/fog/rackspace/storage.rb, line 137 def initialize(service) @service = service @objects, @meta = {}, {} end
Public Instance Methods
Add a new MockObject
to this container. An existing object with the same name will be overwritten.
@param name [String] The object’s name, unescaped. @param data [String, read] The contents of the object.
# File lib/fog/rackspace/storage.rb, line 193 def add_object(name, data) @objects[Fog::Rackspace.escape(name)] = MockObject.new(data, service) end
Total sizes of all objects added to this container.
@return [Integer] The number of bytes occupied by each contained
object.
# File lib/fog/rackspace/storage.rb, line 153 def bytes_used @objects.values.map { |o| o.bytes_used }.reduce(0) { |a, b| a + b } end
Determine if this container contains any MockObjects or not.
@return [Boolean]
# File lib/fog/rackspace/storage.rb, line 145 def empty? @objects.empty? end
Access a MockObject
within this container by (unescaped) name.
@return [MockObject, nil] Return the MockObject
at this name if
one exists; otherwise, `nil`.
# File lib/fog/rackspace/storage.rb, line 174 def mock_object(name) @objects[Fog::Rackspace.escape(name)] end
Access a MockObject
with a specific name, raising a ‘Fog::Storage::Rackspace::NotFound` exception if none are present.
@param name [String] (Unescaped) object name. @return [MockObject] The object within this container with the
specified name.
# File lib/fog/rackspace/storage.rb, line 184 def mock_object!(name) mock_object(name) or raise Fog::Storage::Rackspace::NotFound.new end
Remove a MockObject
from the container by name. No effect if the object is not present beforehand.
@param name [String] The (unescaped) object name to remove.
# File lib/fog/rackspace/storage.rb, line 201 def remove_object(name) @objects.delete Fog::Rackspace.escape(name) end
Render the HTTP headers that would be associated with this container.
@return [Hash<String, String>] Any metadata supplied to this
container, plus additional headers indicating the container's size.
# File lib/fog/rackspace/storage.rb, line 163 def to_headers @meta.merge({ 'X-Container-Object-Count' => @objects.size, 'X-Container-Bytes-Used' => bytes_used }) end