Class: Teams

Inherits:
Object
  • Object
show all
Defined in:
lib/footballdata/convert-teams.rb

Instance Method Summary collapse

Constructor Details

#initializeTeams

Returns a new instance of Teams.



85
86
87
88
# File 'lib/footballdata/convert-teams.rb', line 85

def initialize
   @recs    = []
   @by_name = {}
end

Instance Method Details

#add(recs) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/footballdata/convert-teams.rb', line 91

def add( recs )
    recs.each do |h|
         rec = build_team( h )
         @recs << rec

         ## note - assert/make sure team name is uniq
         if @by_name.has_key?(rec[:name])
           raise ArgumentError, "duplicate team name - #{h.inspect}"
         else
           @by_name[rec[:name]] = rec
         end

         if rec[:name] != rec[:short_name]
           if @by_name.has_key?(rec[:short_name])
              raise ArgumentError, "duplicate team (short) name - #{h.inspect}"
           else
             @by_name[rec[:short_name]] = rec
           end
         end
    end
end

#add_matches(matches) ⇒ Object

use/rename to add_matches - why? why not?



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/footballdata/convert-teams.rb', line 125

def add_matches( matches )  ## use/rename to add_matches - why? why not?
   matches.each do |m|

     ## note - skip teams without name (e.g. N.N.)
     if m['homeTeam']['name']
       rec = find_by!( name: m['homeTeam']['name'] )
       rec[:count] += 1
     end

     if m['awayTeam']['name']
       rec = find_by!( name: m['awayTeam']['name'] )
       rec[:count] += 1
     end
   end
end

#as_json(id: false) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/footballdata/convert-teams.rb', line 145

def as_json( id: false )
   if id
     @recs
   else
     @recs.map { |rec| rec.except(:id ) }
   end
end

#each(&blk) ⇒ Object



141
# File 'lib/footballdata/convert-teams.rb', line 141

def each( &blk) @recs.each( &blk); end

#find_by!(name:) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/footballdata/convert-teams.rb', line 115

def find_by!( name: )
     rec = @by_name[ name ]
     if rec.nil?
       raise ArgumentError, "team >#{name}< not found; sorry"
     end

     rec
end

#sizeObject



153
# File 'lib/footballdata/convert-teams.rb', line 153

def size() @recs.size; end