Method: Spider::Model::UnitOfWork#run

Defined in:
lib/spiderfw/model/unit_of_work.rb

#runObject Also known as: commit

(&proc)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spiderfw/model/unit_of_work.rb', line 30

def run #(&proc)
    #proc.call
    prev_uow = Spider.current[:unit_of_work]
    Spider.current[:unit_of_work] = self
    @tasks = {}
    @processed_tasks = {}
    while objs = new_objects
        objs.each do |obj|
            @actions[obj.object_id].each do |action, params|
                if action == :save
                    next unless obj.mapper && obj.mapper.class.write?
                    next unless obj.modified?
                    obj.save_mode do
                        obj.before_save
                    end
                elsif action == :delete
                    obj.before_delete
                end
            end
        end
    end
    @running = true
    @objects.each do |obj_id, obj|
        @actions[obj_id].each do |action, params|
            task = Spider::Model::MapperTask.new(obj, action, params)
            @tasks[task] = task
        end
    end
    @tasks.clone.each do |k, task|
        find_dependencies(task)
    end
    tasks = tsort()
    
    if Spider.logger.debug?
        Spider.logger.debug("Tasks:")
        tasks.each do |task| 
            debug_str = "-- #{task.action} on #{task.object.class} #{task.object.primary_keys}"
            debug_str += " #{task.params.inspect}" unless task.params.blank?
            Spider.logger.debug debug_str
        end
    end
                
    tasks.each do |task|
        obj = task.object
        if task.action == :save
            next unless obj.mapper && obj.mapper.class.write?
            next if task.params[:force] != :insert && !obj.modified? && obj.primary_keys_set?
        end
        #Spider::Logger.debug("Executing task #{task.inspect}")
        task.execute()
    end
    @objects = {}
    @new_objects = []
    @running = false
    Spider.current[:unit_of_work] = prev_uow
end