module EventSource::Entity::ClassMethods

Public Instance Methods

create(uid = EventSource::UIDGenerator.generate_id) { |entity| ... } click to toggle source
# File lib/event_source/entity.rb, line 4
def create(uid = EventSource::UIDGenerator.generate_id)
    entity = self.new
    entity.send(:uid=, uid)

    yield entity if block_given?
    entity
end
on_event(name, &block) click to toggle source
# File lib/event_source/entity.rb, line 27
def on_event(name, &block)
    self.send(:define_method, name) do |*args|
        block_args = [self] + args
        returnValue = block.call(block_args)
        return if @rebuilding

        entity_events << EventSource::Event.create(name, self, args)

        # if repo is nil, that's because this isn't being executed in the context of a
        # transaction and the result won't be saved
        repo = EventSource::EntityRepository.current
        repo.add(self) if repo

        returnValue
    end
end
rebuild(uid, events) click to toggle source
# File lib/event_source/entity.rb, line 12
def rebuild(uid, events)
    @rebuilding = true
    return self.create(uid) if events.length == 0

    entity = self.new
    entity.send(:uid=, uid)

    events.each do |e|
        entity.send(e.name, *(e.get_args))
    end
    @rebuilding = false

    entity
end