Class: EventSource::EntityRepository

Inherits:
Object
  • Object
show all
Extended by:
MemoizeInstance
Defined in:
lib/event_source/entity_repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MemoizeInstance

create, current, default_args

Constructor Details

#initialize(event_repo = nil) ⇒ EntityRepository

Returns a new instance of EntityRepository.



22
23
24
25
# File 'lib/event_source/entity_repository.rb', line 22

def initialize(event_repo = nil)
    @entities = Set.new
    @event_repo = event_repo
end

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



12
13
14
# File 'lib/event_source/entity_repository.rb', line 12

def entities
  @entities
end

Class Method Details

.transactionObject



15
16
17
18
19
# File 'lib/event_source/entity_repository.rb', line 15

def transaction
    self.current.clear
    yield
    self.current.commit
end

Instance Method Details

#add(entity) ⇒ Object



31
32
33
# File 'lib/event_source/entity_repository.rb', line 31

def add(entity)
    @entities << entity
end

#clearObject



27
28
29
# File 'lib/event_source/entity_repository.rb', line 27

def clear
    @entities.clear
end

#commitObject



35
36
37
38
39
# File 'lib/event_source/entity_repository.rb', line 35

def commit
    # TODO: gather all events of all entities, maintain order and save in batch
    @entities.each {|e| e.save}
    clear
end

#find(type, uid) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/event_source/entity_repository.rb', line 41

def find(type, uid)
    entity = @entities.select {|e| e.uid == uid}[0]
    return entity if entity

    events = @event_repo.get_events(type, uid)

    entity_class = type.to_s.camelize.constantize
    if events.count > 0
        entity = entity_class.rebuild(uid, events)
    else
        entity = entity_class.create(uid)
    end

    entity
end