Module: Lotus::Repository::ClassMethods

Defined in:
lib/lotus/repository.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#adapterObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0



217
218
219
# File 'lib/lotus/repository.rb', line 217

def adapter
  @adapter
end

#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



421
422
423
# File 'lib/lotus/repository.rb', line 421

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



520
521
522
# File 'lib/lotus/repository.rb', line 520

def clear
  @adapter.clear(collection)
end

#create(entity) ⇒ Object

Creates a record in the database for the given entity. It returns a copy of the entity with ‘id` assigned.

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

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

created_article = ArticleRepository.create(article)
created_article.id # => 24

created_article = ArticleRepository.create(existing_article) # => no-op
created_article # => nil

Parameters:

  • entity (#id, #id=)

    the entity to create

Returns:

  • (Object)

    a copy of the entity with ‘id` assigned

See Also:

  • Lotus::Repository#persist

Since:

  • 0.1.0



299
300
301
302
303
304
# File 'lib/lotus/repository.rb', line 299

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



397
398
399
400
401
402
403
404
405
# File 'lib/lotus/repository.rb', line 397

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



444
445
446
# File 'lib/lotus/repository.rb', line 444

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



473
474
475
# File 'lib/lotus/repository.rb', line 473

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



502
503
504
# File 'lib/lotus/repository.rb', line 502

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

persisted_article = ArticleRepository.persist(article) # creates a record
article.id # => nil
persisted_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)

    a copy of the entity with ‘id` assigned

See Also:

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

Since:

  • 0.1.0



261
262
263
264
# File 'lib/lotus/repository.rb', line 261

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



561
562
563
564
565
# File 'lib/lotus/repository.rb', line 561

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



348
349
350
351
352
353
354
355
# File 'lib/lotus/repository.rb', line 348

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