vapir-common 1.7.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,83 @@
1
+ require "vapir-common/specifier"
2
+
3
+ module Vapir
4
+ class ElementCollection
5
+ include Enumerable
6
+
7
+ def initialize(container, collection_class, extra={})
8
+ @container=container
9
+ @collection_class=collection_class
10
+ @extra=extra.merge(:container => container)
11
+ end
12
+
13
+ def each
14
+ candidates.each do |candidate|
15
+ yield @collection_class.new(:element_object, candidate, @extra)
16
+ end
17
+ self
18
+ end
19
+ def each_index
20
+ (1..length).each do |i|
21
+ yield i
22
+ end
23
+ end
24
+ def length
25
+ candidates.length
26
+ end
27
+ alias size length
28
+ def empty?
29
+ size==0
30
+ end
31
+ alias each_with_enumerable_index each_with_index # call ruby's 0-based indexing enumerable_index; call ours element_index
32
+ def each_with_element_index
33
+ index=1
34
+ candidates.each do |candidate|
35
+ yield @collection_class.new(:index, nil, @extra.merge(:index => index, :element_object => candidate)), index
36
+ index+=1
37
+ end
38
+ self
39
+ end
40
+ alias each_with_index each_with_element_index
41
+
42
+ def [](index)
43
+ at(index)
44
+ end
45
+ def at(index)
46
+ @collection_class.new(:index, nil, @extra.merge(:index => index))
47
+ end
48
+ def first
49
+ at(:first)
50
+ end
51
+ def last
52
+ at(:last)
53
+ end
54
+
55
+ def find(&block)
56
+ element=@collection_class.new(:custom, block, @extra.merge(:locate => false))
57
+ element.exists? ? element : nil
58
+ end
59
+ alias detect find
60
+
61
+ def inspect
62
+ "\#<#{self.class.name}:0x#{"%.8x"%(self.hash*2)} #{map{|el|el.inspect}.join(', ')}>"
63
+ end
64
+
65
+ private
66
+ include ElementObjectCandidates
67
+ def candidates
68
+ assert_container_exists
69
+ matched_candidates(@collection_class.specifiers, @collection_class.all_dom_attr_aliases)
70
+ end
71
+ public
72
+ def pretty_print(pp)
73
+ pp.object_address_group(self) do
74
+ pp.seplist(self, lambda { pp.text ',' }) do |element|
75
+ pp.breakable ' '
76
+ pp.group(0) do
77
+ pp.pp element
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end