Sha256: dc6706fb953446d9230bb8bc7e50aa804f1539a01834767b894f19db0a6422a2

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

module SportDb
  module Models


class GameCursor

  def initialize( games )
    @games = games
  end

  def each
    state = GameCursorState.new 

    @games.each do |game|
      state.next( game )
      yield( game, state )   # e.g. lets you use state.new_date?  or state.new_week? or state.new_year? etc.
    end
  end # method each
end # class GameCursor


class GameCursorState

  def initialize
    @last_play_at  = DateTime.new( 1971, 1, 1 )
    @new_date      = true
    @new_year      = true
    @new_week      = true
    @index         = -1   # zero-based index; thus start off with -1 (e.g. -1+=1 => 0)
  end

  attr_reader :index

  def new_date?()  @new_date; end
  def new_year?()  @new_year; end
  def new_week?()  @new_week; end
  

  ## add new league ?
  ## add new round  ?
  ## add new time   ?
    
  def next( game )
    @index += 1   # zero-based index; start off with -1 (undefined/uninitialized)
    game_play_at = game.play_at  # cache play_at value ref

    if @last_play_at.year   == game_play_at.year  &&
       @last_play_at.month  == game_play_at.month &&
       @last_play_at.day    == game_play_at.day
      @new_date = false
    else
      @new_date = true
        
      # check for new_year
      if @last_play_at.year == game_play_at.year
        @new_year = false
      else
        @new_year = true
      end
        
      # check for new_week
      # -- todo: find a method for week number; do NOT use strftime; there must be something easier
      # -- check if activesupport adds  .week or similar ??? use it if it exists
      if @last_play_at.strftime('%V') == game_play_at.strftime('%V')
        @new_week = false
      else
        @new_week = true
      end
    end

    @last_play_at = game.play_at
  end # method next
    
end # class GameCursorState


  end # module Models
end # module SportDb

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sportdb-1.8.0 lib/sportdb/models/utils.rb
sportdb-1.7.9 lib/sportdb/models/utils.rb
sportdb-1.7.8 lib/sportdb/models/utils.rb
sportdb-1.7.7 lib/sportdb/models/utils.rb