Module: Spider::Model::List::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#list(name, attributes = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/spiderfw/model/mixins/list.rb', line 113

def list(name, attributes={})
    attributes[:list] = true
    attributes[:order] ||= true
    element(name, Fixnum, attributes)
    observe_element(name) do |obj, el, new_val|
        obj.save_mode do
            obj.list_mixin_modified_elements[name] = obj.get(el)
        end
    end
    
    (class << self; self; end).instance_eval do
        
        define_method("#{name}_last") do
            qs = self.all.order_by(name).limit(1)
            return qs[0]
        end
        
    end
    
    define_method("insert_last_#{name}") do
        # FIXME: locking!
        last = self.class.send("#{name}_last")
        max = last ? last.get(name) : 0
        set(name, max + 1)
    end
end

#listsObject



109
110
111
# File 'lib/spiderfw/model/mixins/list.rb', line 109

def lists
    elements_array.select{ |el| el.attributes[:list] }
end