Class: ThinModels::LazyArray::MemoizedLength

Inherits:
ThinModels::LazyArray show all
Defined in:
lib/thin_models/lazy_array.rb

Overview

Memoizes the #length of the array, but does not at present memoize the results of each or slice_from_start_and_length.

#length will be memoized as a result of a direct call to #length (which uses an underlying #_length), or as a result of a full iteration via #each (which uses an underlying #_each)

Your extension points are now #_each, #_length and #slice_from_start_and_length

Direct Known Subclasses

Memoized

Instance Method Summary collapse

Methods inherited from ThinModels::LazyArray

#[], #empty?, #first, #initialize, #join, #last, #map, #size, #slice_from_start_and_length, #to_json

Constructor Details

This class inherits a constructor from ThinModels::LazyArray

Instance Method Details

#_lengthObject



152
# File 'lib/thin_models/lazy_array.rb', line 152

alias :_length :length

#eachObject



145
146
147
148
149
150
# File 'lib/thin_models/lazy_array.rb', line 145

def each
  length = 0
  _each {|item| yield item; length += 1}
  @length = length
  self
end

#inspectObject



157
158
159
160
161
162
163
# File 'lib/thin_models/lazy_array.rb', line 157

def inspect
  if @length
    "[ThinModels::LazyArray(length=#{@length}):...]"
  else
    "[ThinModels::LazyArray:...]"
  end
end

#lengthObject



153
154
155
# File 'lib/thin_models/lazy_array.rb', line 153

def length
  @length ||= _length
end