Module: External::Enumerable

Included in:
Base
Defined in:
lib/external/enumerable.rb

Overview

An externalized implementation of Enumerable. External::Enumerable requires several methods with the following functionality:

each

iterates over items in self

another

provide a another instance of self

to_a

converts self to an Array

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enumerate_to_aObject

Flag indicating whether to enumerate (ie collect, select, etc) into an array or into an instance of self. In most cases enumerating to an array performs better, but enumerating to another instance of self may be desired for especially large collections.



19
20
21
# File 'lib/external/enumerable.rb', line 19

def enumerate_to_a
  @enumerate_to_a
end

Instance Method Details

#all?Boolean

:yield: obj

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/external/enumerable.rb', line 21

def all? # :yield: obj
  # WARN -- no tests for this in test_array
  each do |obj|
    return false unless yield(obj)
  end
  true
end

#any?Boolean

:yield: obj

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/external/enumerable.rb', line 29

def any? # :yield: obj
  # WARN -- no tests for this in test_array
  each do |obj|
    return true if yield(obj)
  end
  false
end

#collectObject

:yield: item



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/external/enumerable.rb', line 37

def collect # :yield: item
  if block_given?
    another = enumerate_to_a ? [] : self.another
    each do |item|
      another << yield(item)
    end
    another
  else
    # Not sure if Enumerator works right for large externals...
    Object::Enumerable::Enumerator.new(self)
  end
end

#detect(ifnone = nil) ⇒ Object

def collect! # :yield: item

not_implemented

end



54
55
56
57
58
59
60
# File 'lib/external/enumerable.rb', line 54

def detect(ifnone=nil) # :yield: obj
  # WARN -- no tests for this in test_array
  each do |obj|
    return obj if yield(obj)
  end
  nil
end

#each_with_index(&block) ⇒ Object

def each_slice(n) # :yield:

not_implemented

end



70
71
72
73
74
75
76
# File 'lib/external/enumerable.rb', line 70

def each_with_index(&block)
  chunk do |offset, length|
    self[offset, length].each_with_index do |item, i|
      yield(item, i + offset)
    end
  end
end

#entriesObject



78
79
80
# File 'lib/external/enumerable.rb', line 78

def entries
  to_a
end

#find(ifnone = nil, &block) ⇒ Object

def enum_with_index

not_implemented

end



94
95
96
97
# File 'lib/external/enumerable.rb', line 94

def find(ifnone=nil, &block) # :yield: obj
  # WARN -- no tests for this in test_array
  detect(ifnone, &block)
end

#find_allObject

:yield: obj



99
100
101
102
103
104
105
# File 'lib/external/enumerable.rb', line 99

def find_all # :yield: obj
  another = enumerate_to_a ? [] : self.another
  each do |item|
    another << item if yield(item)
  end
  another
end

#include?(obj) ⇒ Boolean

def grep(pattern) # :yield: obj

not_implemented

end

Returns:

  • (Boolean)


111
112
113
114
115
116
# File 'lib/external/enumerable.rb', line 111

def include?(obj)
  each do |current|
    return true if current == obj
  end
  false
end

#map(&block) ⇒ Object

def inject(init) # :yield: memo, obj

not_implemented

end



122
123
124
# File 'lib/external/enumerable.rb', line 122

def map(&block) # :yield: item
  collect(&block)
end

#member?(obj) ⇒ Boolean

def max # :yield: a,b

not_implemented

end

Returns:

  • (Boolean)


134
135
136
# File 'lib/external/enumerable.rb', line 134

def member?(obj)
  include?(obj)
end

#select(&block) ⇒ Object

def reject! # :yield: item

not_implemented

end



154
155
156
# File 'lib/external/enumerable.rb', line 154

def select(&block) # :yield: obj
  find_all(&block)
end