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



405
406
407
# File 'lib/lotus/repository.rb', line 405

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



504
505
506
# File 'lib/lotus/repository.rb', line 504

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



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

def create(entity)
  unless entity.id
    @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



381
382
383
384
385
386
387
388
389
# File 'lib/lotus/repository.rb', line 381

def delete(entity)
  if entity.id
    @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



428
429
430
# File 'lib/lotus/repository.rb', line 428

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



457
458
459
# File 'lib/lotus/repository.rb', line 457

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



486
487
488
# File 'lib/lotus/repository.rb', line 486

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
# File 'lib/lotus/repository.rb', line 254

def persist(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



545
546
547
548
549
# File 'lib/lotus/repository.rb', line 545

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



333
334
335
336
337
338
339
# File 'lib/lotus/repository.rb', line 333

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