lib/vedeu/repositories/buffers.rb in vedeu-0.2.2 vs lib/vedeu/repositories/buffers.rb in vedeu-0.2.3
- old
+ new
@@ -4,20 +4,23 @@
# displayed.
#
# @api private
module Buffers
- include Vedeu::Common
+ include Common
+ include Repository
extend self
# Add an interface view into the back buffer. If the buffer is already
# registered, then we preserve its front buffer. Returns the name of the
# buffer added to storage.
#
# @param attributes [Hash]
# @return [String]
def add(attributes)
+ validate_attributes!(attributes)
+
if registered?(attributes[:name])
buffer = find(attributes[:name])
buffer[:back_buffer] = attributes
@@ -30,20 +33,10 @@
end
attributes[:name]
end
- # Find the buffer by name.
- #
- # @param name [String]
- # @return [Hash]
- def find(name)
- storage.fetch(name) do
- fail BufferNotFound, "Cannot find buffer with this name: #{name.to_s}."
- end
- end
-
# Returns the latest content for the named buffer. The latest content always
# goes on to the back buffer. The content which was last output is on the
# front buffer.
#
# When the back buffer has new content, we swap the back onto the front and
@@ -69,34 +62,10 @@
nil
end
end
- # Returns a collection of the names of all registered buffers.
- #
- # @return [Array]
- def registered
- storage.keys
- end
-
- # Returns a boolean indicating whether the named buffer is registered.
- #
- # @api private
- # @param name [String]
- # @return [Boolean]
- def registered?(name)
- storage.key?(name)
- end
-
- # Reset the buffers repository; removing all buffers. This does not delete
- # the interfaces themselves.
- #
- # @return [Hash]
- def reset
- @_storage = in_memory
- end
-
private
# Swap the named back buffer into the front buffer of the same name.
#
# @api private
@@ -133,39 +102,39 @@
#
# @api private
# @param name [String]
# @return [Hash|Nil]
def back_buffer(name)
- find(name).fetch(:back_buffer, nil)
+ find(name)[:back_buffer]
end
# Return the named front buffer.
#
# @api private
# @param name [String]
# @return [Hash|Nil]
def front_buffer(name)
- find(name).fetch(:front_buffer, nil)
+ find(name)[:front_buffer]
end
- # Access to the storage for this repository.
- #
# @api private
# @return [Hash]
- def storage
- @_storage ||= in_memory
- end
-
- # @api private
- # @return [Hash]
def in_memory
Hash.new do |hash, interface_name|
hash[interface_name] = {
front_buffer: nil,
back_buffer: nil,
}
end
end
- end
+ # @api private
+ # @param name [String]
+ # @raise [BufferNotFound] When the entity cannot be found with this name.
+ # @return [BufferNotFound]
+ def not_found(name)
+ fail BufferNotFound, "Cannot find buffer with this name: #{name.to_s}."
+ end
-end
+ end # Buffers
+
+end # Vedeu