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



410
411
412
# File 'lib/lotus/repository.rb', line 410

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



509
510
511
# File 'lib/lotus/repository.rb', line 509

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

def create(entity)
  unless entity.id
    @adapter.create(collection, entity)
  end

  entity
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



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

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



433
434
435
# File 'lib/lotus/repository.rb', line 433

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



462
463
464
# File 'lib/lotus/repository.rb', line 462

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



491
492
493
# File 'lib/lotus/repository.rb', line 491

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)
  @adapter.persist(collection, entity)
  entity
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



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

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

  entity
end