Module: ATSPI::Collection

Includes:
Enumerable
Included in:
SelectableCollection, SelectableCollection::Selected
Defined in:
lib/atspi/collection.rb

Overview

Included in classes representing a collection that can be iterated over. A Collection is an Enumerable adjusted to the what we can efficiently get from libatspi. In particular, it provides access to items by index and from the end of the collection.

Enumerable interface collapse

Representation collapse

Instance Method Details

#[](idx) ⇒ Object

alias for #at



29
30
31
# File 'lib/atspi/collection.rb', line 29

def [](idx)
  at(idx)
end

#at(idx) ⇒ Object

Returns item at index idx.

Parameters:

  • idx (Integer)

Returns:

  • (Object)

    item at index idx



18
19
20
21
22
23
24
25
26
# File 'lib/atspi/collection.rb', line 18

def at(idx)
  if idx.between?(0, count-1)
    yield idx
  elsif idx.between?(-count, -1)
    yield count+idx
  else
    nil
  end
end

#count0

Returns default size of collection.

Returns:

  • (0)

    default size of collection



34
35
36
# File 'lib/atspi/collection.rb', line 34

def count
  0
end

#eachObject

prerequisite for Enumerable



10
11
12
13
14
# File 'lib/atspi/collection.rb', line 10

def each
  count.times.each do |idx|
    yield at(idx)
  end
end

#inspectString

Returns instance as inspectable string.

Returns:

  • (String)

    instance as inspectable string



55
56
57
# File 'lib/atspi/collection.rb', line 55

def inspect
  "#<#{self.class.name}:0x#{'%x14' % __id__} @count=#{count}>"
end

#last(n = 1) ⇒ Object+

Returns item(s) from the end.

Parameters:

  • n (Integer) (defaults to: 1)

    number of items to return

Returns:

  • (Object, Array<Object>)

    item(s) from the end



44
45
46
47
48
49
50
# File 'lib/atspi/collection.rb', line 44

def last(n = 1)
  if n > 1
    [n,count].min.downto(1).map{ |idx| at(-idx) }
  else
    at(-1)
  end
end

#lengthObject

alias for #count



40
# File 'lib/atspi/collection.rb', line 40

def length; count end

#sizeObject

alias for #count



38
# File 'lib/atspi/collection.rb', line 38

def size; count end