Module: Spider::Model::Versioned

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

Defined Under Namespace

Modules: ClassMethods, Mapper

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) create_version_model(model)



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spiderfw/model/mixins/versioned.rb', line 30

def self.create_version_model(model)
    vmod = Class.new(model)
    vmod.class_eval{ include VersionModel }
    vmod.remove_element(:v_sha1)
    vmod.primary_keys.each do |pk|
         vmod.element_attributes(pk.name, :primary_key => false)
     end
    vmod.element(:v_sha1, String, :primary_key => true, :length => 40, :version_pk => true, :hidden => true) 
    local_pk = "id_versioned_#{model.short_name.downcase}"
    vmod.element(local_pk, Fixnum, :autoincrement => true, :local_pk => true, :hidden => true)
    vmod
end

+ (Object) included(model)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spiderfw/model/mixins/versioned.rb', line 9

def self.included(model)
    return if model.is_a?(ClassMethods) && model.version_model
    model.extend(ClassMethods)
    model.mapper_include(Mapper)
    model.remove_element(:v_sha1) if model.elements[:v_sha1] && model.elements[:v_sha1].integrated?
    unless model.elements[:v_sha1] #|| model.attributes[:sub_model]
        model.element(:v_sha1, String, :length => 40, :hidden => true) 
    end
    
    #model.versioning
    par = model.containing_module
    local_name = model.name.sub(par.name+'::', '')
    unless par.const_defined?(:Versioned)
        par.const_set(:Versioned, Module.new)
    end
    versioned_module = par.const_get(:Versioned)
    vmod = Versioned.create_version_model(model)
    versioned_module.const_set_full(local_name, vmod)
    model.set_version_model(nil, vmod)
end

Instance Method Details

- (Object) last_version



540
541
542
543
544
# File 'lib/spiderfw/model/mixins/versioned.rb', line 540

def last_version
    self.class.version_model.where{ |s| 
        (s.v_original == self)
    }.order_by(:version_date, :desc).limit(1)[0]
end

- (Object) polymorphic_become(model)



526
527
528
529
530
531
532
# File 'lib/spiderfw/model/mixins/versioned.rb', line 526

def polymorphic_become(model)
    return super unless model < Spider::Model::VersionModel
    obj = super
    el = self.class.polymorphic_models[model][:through]
    super_obj = obj.get(el)
    return model.where(el => super_obj).order_by(:version_date, :desc).limit(1)[0]
end

- (Object) populate_version_object(vobj)



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/spiderfw/model/mixins/versioned.rb', line 461

def populate_version_object(vobj)
    mod = self.class
    vmod = mod.version_model
    version_contents = self.class.version_contents
    
    self.class.elements_array.each do |el|
        v_el = vmod.elements[el.name]
        next unless v_el
        next if el.model? && el.type.attributes[:version_model]
        next if el.integrated? && !(el.integrated_from.embedded? && vmod.storage.supports?(:embedding))
        is_embedded = el.attributes[:embedded] && vmod.storage.supports?(:embedding)
        next if el.multiple? && el.model.respond_to?(:version_element) && !is_embedded
        is_version_content = version_contents.include?(el)
        #debugger if el.name == :news_list
        el_val = self.get(el)
        if self.respond_to?(:prepare_version_object)
            el_val = self.prepare_version_object(el.name, el_val) 
        end
        #next unless mod.mapper.have_references?(el)
        if el_val && el.multiple?
            if !(v_el.model < Spider::Model::VersionModel)
                el_val.each do |v|
                    vobj.get(el.name) << v
                end
            elsif is_embedded && el.junction?
                el_val = instance_variable_get("@#{el.name}_junction") if !el.attributes[:keep_junction]
                el_val.each do |v|
                    jv = v_el.model.new
                    jv_sha1 = v.version_sha1
                    v.populate_version_object(jv)
                    jv.v_sha1 = jv_sha1
                    vobj.get(el.name) << jv
                end
            end
        elsif el.model? && el_val && v_el.type < Spider::Model::VersionModel # && is_version_content 
            
            vobj.set(el.name, el_val.get(el.type.version_element))
                    # if self.class.attributes[:sub_model] && self.class.attributes[:sub_model].respond_to?(:prepare_junction_version_object)
                    #     prep_val = self.class.attributes[:sub_model].prepare_junction_version_object(self.class.attributes[:sub_model_element], el_val)
                    #     prep_val.save_version
                    # end
        else
            vobj.set(el.name, el_val)
        end
    end
end

- (Object) previous_version(back = 1)



534
535
536
537
538
# File 'lib/spiderfw/model/mixins/versioned.rb', line 534

def previous_version(back=1)
    self.class.version_model.where{ |s| 
        (s.v_original == self) & (s.v_sha1 .not self.v_sha1) 
    }.order_by(:version_date, :desc).limit(1).offset(back-1)[0]
end

- (Object) save_version(comment = nil)



508
509
510
511
512
513
514
# File 'lib/spiderfw/model/mixins/versioned.rb', line 508

def save_version(comment=nil)
    obj = nil
    Spider::Model.in_unit do |uow|
        obj = Spider::Model.identity_mapper.put(self)
        uow.add(obj, :save_version)
    end
end

- (Object) v_obj_pks(obj, klass, use_sha)



338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/spiderfw/model/mixins/versioned.rb', line 338

