lib/vedeu/repositories/repository.rb in vedeu-0.4.13 vs lib/vedeu/repositories/repository.rb in vedeu-0.4.14
- old
+ new
@@ -1,7 +1,7 @@
-require 'vedeu/exceptions'
require 'vedeu/models/all'
+require 'vedeu/storage/store'
require 'vedeu/support/common'
module Vedeu
# Provides common methods for accessing the various repositories Vedeu uses.
@@ -12,65 +12,63 @@
# { 'models' => [Model] }
#
class Repository
include Vedeu::Common
- include Enumerable
+ include Vedeu::Store
# @!attribute [r] model
# @return [void]
attr_reader :model
# @!attribute [r] storage
# @return [void]
attr_reader :storage
+ def self.register_repository(model = nil, storage = {})
+ new(model, storage).tap do |klass|
+ Vedeu::Repositories.register(klass.repository)
+ end
+ end
+
# Returns a new instance of Vedeu::Repository.
#
# @param model [Class]
# @param storage [Class|Hash]
# @return [Vedeu::Repository]
def initialize(model = nil, storage = {})
- @model = model
- @storage = storage
+ @model = model
+ @storage = storage
end
- # Return the whole repository.
- #
- # @return [Array|Hash|Set]
- def all
- storage
+ def repository
+ self.class # .name
end
# Return the model for the interface currently in focus.
#
# @return [String|NilClass]
def current
find_or_create(Vedeu.focus) if Vedeu.focus
end
- # @return [Enumerator]
- def each(&block)
- storage.each(&block)
- end
-
- # Return a boolean indicating whether the storage is empty.
+ # Find the model by name.
#
- # @return [Boolean]
- def empty?
- storage.empty?
+ # @param name [String]
+ # @return [Hash<String => Object>|NilClass]
+ def find(name)
+ storage[name]
end
- # Find the model attributes by name.
+ # Find the model attributes by name, raises an exception if the model cannot
+ # be found.
#
# @param name [String]
# @raise [ModelNotFound] When the model cannot be found with this name.
# @return [Hash<String => Object>]
- def find(name)
- storage.fetch(name) do
- fail ModelNotFound, "Cannot find model by name: '#{name}'"
- end
+ def find!(name)
+ find(name) || fail(ModelNotFound, "Cannot find model by name: '#{name}'")
end
# Find a model by name, registers the model by name if not found.
#
# @param name [String]
@@ -78,11 +76,12 @@
def find_or_create(name)
if registered?(name)
find(name)
else
- Vedeu.log(type: :store, message: "Model (#{model}) not found, registering: '#{name}'")
+ Vedeu.log(type: :store,
+ message: "Model (#{model}) not found, registering: '#{name}'")
model.new(name).store
end
end
alias_method :by_name, :find_or_create
@@ -129,61 +128,44 @@
end
alias_method :destroy, :remove
alias_method :delete, :remove
alias_method :deregister, :remove
- # Reset the repository.
- #
- # @return [Array|Hash|Set]
- def reset
- @storage = in_memory
- end
-
# Stores the model instance by name in the repository of the model.
#
# @param model [void] A model instance.
# @raise [MissingRequired] When the name attribute is not defined.
# @return [void] The model instance which was stored.
def store(model)
fail MissingRequired, "Cannot store model '#{model.class}' without a " \
- "name attribute." unless defined_value?(model.name)
+ 'name attribute.' unless defined_value?(model.name)
- Vedeu.log(type: log_type(model), message: "#{model.class.name}: '#{model.name}'")
+ log_store(model)
storage[model.name] = model
end
alias_method :register, :store
# Access a model by name.
#
# @param name [String]
# @return [|NilClass]
def use(name)
- if registered?(name)
- find(name)
-
- else
- nil
-
- end
+ find(name) if registered?(name)
end
private
# @return [Hash]
def in_memory
{}
end
# @return [String]
- def log_type(model)
- if registered?(model.name)
- :update
+ def log_store(model)
+ type = registered?(model.name) ? :update : :create
- else
- :create
-
- end
+ Vedeu.log(type: type, message: "#{model.class.name}: '#{model.name}'")
end
end # Repository
end # Vedeu