Sha256: 5551dcb78d3cf0844a0d677c587544e3f9e653b25138a685930599c13c834214

Contents?: true

Size: 946 Bytes

Versions: 8

Compression:

Stored size: 946 Bytes

Contents

module Quarry
  class EnumerableHelper
    include Enumerable
    attr_reader :size
    
    def initialize(data_set, container, klass, size, get_fn)
      @size = container.send(size)
      @container = container
      @data_set = data_set
      @get_fn = get_fn
      @klass = klass
      
      # because of the way the Ruby GC works, it's easier to store
      # references to enumerated objects here than from the C++ side.
      # by keeping a reference to returned objects in this object,
      # iterated objects that shouldn't be released (e.g Examples)
      # won't be until the data set is released.
      @objects = Hash.new {|hash, index| hash[index] = @klass.new(@container.send(@get_fn, index), @data_set)}
    end
    
    def [](index)
      return nil if (index < 0) || (index >= @size)
      @objects[index]
    end
    
    def each
      (0...@size).each do |index|
        yield @objects[index]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
thera-0.0.8 lib/quarry_rb/enumerable_helper.rb
thera-0.0.7 lib/quarry_rb/enumerable_helper.rb
thera-0.0.6 lib/quarry_rb/enumerable_helper.rb
thera-0.0.5 lib/quarry_rb/enumerable_helper.rb
thera-0.0.4 lib/quarry_rb/enumerable_helper.rb
thera-0.0.3 lib/quarry_rb/enumerable_helper.rb
thera-0.0.2 lib/quarry_rb/enumerable_helper.rb
thera-0.0.1 lib/quarry_rb/enumerable_helper.rb