def v_obj_pks(obj, klass, use_sha)
    if use_sha && obj.respond_to?(:v_sha1)
        return obj.v_sha1
    else
        unless obj
            return klass.primary_keys.length > 1 ? [] : nil
        end
        pks = obj.primary_keys
        return pks[0] if pks.length == 1
        return pks
    end
end

- (Boolean) version?



522
523
524
# File 'lib/spiderfw/model/mixins/versioned.rb', line 522

def version?
    false
end

- (Object) version_content_hash(params = {})



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/spiderfw/model/mixins/versioned.rb', line 395

def version_content_hash(params={})
    h = {:class => self.class.name}
    version_contents = self.class.version_contents
    exclude_elements = self.class.version_ignored_elements
    params[:except_objects] ||= []
    except_objects = params[:except_objects].clone
    except_objects << self
    self.class.elements_array.each do |el|
        next if exclude_elements.include?(el.name)
        next if el.name == :v_sha1
        next if el.name == :history
        next if params[:except] && params[:except].include?(el.name)
        next if el.integrated?
        if el.model?
            obj = get(el)
            next unless obj
            if el.multiple?
                obj.each{ |o| except_objects << o }
            else
                except_objects << obj
            end
        end
    end
    self.class.elements_array.each do |el|                
        next if exclude_elements.include?(el.name)
        next if el.name == :v_sha1
        next if el.name == :history
        next if params[:except] && params[:except].include?(el.name)
        next if el.integrated?
        
        if (el.model?)
            obj = get(el)
            next if params[:except_objects].include?(obj)
            if self.class.attributes[:sub_model] && self.class.attributes[:sub_model].respond_to?(:prepare_junction_version_object)
                obj =  self.class.attributes[:sub_model].prepare_junction_version_object(self.class.attributes[:sub_model_element], obj)
            elsif self.respond_to?(:prepare_version_object)
                obj = self.prepare_version_object(el.name, obj)
            end
            next if params[:except_objects].include?(obj)
            
            if !obj
               h[el.name] = nil
            elsif el.type.method_defined?(:version_content_hash)
                if (el.multiple?)
                    h[el.name] = obj.reject{ |o| 
                        params[:except_objects].include?(o) 
                    }.map{ |o| o.version_content_hash(:except_objects => except_objects) }
                else
                    h[el.name] = obj.version_content_hash(:except_objects => except_objects)
                end
            else
                if (el.multiple?)
                    h[el.name] = obj.reject{ |o| 
                        params[:except_objects].include?(o) 
                    }.map{ |o| params[:except_objects] << o; o.to_yaml_h(:except => exclude_elements + [el.reverse]) }
                else
                    h[el.name] = obj.to_yaml_h(:except => exclude_elements + [el.reverse])
                end
            end
        else
            h[el.name] = get(el)
        end
    end
    h
end

- (Object) version_flatout(params = {})



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/spiderfw/model/mixins/versioned.rb', line 336

def version_flatout(params={})
    h = {}
    def v_obj_pks(obj, klass, use_sha)
        if use_sha && obj.respond_to?(:v_sha1)
            return obj.v_sha1
        else
            unless obj
                return klass.primary_keys.length > 1 ? [] : nil
            end
            pks = obj.primary_keys
            return pks[0] if pks.length == 1
            return pks
        end
    end 

    version_contents = self.class.version_contents
    exclude_elements = self.class.version_ignored_elements
    self.class.elements_array.each do |el|
        next if exclude_elements.include?(el.name)
        next if el.name == :v_sha1
        next if el.name == :history
        next if params[:except] && params[:except].include?(el.name)
        next if el.integrated?
        if (el.model?)
            is_version_content = version_contents.include?(el)
            obj = get(el)
            unless obj
                h[el.name] = obj
                next
            end
            if self.class.attributes[:sub_model] && self.class.attributes[:sub_model].respond_to?(:prepare_junction_version_object)
                obj = self.class.attributes[:sub_model].prepare_junction_version_object(self.class.attributes[:sub_model_element], obj)
            elsif self.respond_to?(:prepare_version_object)
                obj = self.prepare_version_object(el.name, obj)
            end
            if is_version_content && self.class.mapper.have_references?(el)
                if (el.multiple?)
                    h[el.name] = obj.map{ |o| o.v_sha1 }
                else
                    h[el.name] = obj.v_sha1
                end
            elsif is_version_content && el.type < Spider::Model::Versioned
                if (el.multiple?)
                    h[el.name] = obj.map{ |o| o.version_content_hash(:except_objects => [self]) }
                else
                    h[el.name] = obj.version_content_hash(:except_objects => [self])
                end
            elsif (el.multiple?)
                h[el.name] = obj.map{ |o| v_obj_pks(o, el.model, false) }
            else
                h[el.name] = v_obj_pks(obj, el.model, false)
            end
        else
            h[el.name] = get(el)
        end
    end
    return h
end

- (Boolean) version_modified?



518
519
520
# File 'lib/spiderfw/model/mixins/versioned.rb', line 518

def version_modified?
    self.version_sha1 != self.get(self.class.version_element)
end

- (Object) version_sha1



326
327
328
329
# File 'lib/spiderfw/model/mixins/versioned.rb', line 326

def version_sha1
    yaml = YAML::dump(self.version_flatout)
    sha1 = Digest::SHA1.hexdigest(yaml)
end

- (Object) version_sha1_getnew



331
332
333
334
# File 'lib/spiderfw/model/mixins/versioned.rb', line 331

def version_sha1_getnew
    yaml = YAML::dump(self.get_new.version_flatout)
    sha1 = Digest::SHA1.hexdigest(yaml)
end