Class: BackReferences

Inherits:
Object
  • Object
show all
Defined in:
lib/ObjectModel/AnEntity/BackReferences.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_id) ⇒ BackReferences

Returns a new instance of BackReferences.



2
3
4
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 2

def initialize entity_id
  @entity_id, @array = entity_id, []
end

Class Method Details

.delete(e, storage) ⇒ Object



92
93
94
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 92

def delete e, storage
  storage[:back_references].filter(:entity_id => e.entity_id).delete
end

.delete_all_references_to(e, c, &callback) ⇒ Object



108
109
110
111
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 108

def delete_all_references_to e, c, &callback
  array = c.back_references._array.dup
  array.each{|entity_id| callback.call entity_id}            
end

.delete_backreference(entity, reference, reference_copy) ⇒ Object



96
97
98
99
100
101
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 96

def delete_backreference entity, reference, reference_copy
  br = reference_copy.back_references
  index = br._array.index entity.entity_id
  index.should! :>=, 0
  br._array.delete_at index
end

.initialize_copy(e, c) ⇒ Object



68
69
70
71
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 68

def initialize_copy e, c
  c.back_references = BackReferences.new e.entity_id
  c.back_references._array.replace e.back_references._array
end

.initialize_entity(e) ⇒ Object



73
74
75
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 73

def initialize_entity e
  e.instance_variable_set "@back_references", BackReferences.new(e.entity_id)
end

.initialize_storage(db) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 33

def initialize_storage db
  db.create_table :back_references do
    column :entity_id, :text
    column :referrer_id, :text
    
#       index :entity_id
  end
end

.load(e, storage) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 48

def load e, storage
  rows = storage[:back_references].filter :entity_id => e.entity_id

  br = if rows.count > 0
    br = BackReferences.new e.entity_id
    rows.each do |row|
      br._array << EntityType.load_id(row[:referrer_id])
    end
    br
  else
    BackReferences.new e.entity_id
  end
#           p [e, br._array]
  e.instance_variable_set "@back_references", br
end

.new_backreference(entity, reference, reference_copy) ⇒ Object



103
104
105
106
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 103

def new_backreference entity, reference, reference_copy
  br = reference_copy.back_references      
  br._array << entity.entity_id      
end

.persist(c, entity_id, storage) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 77

def persist c, entity_id, storage
  # TODO reimplement more efficiently      
  br = c.back_references
  storage[:back_references].filter(:entity_id => entity_id).delete
  if br._array.size > 0          
    br._array.each do |ref_id|    
      ref_id.should_not! :be_nil
      storage[:back_references].insert(
                                      :entity_id => entity_id, 
                                      :referrer_id => EntityType.dump_id(ref_id)
      )
    end               
  end                  
end


42
43
44
45
46
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 42

def print_storage db, name
  return unless name == nil or name == :back_references
  puts "\nBackReferences:"
  db[:back_references].print
end

.write_back(copy, entity) ⇒ Object



64
65
66
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 64

def write_back copy, entity
  entity.back_references._array.replace copy.back_references._array # TODO reimplement more efficiently
end

Instance Method Details

#_arrayObject



28
29
30
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 28

def _array
  @array
end

#each(&b) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 6

def each &b
  tr = Thread.current[:om_transaction]
  if tr and tr.changed? @entity_id
    br = tr.copies[@entity_id].back_references      
    br._array.each{|entity_id| b.call tr.resolve(entity_id)}
  else
    self._array.each{|entity_id| b.call @om_repository.by_id(entity_id)}
  end    
end

#sizeObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ObjectModel/AnEntity/BackReferences.rb', line 16

def size
  if tr = Thread.current[:om_transaction]      
    if tr.changed? @entity_id
      tr.copies[@entity_id].back_references._array.size
    else
      @array.size
    end
  else
    @array.size
  end
end