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

dao[R]
factory[R]

Public Class Methods

new(factory:, dao:) click to toggle source

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(entity) click to toggle source

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
all() click to toggle source

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
delete(identifier) click to toggle source

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_by_slug(slug) click to toggle source

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(identifier:, updated_attrs:) click to toggle source

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

validate_initializer_argument(arg_sym, value) click to toggle source

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