Class: Bag

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ObjectModel/AnEntity/Bag.rb

Direct Known Subclasses

ChildrenBag, ReferencesBag

Instance Method Summary collapse

Constructor Details

#initialize(entity, attr_name, repository) ⇒ Bag

Returns a new instance of Bag.



4
5
6
7
8
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 4

def initialize entity, attr_name, repository		
	@entity, @entity_id, @attr_name, @repository = entity, entity.entity_id, attr_name, repository
	@array = []		
	@ivname = "@#{@attr_name}"
end

Instance Method Details

#<<(value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 52

def << value		
	if tr = Thread.current[:om_transaction]
		tr.event_processor.fire_before @entity, new_item_method_name, @attr_name, value
		copy = tr.copy_get! @entity
		copy[@ivname].add value.entity_id
		tr.event_processor.fire_after @entity, new_item_method_name, @attr_name, value
	else
		raise NoTransactionError
	end
end

#_arrayObject



10
11
12
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 10

def _array
	@array
end

#clearObject



63
64
65
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 63

def clear
	delete_if{true}
end

#delete(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 14

def delete value	
	if tr = Thread.current[:om_transaction]
		return unless value.is_a? Entity						
		copy = tr.copy_get! @entity
		previous = false
		copy[@ivname].delete_if do |entity_id| 
			if previous
				tr.event_processor.fire_after @entity, delete_item_method_name, @attr_name, value
				previous = false
			end
			
			if value.entity_id == entity_id
				tr.event_processor.fire_before @entity, delete_item_method_name, @attr_name, value
				previous = true
				true
			else
				false
			end
		end			
		
		tr.event_processor.fire_after @entity, delete_item_method_name, @attr_name, value if previous
	else
		raise NoTransactionError
	end
end

#delete_if(&b) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 67

def delete_if &b	
	if tr = Thread.current[:om_transaction]
		copy = tr.copy_get! @entity
		previous_entity = nil
		copy[@ivname].delete_if do |entity_id| 								
			entity = tr.resolve(entity_id)
			
			if previous_entity != nil
				tr.event_processor.fire_after @entity, delete_item_method_name, @attr_name, previous_entity
				previous_entity = entity
			end
			
			if b.call entity
				tr.event_processor.fire_before @entity, delete_item_method_name, @attr_name, entity
				previous = true
				true
			else
				false
			end
		end			
		
		if previous_entity != nil
			tr.event_processor.fire_after @entity, delete_item_method_name, @attr_name, previous_entity 
		end
	else
		raise NoTransactionError
	end		
end

#each(&b) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 96

def each &b
	tr = Thread.current[:om_transaction]
	if tr and tr.changed? @entity_id
			array = tr.copies[@entity_id][@ivname]			
			array.each{|entity_id| b.call tr.resolve(entity_id)}
	else
		@array.each{|entity_id| b.call @repository.by_id(entity_id)}
	end				
end

#empty?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 122

def empty?
	size == 0
end

#idsObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 106

def ids
	if tr = Thread.current[:om_transaction]
		if tr.changed? @entity_id
			tr.copies[@entity_id][@ivname]
		else
			@array
		end				
	else
		@array
	end		
end

#inspectObject



126
127
128
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 126

def inspect
	to_s
end

#sizeObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 40

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

#to_sObject



118
119
120
# File 'lib/ObjectModel/AnEntity/Bag.rb', line 118

def to_s
	"#<Bag '#{@entity_id}': [#{@array.join(', ')}]>"
end