Class: Fog::Storage::Rackspace::Mock::MockContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/storage.rb

Overview

An in-memory container for use with Rackspace storage mocks. Includes many ‘objects` mapped by (escaped) object name. Tracks container metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ MockContainer

Create a new container. Generally, you should call Rackspace::Storage#add_container instead.



137
138
139
140
# File 'lib/fog/rackspace/storage.rb', line 137

def initialize(service)
  @service = service
  @objects, @meta = {}, {}
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



133
134
135
# File 'lib/fog/rackspace/storage.rb', line 133

def meta
  @meta
end

#objectsObject (readonly)

Returns the value of attribute objects.



133
134
135
# File 'lib/fog/rackspace/storage.rb', line 133

def objects
  @objects
end

#serviceObject (readonly)

Returns the value of attribute service.



133
134
135
# File 'lib/fog/rackspace/storage.rb', line 133

def service
  @service
end

Instance Method Details

#add_object(name, data) ⇒ Object

Add a new MockObject to this container. An existing object with the same name will be overwritten.

Parameters:

  • name (String)

    The object’s name, unescaped.

  • data (String, #read)

    The contents of the object.



193
194
195
# File 'lib/fog/rackspace/storage.rb', line 193

def add_object(name, data)
  @objects[Fog::Rackspace.escape(name)] = MockObject.new(data, service)
end

#bytes_usedInteger

Total sizes of all objects added to this container.

Returns:

  • (Integer)

    The number of bytes occupied by each contained object.



153
154
155
# 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

#empty?Boolean

Determine if this container contains any MockObjects or not.

Returns:

  • (Boolean)


145
146
147
# File 'lib/fog/rackspace/storage.rb', line 145

def empty?
  @objects.empty?
end

#mock_object(name) ⇒ MockObject?

Access a MockObject within this container by (unescaped) name.

Returns:

  • (MockObject, nil)

    Return the MockObject at this name if one exists; otherwise, ‘nil`.



174
175
176
# File 'lib/fog/rackspace/storage.rb', line 174

def mock_object(name)
  @objects[Fog::Rackspace.escape(name)]
end

#mock_object!(name) ⇒ MockObject

Access a MockObject with a specific name, raising a ‘Fog::Storage::Rackspace::NotFound` exception if none are present.

Parameters:

  • name (String)

    (Unescaped) object name.

Returns:

  • (MockObject)

    The object within this container with the specified name.



184
185
186
# File 'lib/fog/rackspace/storage.rb', line 184

def mock_object!(name)
  mock_object(name) or raise Fog::Storage::Rackspace::NotFound.new
end

#remove_object(name) ⇒ Object

Remove a MockObject from the container by name. No effect if the object is not present beforehand.

Parameters:

  • name (String)

    The (unescaped) object name to remove.



201
202
203
# File 'lib/fog/rackspace/storage.rb', line 201

def remove_object(name)
  @objects.delete Fog::Rackspace.escape(name)
end

#to_headersHash<String, String>

Render the HTTP headers that would be associated with this container.

Returns:

  • (Hash<String, String>)

    Any metadata supplied to this container, plus additional headers indicating the container’s size.



163
164
165
166
167
168
# 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