Module: Lotus::Repository::ClassMethods

Defined in:
lib/lotus/repository.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#adapter=(adapter) ⇒ Object

Assigns an adapter.

Lotus::Model is shipped with two adapters:

* SqlAdapter
* MemoryAdapter

Examples:

Memory adapter

require 'lotus/model'
require 'lotus/model/adapters/memory_adapter'

mapper = Lotus::Model::Mapper.new do
  # ...
end

adapter = Lotus::Model::Adapters::MemoryAdapter.new(mapper)

class UserRepository
  include Lotus::Repository
end

UserRepository.adapter = adapter

SQL adapter with a Sqlite database

require 'sqlite3'
require 'lotus/model'
require 'lotus/model/adapters/sql_adapter'

mapper = Lotus::Model::Mapper.new do
  # ...
end

adapter = Lotus::Model::Adapters::SqlAdapter.new(mapper, 'sqlite://path/to/database.db')

class UserRepository
  include Lotus::Repository
end

UserRepository.adapter = adapter

SQL adapter with a Postgres database

require 'pg'
require 'lotus/model'
require 'lotus/model/adapters/sql_adapter'

mapper = Lotus::Model::Mapper.new do
  # ...
end

adapter = Lotus::Model::Adapters::SqlAdapter.new(mapper, 'postgres://host:port/database')

class UserRepository
  include Lotus::Repository
end

UserRepository.adapter = adapter

Parameters:

  • adapter (Object)

    an object that implements ‘Lotus::Model::Adapters::Abstract` interface

See Also:

Since:

  • 0.1.0



211
212
213
# File 'lib/lotus/repository.rb', line 211

def adapter=(adapter)
  @adapter = adapter
end

#allArray<Object>

Returns all the persisted entities.

Examples:

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

ArticleRepository.all # => [ #<Article:0x007f9b19a60098> ]

Returns:

  • (Array<Object>)

    the result of the query

Since:

  • 0.1.0



408
409
410
# File 'lib/lotus/repository.rb', line 408

def all
  @adapter.all(collection)
end

#clearObject

Deletes all the records from the current collection.

If used with a SQL database it executes a ‘DELETE FROM <table>`.

Examples:

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

ArticleRepository.clear # deletes all the records

Since:

  • 0.1.0



507
508
509
# File 'lib/lotus/repository.rb', line 507

def clear
  @adapter.clear(collection)
end

#create(entity) ⇒ Object

Creates a record in the database for the given entity. It assigns the ‘id` attribute, in case of success.

If already persisted (‘id` present) it does nothing.

Examples:

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing Lotus::Model')
article.id # => nil

ArticleRepository.create(article) # creates a record
article.id # => 23

ArticleRepository.create(article) # no-op

