Module: AnnotatedArray
- Defined in:
- lib/scout/annotation/array.rb
Defined Under Namespace
Modules: AnnotatedArrayItem
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.is_contained?(obj) ⇒ Boolean
7
8
9
|
# File 'lib/scout/annotation/array.rb', line 7
def self.is_contained?(obj)
AnnotatedArrayItem === obj
end
|
Instance Method Details
#[](pos, clean = false) ⇒ Object
21
22
23
24
25
|
# File 'lib/scout/annotation/array.rb', line 21
def [](pos, clean = false)
item = super(pos)
return item if item.nil? or clean
annotate_item(item, pos)
end
|
#annotate_item(obj, position = nil) ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/scout/annotation/array.rb', line 11
def annotate_item(obj, position = nil)
return obj if obj.nil?
obj = obj.dup if obj.frozen?
obj.extend AnnotatedArray if Array === obj
obj.extend AnnotatedArrayItem
obj.container = self
obj.container_index = position
self.annotate(obj)
end
|
#collect(&block) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/scout/annotation/array.rb', line 65
def collect(&block)
if block_given?
inject([]){|acc,item| acc.push(block.call(item)); acc }
else
inject([]){|acc,item| acc.push(item); acc }
end
end
|
#each(&block) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/scout/annotation/array.rb', line 50
def each(&block)
i = 0
super do |item|
block.call annotate_item(item, i)
i += 1
end
end
|
#each_with_index(&block) ⇒ Object
35
36
37
38
39
|
# File 'lib/scout/annotation/array.rb', line 35
def each_with_index(&block)
super do |item,i|
block.call annotate_item(item, i)
end
end
|
#first ⇒ Object
27
28
29
|
# File 'lib/scout/annotation/array.rb', line 27
def first
annotate_item(super, 0)
end
|
#inject(acc, &block) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/scout/annotation/array.rb', line 58
def inject(acc, &block)
each do |item|
acc = block.call acc, item
end
acc
end
|
#last ⇒ Object
31
32
33
|
# File 'lib/scout/annotation/array.rb', line 31
def last
annotate_item(super, self.length - 1)
end
|
#select(&block) ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/scout/annotation/array.rb', line 41
def select(&block)
selected = []
each do |item|
selected << item if block.call(item)
end
self.annotate(selected)
end
|