class Repository::Base
Base
class for Repository
in Data Mapper pattern.
Base
class for Repository
in Data Mapper pattern.
Base
class for Repository
in Data Mapper pattern.
Base
class for Repository
in Data Mapper pattern.
Base
class for Repository
in Data Mapper pattern.
Base
class for Repository
in Data Mapper pattern.
Constants
- VERSION
Attributes
Public Class Methods
Initialise a new `Repository::Base` instance. @param factory Has a .create method to create entities from DAO records. @param dao Data Access Object implements persistence without business
logic.
# File lib/repository/base.rb, line 32 def initialize(factory:, dao:) validate_initializer_argument(:dao, dao) validate_initializer_argument(:factory, factory) @factory = factory @dao = dao end
Public Instance Methods
Add a new record with attributes matching the specified entity to the associated DAO. @param entity Entity specifying record to be persisted to new DAO record. @return [Repository::Support::StoreResult] An object containing
information about the success or failure of an action.
# File lib/repository/base.rb, line 44 def add(entity) record = dao.new filtered_attributes_for(entity) RecordSaver.new(record: record, factory: factory).result end
Return an array of entities matching all records currently in the associated DAO. @return [Array] Array of entities as supplied by the `factory`. @since 0.0.2
# File lib/repository/base.rb, line 53 def all dao.all.map { |record| factory.create record } end
Remove a record from the underlying DAO whose slug matches the passed-in identifier. @param identifier [String] [Slug](en.wikipedia.org/wiki/Semantic_URL#Slug)
for record to be deleted.
@return [Repository::Support::StoreResult] An object containing
information about the success or failure of an action.
@since 0.0.5
# File lib/repository/base.rb, line 64 def delete(identifier) RecordDeleter.new(identifier: identifier, dao: dao, factory: factory) .delete end
Find a record in the DAO and, on success, return a corresponding entity using the specified [slug](en.wikipedia.org/wiki/Semantic_URL#Slug), not a numeric record ID, as a search identifier. @param slug [String] [Slug](en.wikipedia.org/wiki/Semantic_URL#Slug)
for record to be deleted.
@return [Repository::Support::StoreResult] An object containing
information about the success or failure of an action.
@since 0.0.3
# File lib/repository/base.rb, line 77 def find_by_slug(slug) SlugFinder.new(slug: slug, dao: dao, factory: factory).find end
Update a record in the DAO corresponding to the specified identifier, using the specified attribute-name/value pairs. @param identifier [String] [Slug](en.wikipedia.org/wiki/Semantic_URL#Slug)
for record to be deleted.
@param updated_attrs [Hash] Attributes to be updated. @since 0.0.4 @example
result = user_repo.update @user.slug, params[:user_params] @user = result.entity if result.success?
# File lib/repository/base.rb, line 90 def update(identifier:, updated_attrs:) RecordUpdater.new(identifier: identifier, updated_attrs: updated_attrs, dao: dao, factory: factory).update end
Private Instance Methods
Verifies that parameter passed to initialize is a Class. @param arg_sym [Symbol] Which parameter is being validated, either `:dao`
or `:factory`.
@param value Parameter value being validated. Must be a Class. @return [boolean]
# File lib/repository/base.rb, line 104 def validate_initializer_argument(arg_sym, value) message = "the :#{arg_sym} argument must be a Class" raise ArgumentError, message unless value.respond_to? :new end