Parameters:

  • entity (#id, #id=)

    the entity to create

Returns:

  • (Object)

    the entity

See Also:

  • Lotus::Repository#persist

Since:

  • 0.1.0



286
287
288
289
290
291
# File 'lib/lotus/repository.rb', line 286

def create(entity)
  unless _persisted?(entity)
    _touch(entity)
    @adapter.create(collection, entity)
  end
end

#delete(entity) ⇒ Object

Deletes a record in the database corresponding to the given entity.

If not already persisted (‘id` present) it raises an exception.

Examples:

With a persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

article = ArticleRepository.find(23)
article.id # => 23

ArticleRepository.delete(article) # deletes the record

With a non persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing Lotus::Model')
article.id # => nil

ArticleRepository.delete(article) # raises Lotus::Model::NonPersistedEntityError

Parameters:

  • entity (#id)

    the entity to delete

Returns:

  • (Object)

    the entity

Raises:

See Also:

Since:

  • 0.1.0



384
385
386
387
388
389
390
391
392
# File 'lib/lotus/repository.rb', line 384

def delete(entity)
  if _persisted?(entity)
    @adapter.delete(collection, entity)
  else
    raise Lotus::Model::NonPersistedEntityError
  end

  entity
end

#find(id) ⇒ Object, NilClass

Finds an entity by its identity.

If used with a SQL database, it corresponds to the primary key.

Examples:

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

ArticleRepository.find(23)   # => #<Article:0x007f9b19a60098>
ArticleRepository.find(9999) # => nil

Parameters:

  • id (Object)

    the identity of the entity

Returns:

  • (Object, NilClass)

    the result of the query, if present

Since:

  • 0.1.0



431
432
433
# File 'lib/lotus/repository.rb', line 431

def find(id)
  @adapter.find(collection, id)
end

#firstObject?

Returns the first entity in the database.

Examples:

With at least one persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

ArticleRepository.first # => #<Article:0x007f8c71d98a28>

With an empty collection

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

ArticleRepository.first # => nil

Returns:

  • (Object, nil)

    the result of the query

See Also:

  • Lotus::Repository#last

Since:

  • 0.1.0



460
461
462
# File 'lib/lotus/repository.rb', line 460

def first
  @adapter.first(collection)
end

#lastObject?

Returns the last entity in the database.

Examples:

With at least one persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

ArticleRepository.last # => #<Article:0x007f8c71d98a28>

With an empty collection

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

ArticleRepository.last # => nil

Returns:

  • (Object, nil)

    the result of the query

See Also:

  • Lotus::Repository#last

Since:

  • 0.1.0



489
490
491
# File 'lib/lotus/repository.rb', line 489

def last
  @adapter.last(collection)
end

#persist(entity) ⇒ Object

Creates or updates a record in the database for the given entity.

Examples:

With a non persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing Lotus::Model')
article.id # => nil

ArticleRepository.persist(article) # creates a record
article.id # => 23

With a persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

article = ArticleRepository.find(23)
article.id # => 23

article.title = 'Launching Lotus::Model'
ArticleRepository.persist(article) # updates the record

article = ArticleRepository.find(23)
article.title # => "Launching Lotus::Model"

Parameters:

  • entity (#id, #id=)

    the entity to persist

Returns:

  • (Object)

    the entity

See Also:

  • Lotus::Repository#create
  • Lotus::Repository#update

Since:

  • 0.1.0



254
255
256
257
# File 'lib/lotus/repository.rb', line 254

def persist(entity)
  _touch(entity)
  @adapter.persist(collection, entity)
end

#transaction(options = {}) ⇒ Object

Wraps the given block in a transaction.

For performance reasons the block isn’t in the signature of the method, but it’s yielded at the lower level.

Please note that it’s only supported by some databases. For this reason, the accepted options may be different from adapter to adapter.

For advanced scenarios, please check the documentation of each adapter.

Examples:

Basic usage with SQL adapter

require 'lotus/model'

class Article
  include Lotus::Entity
  attributes :title, :body
end

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing transactions',
  body: 'lorem ipsum')

ArticleRepository.transaction do
  ArticleRepository.dangerous_operation!(article) # => RuntimeError
  # !!! ROLLBACK !!!
end

Parameters:

  • options (Hash) (defaults to: {})

    options for transaction

See Also:

Since:

  • 0.2.3



548
549
550
551
552
# File 'lib/lotus/repository.rb', line 548

def transaction(options = {})
  @adapter.transaction(options) do
    yield
  end
end

#update(entity) ⇒ Object

Updates a record in the database corresponding to the given entity.

If not already persisted (‘id` present) it raises an exception.

Examples:

With a persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

article = ArticleRepository.find(23)
article.id # => 23
article.title = 'Launching Lotus::Model'

ArticleRepository.update(article) # updates the record

With a non persisted entity

require 'lotus/model'

class ArticleRepository
  include Lotus::Repository
end

article = Article.new(title: 'Introducing Lotus::Model')
article.id # => nil

ArticleRepository.update(article) # raises Lotus::Model::NonPersistedEntityError

Parameters:

  • entity (#id)

    the entity to update

Returns:

  • (Object)

    the entity

Raises:

See Also:

Since:

  • 0.1.0



335
336
337
338
339
340
341
342
# File 'lib/lotus/repository.rb', line 335

def update(entity)
  if _persisted?(entity)
    _touch(entity)
    @adapter.update(collection, entity)
  else
    raise Lotus::Model::NonPersistedEntityError
  end
end