Programming Languages &Software 16 Jan 2008 05:19 am

Vis a tergo: Baseball cards as objects

Rob’s discussing object models for baseball cards. He is evolving a design bit-by-bit.

My first question is what use cases do you want for your database of baseball cards? Searching real stats for the season? Or just searching for cards? To me, that changes the way I’d lay it out.

To play along, here is a primitive set of plain old Ruby objects to model what he has so far. I thought it might be interesting to contrast the two.

OK, well, I lied, it is a little different, because I went ahead and subclassed for the variant card type.

class Card
  attr_reader :year, :number
  
  def initialize(number, year)
    @number = number
    @year = year
  end
end

class PlayerCard < Card
  attr_reader :player
  
  def initialize(number, year, player)
    super(number, year)
    @player = player    
  end
end

class BattingLeadersCard < Card
  attr_reader :top_50_batting_players # this is a list of players
  
  def initialize(number, year, top_50_batting_players)
    super(number, year)
    @top_50_batting_players = top_50_batting_players
  end

  def top_3
	top_50_batting_players[0..2]
  end
end


class Player
  attr_reader :name, :team
  
  def initialize(name, team)
    @name = name
    @team = team    
  end
end

3 Responses to “Vis a tergo: Baseball cards as objects”

  1. on 16 Jan 2008 at 10:37 am 1.Kevin said …

    Do you have a test for that?

  2. on 16 Jan 2008 at 2:01 pm 2.Bob said …

    Why yes, I do.

    Here they are, with the suite first, all concatenated. Note, these are only positive tests, not negative cases, because this is just for fun.

    $:.unshift File.join(File.dirname(__FILE__),'..','lib')
    
    require 'test/unit'
    require 'player_test'
    require 'card_test'
    require 'player_card_test'
    require 'batting_leaders_card_test'
    
    
    ##########
    require 'test/unit'
    require 'card'
    require 'date'
    
    class CardTest < Test::Unit::TestCase
      def test_ctor
        card = Card.new('1', Date.strptime("1968", '%Y').year)
        assert_equal('1', card.number)
        assert_equal(1968, card.year)
      end
    end
    
    require 'player'
    
    class PlayerTest < Test::Unit::TestCase
      def test_ctor
        player = Player.new("Jimbo Smith", "Orioles")
        assert_equal("Jimbo Smith", player.name)
        assert_equal("Orioles", player.team)
      end
    end
    
    require 'player_card'
    require 'date'
    
    class PlayerCardTest < Test::Unit::TestCase
      def test_ctor
        player = Player.new("Jimbo Smith", "Orioles")
        card = PlayerCard.new('1', Date.strptime("1968", '%Y').year, player)
        assert_equal('1', card.number)
        assert_equal(1968, card.year)
        assert_equal("Jimbo Smith", player.name)
        assert_equal("Orioles", player.team)
      end
    end
    
    require 'batting_leaders_card'
    require 'player'
    require 'date'
    
    class BattingLeadersCardTest < Test::Unit::TestCase
      def create_50_players
        players = []
        0.upto(49) { |index| players << Player.new("Jimbo Smith #{index}", "Orioles")}
        players
      end
      
      def test_top_3
        card = BattingLeadersCard.new('1', Date.strptime("1968", '%Y').year, create_50_players)
        assert_equal('1', card.number)
        assert_equal(1968, card.year)
        assert_equal(50, card.top_50_batting_players.size)
        top3 = card.top_3
        assert_equal(3, top3.size)
        top_1st, top_2nd, top_3rd = top3
        assert_equal("Jimbo Smith 0", top_1st.name)
        assert_equal("Orioles", top_1st.team)
        assert_equal("Jimbo Smith 1", top_2nd.name)
        assert_equal("Orioles", top_2nd.team)
        assert_equal("Jimbo Smith 2", top_3rd.name)
        assert_equal("Orioles", top_3rd.team)
      end
    end

  3. on 16 Jan 2008 at 2:01 pm 3.Bob said …

    aaarrrgghh it took away the indentation

Subscribe to the comments through RSS Feed

Leave a Reply