Module: Spider::Model::List::MapperMethods

Defined in:
lib/spiderfw/model/mixins/list.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) before_delete(objects)



50
51
52
53
54
55
56
57
58
59
# File 'lib/spiderfw/model/mixins/list.rb', line 50

def before_delete(objects)
    @model.lists.each do |list_el|
        objects.each do |obj|
            cond = get_list_condition(list_el, obj)
            val = obj.get(list_el)
            move_down_list(list_el.name, val, nil, cond) if val
        end
    end
    super(objects)
end

- (Object) before_save(obj, mode)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spiderfw/model/mixins/list.rb', line 16

def before_save(obj, mode)
    return super if obj.embedder && storage.supports?(:embedding)
    obj.class.lists.each do |l|
        next if (!check_list_condition(l, obj))
        cond = get_list_condition(l, obj)
        cur = obj.get(l.name)
        obj.set(l.name, max(l.name, cond) + 1) unless cur
    end
    if (obj.list_mixin_modified_elements)
        obj.list_mixin_modified_elements.each do |name, old|
            if (!check_list_condition(name, obj))
                obj.set(name, nil)
                next
            end
            cond = get_list_condition(name, obj)
            new_val = nil
            obj.save_mode do
                new_val = obj.get(name)
            end
            new_val ||= max(name, cond) + 1
            if (!old)
                move_up_list(name, new_val, nil, cond)
            else
                if (new_val < old)
                    move_up_list(name, new_val, old-1, cond)
                else
                    move_down_list(name, old+1, new_val, cond)
                end
            end
        end
    end
    super(obj, mode)
end

- (Object) check_list_condition(element, obj)



87
88
89
90
91
92
93
# File 'lib/spiderfw/model/mixins/list.rb', line 87

def check_list_condition(element, obj)
    element = @model.elements[element] unless element.is_a?(Element)
    l_check = element.attributes[:check_list_condition]
    return true unless l_check
    return l_check.call(obj)

end

- (Object) get_list_condition(element, obj)



77
78
79
80
81
82
83
84
85
# File 'lib/spiderfw/model/mixins/list.rb', line 77

def get_list_condition(element, obj)
    element = @model.elements[element] unless element.is_a?(Element)
    l_cond = element.attributes[:list_condition]
    return nil unless l_cond
    cond = l_cond.call(obj)
    cond = Condition.new(cond) unless cond.is_a?(Condition)
    preprocess_condition(cond)
    return cond
end

- (Object) move_down_list(element_name, from, to = nil, condition = nil)



69
70
71
72
73
74
75
# File 'lib/spiderfw/model/mixins/list.rb', line 69

def move_down_list(element_name, from, to=nil, condition=nil)
    expr = ":#{element_name} - 1"
    cond = condition || Condition.and
    cond.set(element_name, '>=', from)
    cond.set(element_name, '<=', to) if to
    bulk_update({element_name => Spider::QueryFuncs::Expression.new(expr)}, cond)
end

- (Object) move_up_list(element_name, from, to = nil, condition = nil)



61
62
63
64
65
66
67
# File 'lib/spiderfw/model/mixins/list.rb', line 61

def move_up_list(element_name, from, to=nil, condition=nil)
    expr = ":#{element_name} + 1"
    cond = condition || Condition.and
    cond.set(element_name, '>=', from)
    cond.set(element_name, '<=', to) if to
    bulk_update({element_name => Spider::QueryFuncs::Expression.new(expr)}, cond)
end