Class: Vapir::ElementCollection
- Inherits:
-
Object
- Object
- Vapir::ElementCollection
- Includes:
- Enumerable, ElementObjectCandidates
- Defined in:
- lib/vapir-common/element_collection.rb
Instance Method Summary collapse
-
#[](index) ⇒ Object
returns the element at the given index in the collection.
- #at(index) ⇒ Object
- #each ⇒ Object
-
#each_by_index ⇒ Object
yields each element, specified by index (as opposed to by :element_object as #each yields) same as #each_with_index, except it doesn’t yield the index number.
- #each_index ⇒ Object
- #each_with_element_index ⇒ Object (also: #each_with_index)
- #empty? ⇒ Boolean
- #find(&block) ⇒ Object (also: #detect)
- #first ⇒ Object
-
#initialize(container, collection_class, extra = {}) ⇒ ElementCollection
constructor
A new instance of ElementCollection.
- #inspect ⇒ Object
- #last ⇒ Object
- #length ⇒ Object (also: #size)
- #pretty_print(pp) ⇒ Object
Constructor Details
#initialize(container, collection_class, extra = {}) ⇒ ElementCollection
Returns a new instance of ElementCollection.
7 8 9 10 11 |
# File 'lib/vapir-common/element_collection.rb', line 7 def initialize(container, collection_class, extra={}) @container=container @collection_class=collection_class @extra=extra.merge(:container => container) end |
Instance Method Details
#[](index) ⇒ Object
returns the element at the given index in the collection. indices start at 1.
50 51 52 |
# File 'lib/vapir-common/element_collection.rb', line 50 def [](index) at(index) end |
#at(index) ⇒ Object
53 54 55 |
# File 'lib/vapir-common/element_collection.rb', line 53 def at(index) @collection_class.new(:index, nil, @extra.merge(:index => index)) end |
#each ⇒ Object
13 14 15 16 17 18 |
# File 'lib/vapir-common/element_collection.rb', line 13 def each candidates.each do |candidate| yield @collection_class.new(:element_object, candidate, @extra) end self end |
#each_by_index ⇒ Object
yields each element, specified by index (as opposed to by :element_object as #each yields) same as #each_with_index, except it doesn’t yield the index number.
44 45 46 47 48 |
# File 'lib/vapir-common/element_collection.rb', line 44 def each_by_index # :yields: element each_with_element_index do |element, i| yield element end end |
#each_index ⇒ Object
19 20 21 22 23 |
# File 'lib/vapir-common/element_collection.rb', line 19 def each_index (1..length).each do |i| yield i end end |
#each_with_element_index ⇒ Object Also known as: each_with_index
32 33 34 35 36 37 38 39 |
# File 'lib/vapir-common/element_collection.rb', line 32 def each_with_element_index index=1 candidates.each do |candidate| yield @collection_class.new(:index, nil, @extra.merge(:index => index, :element_object => candidate, :locate => false)), index index+=1 end self end |
#empty? ⇒ Boolean
28 29 30 |
# File 'lib/vapir-common/element_collection.rb', line 28 def empty? size==0 end |
#find(&block) ⇒ Object Also known as: detect
63 64 65 66 |
# File 'lib/vapir-common/element_collection.rb', line 63 def find(&block) element=@collection_class.new(:custom, block, @extra.merge(:locate => false)) element.exists? ? element : nil end |
#first ⇒ Object
56 57 58 |
# File 'lib/vapir-common/element_collection.rb', line 56 def first at(:first) end |
#inspect ⇒ Object
69 70 71 |
# File 'lib/vapir-common/element_collection.rb', line 69 def inspect "\#<#{self.class.name}:0x#{"%.8x"%(self.hash*2)} #{map{|el|el.inspect}.join(', ')}>" end |
#last ⇒ Object
59 60 61 |
# File 'lib/vapir-common/element_collection.rb', line 59 def last at(:last) end |
#length ⇒ Object Also known as: size
24 25 26 |
# File 'lib/vapir-common/element_collection.rb', line 24 def length candidates.length end |
#pretty_print(pp) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/vapir-common/element_collection.rb', line 80 def pretty_print(pp) pp.object_address_group(self) do pp.seplist(self, lambda { pp.text ',' }) do |element| pp.breakable ' ' pp.group(0) do pp.pp element end end end end |