class FortuneTeller::MomentStructReader

An enumerator-like reader for efficient ordered reading

Public Class Methods

new(struct) click to toggle source
# File lib/fortuneteller/moment_struct.rb, line 61
def initialize(struct)
  @struct = struct
  @struct_ct = @struct.future.length
  @date = nil
  @next_date = (@struct.future.empty? ? nil : @struct.future[0][:date])
  @i = -1
  @object = @struct.start
end

Public Instance Methods

read(date) click to toggle source
# File lib/fortuneteller/moment_struct.rb, line 70
def read(date)
  validate_read(date)
  @date = date
  return @object if @next_date.nil? || (date < @next_date)
  ((@i + 1)..(@struct_ct - 1)).each do |i|
    next_obj = @struct.future[i + 1]
    next unless next_obj.nil? || (next_obj[:date] > @date)
    @next_date = (next_obj.nil? ? nil : next_obj[:date])
    @i = i
    return @object = @struct.future[i][:struct]
  end
end

Private Instance Methods

validate_read(date) click to toggle source
# File lib/fortuneteller/moment_struct.rb, line 85
def validate_read(date)
  throw 'Reading backwards' if !@date.nil? && (date < @date)
end