Class: Repository::Base

Inherits:
Object
  • Object
show all
Includes:
Internals
Defined in:
lib/repository/base.rb,
lib/repository/base/internals/record_deleter.rb,
lib/repository/base/internals/record_saver.rb,
lib/repository/base/internals/record_updater.rb,
lib/repository/base/internals/slug_finder.rb,
lib/repository/base/version.rb

Overview

Base class for Repository in Data Mapper pattern.

Defined Under Namespace

Modules: Internals

Constant Summary

VERSION =

Gem version, following [RubyGems.org](rubygems.org) and [SemVer](semver.org/) conventions.

'0.4.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory:, dao:) ⇒ Base

Initialise a new `Repository::Base` instance.

Parameters:

  • factory

    Has a .create method to create entities from DAO records.

  • dao

    Data Access Object implements persistence without business logic.



32
33
34
35
36
37
# 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

Instance Attribute Details

#daoObject (readonly)

Returns the value of attribute dao



26
27
28
# File 'lib/repository/base.rb', line 26

def dao
  @dao
end

#factoryObject (readonly)

Returns the value of attribute factory



26
27
28
# File 'lib/repository/base.rb', line 26

def factory
  @factory
end

Instance Method Details

#add(entity) ⇒ Repository::Support::StoreResult

Add a new record with attributes matching the specified entity to the associated DAO.

Parameters:

  • entity

    Entity specifying record to be persisted to new DAO record.

Returns:

  • (Repository::Support::StoreResult)

    An object containing information about the success or failure of an action.



44
45
46
47
# 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

#allArray

Return an array of entities matching all records currently in the associated DAO.

Returns:

  • (Array)

    Array of entities as supplied by the `factory`.

Since:

  • 0.0.2



53
54
55
# File 'lib/repository/base.rb', line 53

def all
  dao.all.map { |record| factory.create record }
end

#delete(identifier) ⇒ Repository::Support::StoreResult

Remove a record from the underlying DAO whose slug matches the passed-in identifier.

Parameters:

Returns:

  • (Repository::Support::StoreResult)

    An object containing information about the success or failure of an action.

Since:

  • 0.0.5



64
65
66
67
# File 'lib/repository/base.rb', line 64

def delete(identifier)
  RecordDeleter.new(identifier: identifier, dao: dao, factory: factory)
               .delete
end

#filtered_attributes_for(entity) ⇒ Object (private)

supporting #add



111
112
113
# File 'lib/repository/base.rb', line 111

def filtered_attributes_for(entity) # :nodoc:
  entity.attributes.to_hash.reject { |k, _v| k == :errors }
end

#find_by_slug(slug) ⇒ Repository::Support::StoreResult

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.

Parameters:

Returns:

  • (Repository::Support::StoreResult)

    An object containing information about the success or failure of an action.

Since:

  • 0.0.3



77
78
79
# 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:) ⇒ Object

Update a record in the DAO corresponding to the specified identifier, using the specified attribute-name/value pairs.

Examples:

result = user_repo.update @user.slug, params[:user_params]
@user = result.entity if result.success?

Parameters:

Since:

  • 0.0.4



90
91
92
93
# 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

#validate_initializer_argument(arg_sym, value) ⇒ boolean (private)

Verifies that parameter passed to #initialize is a Class.

Parameters:

  • arg_sym (Symbol)

    Which parameter is being validated, either `:dao` or `:factory`.

  • value

    Parameter value being validated. Must be a Class.

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


104
105
106
107
# 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