Module: LittleMapper::MapperInstanceMethods

Defined in:
lib/little_mapper/mapper_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#<<(entity) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/little_mapper/mapper_instance_methods.rb', line 24

def <<(entity)
  pe = to_persistent(entity)
  if pe.save
    entity.id = pe.id unless entity.id # set this only if configured
    LittleMapper::Result::RepoSuccess.new(pe)
  else
    LittleMapper::Result::RepoFailure.new(pe)
  end
end

#find_allObject



44
45
46
# File 'lib/little_mapper/mapper_instance_methods.rb', line 44

def find_all
  self.class.persistent_entity.all.collect { |e| to_entity(e) }
end

#find_by_id(id) ⇒ Object



38
39
40
41
42
# File 'lib/little_mapper/mapper_instance_methods.rb', line 38

def find_by_id(id)
  pe = find_persistent_by_id(id)
  return nil unless pe
  to_entity(pe)
end

#find_persistent_by_id(id) ⇒ Object



34
35
36
# File 'lib/little_mapper/mapper_instance_methods.rb', line 34

def find_persistent_by_id(id)
  self.class.persistent_entity.find_by_id(id)
end

#lastObject



48
49
50
# File 'lib/little_mapper/mapper_instance_methods.rb', line 48

def last
  to_entity(self.class.persistent_entity.last)
end

#persisted?(entity) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/little_mapper/mapper_instance_methods.rb', line 20

def persisted?(entity)
  !entity.id.nil? # should maybe check for empty strings too?
end

#to_entity(persistent) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/little_mapper/mapper_instance_methods.rb', line 11

def to_entity(persistent)
  e = self.class.entity.new
  e.id = persistent.id # probably set this only if configured & see duplication below
  self.class.mappings.each do |m|
    m.from(persistent).to_entity(e)
  end
  e
end

#to_persistent(entity) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/little_mapper/mapper_instance_methods.rb', line 3

def to_persistent(entity)
  pe = persisted?(entity) ? find_persistent_by_id(entity.id) : self.class.persistent_entity.new
  self.class.mappings.each do |m|
    m.from(entity).to_persistent(pe)
  end
  pe
end