Module: ORMivore::Repo

Included in:
App::AccountRepo, App::AddressRepo
Defined in:
lib/ormivore/repo.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
# File 'lib/ormivore/repo.rb', line 10

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#find_by_id(id, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ormivore/repo.rb', line 19

def find_by_id(id, options = {})
  quiet = options.fetch(:quiet, false)

  attrs_to_entity(port.find(
      { id: id },
      [:id].concat(entity_class.attributes_list),
      {}
    ).first
 ).tap { |record|
   raise RecordNotFound, "#{entity_class.name} with id #{id} was not found" if record.nil? && !quiet
 }
end

#initialize(port, entity_class = nil) ⇒ Object



14
15
16
17
# File 'lib/ormivore/repo.rb', line 14

def initialize(port, entity_class = nil)
  @port = port
  @entity_class = entity_class || self.class.default_entity_class
end

#persist(entity) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ormivore/repo.rb', line 32

def persist(entity)
  if entity.id
    count = port.update(entity.changes, { :id => entity.id })
    raise ORMivore::StorageError, 'No records updated' if count.zero?
    raise ORMivore::StorageError, 'WTF' if count > 1

    entity_class.construct(entity.attributes, entity.id)
  else
    attrs_to_entity(port.create(entity.changes))
  end
end