Class: Vapir::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, ElementObjectCandidates
Defined in:
lib/vapir-common/element_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(container, collection_class, extra = {}, how = nil, what = nil) ⇒ ElementCollection

Returns a new instance of ElementCollection.



7
8
9
10
11
12
13
# File 'lib/vapir-common/element_collection.rb', line 7

def initialize(container, collection_class, extra={}, how=nil, what=nil)
  @container=container
  @collection_class=collection_class
  @extra=extra.merge(:container => container)
  @how=how
  @what=what
end

Instance Method Details

#[](index) ⇒ Object

returns the element at the given index in the collection. indices start at 1.



64
65
66
# File 'lib/vapir-common/element_collection.rb', line 64

def [](index)
  at(index)
end

#at(index) ⇒ Object

returns the element at the given index in the collection. indices start at 1.



68
69
70
# File 'lib/vapir-common/element_collection.rb', line 68

def at(index)
  @collection_class.new(@how, @what, @extra.merge(:index => index))
end

#by_indexObject



60
61
62
# File 'lib/vapir-common/element_collection.rb', line 60

def by_index
  Enumerator.new(self, :each_by_index)
end

#eachObject

yields each element in the collection to the given block



16
17
18
19
20
21
22
# File 'lib/vapir-common/element_collection.rb', line 16

def each # :yields: element
  element_objects.each do |element_object|
    # todo: instantiated Element should use @how/@what? that would make #each the same as with #each_by_index 
    yield @collection_class.new(:element_object, element_object, @extra)
  end
  self
end

#each_by_indexObject

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.



55
56
57
58
59
# File 'lib/vapir-common/element_collection.rb', line 55

def each_by_index # :yields: element
  each_with_element_index do |element, i|
    yield element
  end
end

#each_indexObject

yields each index from 1..length.

note that if you are using this and accessing each subscript, it will be exponentially slower than using #each or #each_with_index.



27
28
29
30
31
# File 'lib/vapir-common/element_collection.rb', line 27

def each_index # :yields: index
  (1..length).each do |i|
    yield i
  end
end

#each_with_element_indexObject Also known as: each_with_index

yields each element and index in the collection



43
44
45
46
47
48
49
50
# File 'lib/vapir-common/element_collection.rb', line 43

def each_with_element_index # :yields: element, index
  index=1
  element_objects.each do |element_object|
    yield @collection_class.new(@how, @what, @extra.merge(:index => index, :element_object => element_object, :locate => false)), index
    index+=1
  end
  self
end

#empty?Boolean

returns true if this collection contains no elements

Returns:

  • (Boolean)


38
39
40
# File 'lib/vapir-common/element_collection.rb', line 38

def empty?
  size==0
end

#enumerable_findObject



100
# File 'lib/vapir-common/element_collection.rb', line 100

alias enumerable_find find

#enumerable_rejectObject



90
# File 'lib/vapir-common/element_collection.rb', line 90

alias enumerable_reject reject

#enumerable_selectObject



80
# File 'lib/vapir-common/element_collection.rb', line 80

alias enumerable_select select

#find(&block) ⇒ Object Also known as: detect

returns an element for which the given block returns true (that is, not false or nil) when yielded that element

returns nil if no such element exists.



104
105
106
107
108
109
110
111
# File 'lib/vapir-common/element_collection.rb', line 104

def find(&block) # :yields: element
  if @how # can't set how=:custom if @how is given to us, so fall back to Enumerable's #find method 
    enumerable_find(&block)
  else
    element=@collection_class.new(:custom, block, @extra.merge(:locate => false))
    element.exists? ? element : nil
  end
end

#find!(&block) ⇒ Object Also known as: detect!

returns an element for which the given block returns true (that is, not false or nil) when yielded that element

raises UnknownObjectException if no such element exists.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vapir-common/element_collection.rb', line 116

def find!(&block)
  if @how # can't set how=:custom if @how is given to us, so fall back to Enumerable's #find method 
    enumerable_find(&block) || begin
      # TODO: DRY against Element#locate!
      klass=(@collection_class <= Frame) ? Vapir::Exception::UnknownFrameException : Vapir::Exception::UnknownObjectException
      message="Unable to locate #{@collection_class} using custom find block"
      message+="\non element collection #{self.inspect}"
      message+="\non container: #{@container.inspect}"
      raise(klass, message)
    end
  else
    element=@collection_class.new(:custom, block, @extra.merge(:locate => :assert))
  end
end

#firstObject

returns the first element in the collection.



72
73
74
# File 'lib/vapir-common/element_collection.rb', line 72

def first
  at(:first)
end

#inspectObject

:nodoc:



163
164
165
166
# File 'lib/vapir-common/element_collection.rb', line 163

def inspect # :nodoc: 
  # todo: include how/what if set 
  "\#<#{self.class.name}:0x#{"%.8x"%(self.hash*2)} #{map{|el|el.inspect}.join(', ')}>"
end

#lastObject

returns the last element. this will refer to the last element even if the number of elements changes, assuming relocation.



76
77
78
# File 'lib/vapir-common/element_collection.rb', line 76

def last
  at(:last)
end

#lengthObject Also known as: size

returns the number of elements in the collection



33
34
35
# File 'lib/vapir-common/element_collection.rb', line 33

def length
  element_objects.length
end

#pretty_print(pp) ⇒ Object

:nodoc:



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vapir-common/element_collection.rb', line 167

def pretty_print(pp) # :nodoc: 
  # todo: include how/what if set 
  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

#reject(&block) ⇒ Object

:yields: element



91
92
93
94
95
96
97
98
# File 'lib/vapir-common/element_collection.rb', line 91

def reject(&block) # :yields: element
  # TODO: test 
  if @how
    enumerable_reject(&block)
  else
    ElementCollection.new(@container, @collection_class, @extra, :custom, proc{|el| !block.call(el) })
  end
end

#select(&block) ⇒ Object

:yields: element



81
82
83
84
85
86
87
88
# File 'lib/vapir-common/element_collection.rb', line 81

def select(&block) # :yields: element
  # TODO: test
  if @how
    enumerable_select(&block)
  else
    ElementCollection.new(@container, @collection_class, @extra, :custom, block)
  end
end