lib/sportdb/models/utils.rb in sportdb-models-1.19.1 vs lib/sportdb/models/utils.rb in sportdb-models-2.0.0

- old
+ new

@@ -1,30 +1,30 @@ module SportDb module Model -class GameCursor +class MatchCursor - def initialize( games ) - @games = games + def initialize( matches ) + @matches = matches end def each - state = GameCursorState.new + state = MatchCursorState.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. + @matches.each do |match| + state.next( match ) + yield( match, 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 +end # class MatchCursor -class GameCursorState +class MatchCursorState def initialize - @last_play_at = DateTime.new( 1971, 1, 1 ) + @last_date = 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 @@ -32,47 +32,47 @@ 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 ) + + def next( match ) @index += 1 # zero-based index; start off with -1 (undefined/uninitialized) - game_play_at = game.play_at # cache play_at value ref + match_date = match.date # cache date 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 + if @last_date.year == match_date.year && + @last_date.month == match_date.month && + @last_date.day == match_date.day @new_date = false else @new_date = true - + # check for new_year - if @last_play_at.year == game_play_at.year + if @last_date.year == match_date.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') + if @last_date.strftime('%V') == match_date.strftime('%V') @new_week = false else @new_week = true end end - @last_play_at = game.play_at + @last_date = match.date end # method next - -end # class GameCursorState + +end # class MatchCursorState end # module Model end # module SportDb