Permalink
Browse files

add game cursor

  • Loading branch information...
1 parent ca52bca commit 57205bcb603274957de2afeb04c2872aed800c07 @geraldb geraldb committed Oct 27, 2013
Showing with 125 additions and 1 deletion.
  1. +5 −1 lib/sportdb.rb
  2. +3 −0 lib/sportdb/console.rb
  3. +73 −0 lib/sportdb/models/utils.rb
  4. +44 −0 test/test_date.rb
View
@@ -53,6 +53,7 @@
require 'sportdb/models/season'
require 'sportdb/models/team'
require 'sportdb/models/track'
+require 'sportdb/models/utils' # e.g. GameCursor
require 'sportdb/schema' # NB: requires sportdb/models (include SportDB::Models)
require 'sportdb/utils'
require 'sportdb/reader'
@@ -79,7 +80,7 @@
module SportDb
def self.banner
- "sportdb #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
+ "sportdb/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end
def self.root
@@ -96,6 +97,9 @@ def self.data_path
def self.lang
# todo/fix: find a better way for single instance ??
+ # will get us ruby warning: instance variable @lang not initialized => find a better way!!!
+ # just use @lang w/o .nil? e.g.
+ # @lang =|| Lang.new why? why not?? or better use @@lang =|| Lang.new for class variable!!!
if @lang.nil?
@lang = Lang.new
end
View
@@ -14,6 +14,9 @@
## shortcuts for models
+##
+## todo/fix: just use include SportDb::Models - why? why not? is it possible/working?
+
Badge = SportDb::Models::Badge
Event = SportDb::Models::Event
Game = SportDb::Models::Game
@@ -0,0 +1,73 @@
+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
+ end
+
+ 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 )
+ 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
View
@@ -0,0 +1,44 @@
+# encoding: utf-8
+
+require 'helper'
+
+class TestDate < MiniTest::Unit::TestCase
+
+ def test_date
+ assert_datetime DateTime.new( 2013, 1, 19, 22 ), parse_date( '19.01.2013 22.00' )
+ assert_datetime DateTime.new( 2013, 1, 21, 21, 30 ), parse_date( '21.01.2013 21.30' )
+
+ assert_date DateTime.new( 2013, 1, 26 ), parse_date( '26.01.2013' )
+ end
+
+private
+
+ ## todo: check if assert_datetime or assert_date exist already? what is the best practice to check dates ???
+ def assert_date( exp, act )
+ assert_equal exp.year, act.year
+ assert_equal exp.month, act.month
+ assert_equal exp.day, act.day
+ end
+
+ def assert_time( exp, act )
+ assert_equal exp.hour, act.hour
+ assert_equal exp.min, act.min
+ end
+
+ def assert_datetime( exp, act )
+ assert_date( exp, act )
+ assert_time( exp, act )
+ end
+
+ class Reader
+ include LogUtils::Logging # add logger
+ include SportDb::FixtureHelpers
+ end
+
+ def parse_date( line, opts={} )
+ # e.g. lets you pass in opts[:start_at] ???
+ reader = Reader.new
+ reader.find_date!( line, opts )
+ end
+
+end # class TestScores

0 comments on commit 57205bc

Please sign in to comment.