Module: Repobahn::Repository::ClassMethods

Defined in:
lib/repobahn/repository.rb

Instance Method Summary collapse

Instance Method Details

#db_modelObject



21
22
23
# File 'lib/repobahn/repository.rb', line 21

def db_model
  @db_model || default_db_model
end

#db_model=(klass) ⇒ Object



25
26
27
# File 'lib/repobahn/repository.rb', line 25

def db_model=(klass)
  @db_model = klass
end

#entity_modelObject



13
14
15
# File 'lib/repobahn/repository.rb', line 13

def entity_model
  @entity_model || default_entity_model
end

#entity_model=(klass) ⇒ Object



17
18
19
# File 'lib/repobahn/repository.rb', line 17

def entity_model=(klass)
  @entity_model = klass
end

#find(id) ⇒ Object



29
30
31
32
# File 'lib/repobahn/repository.rb', line 29

def find(id)
  record = db_model.find id
  to_entity_from_db record if record
end

#to_entity_from_db(records) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/repobahn/repository.rb', line 34

def to_entity_from_db(records)
  entities = Array(records).map do |record|
    entity = entity_model.new(record.attributes)
    run_hook :after_conversion, entity, record
    entity
  end
  entities.length > 1 ? entities : entities.first
end