Class: Stadiums

Inherits:
Object show all
Defined in:
lib/fifadat/tables/stadiums.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Stadiums

Returns a new instance of Stadiums.



5
6
7
# File 'lib/fifadat/tables/stadiums.rb', line 5

def initialize
   @recs = {}
end

Instance Method Details

#_add(new_rec) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fifadat/tables/stadiums.rb', line 22

def _add( new_rec )
   rec =  @recs[ new_rec.id ]
   if rec.nil?
       rec = new_rec
       @recs[ new_rec.id] = new_rec
   else
       ## assert attributes equal - why? why not?

      assert( new_rec == rec,
               "stadium records NOT matching - #{rec.pretty_inspect} != #{new_rec.pretty_inspect}")
   end
   rec
end

#add_matches(matches) ⇒ Object

rename to add_matches - why? why not?



9
10
11
12
13
14
15
16
17
18
# File 'lib/fifadat/tables/stadiums.rb', line 9

def add_matches( matches )  ## rename to add_matches - why? why not?

  matches.each_with_index do |m|
    stadium = Stadium.build( m['Stadium'] )
    rec = _add( stadium )

    ## update match counter by team

    team_name = desc(m['Home']['TeamName'])
    rec.count[ team_name ] += 1
  end
end

#as_json(id: false) ⇒ Object

note ignore remove id for now



65
66
67
# File 'lib/fifadat/tables/stadiums.rb', line 65

def as_json( id: false )  ## note ignore remove id for now

     @recs.values.as_json
end

#find!(h) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fifadat/tables/stadiums.rb', line 37

def find!( h )
   if h
     name  = desc( h['Name'] )
     city  = desc( h['CityName'] )
     ## auto-generate id

     ##  use slug of name plus id_city

     ## add city name

     ##  fix- add   0-9 & dash(-) to name too - why? why not?

     id  =       name.downcase.gsub( /[^a-z]/, '' )
     id += "_" + city.downcase.gsub( /[^a-z]/, '' )
   else
     puts "warn: empty stadium lookup:"
     pp h
     id = '<nil>'
   end

   ## check for nil - why? why not?

    rec = @recs[ id ]
    if rec.nil?
       puts "#{@recs.size} stadiums:"
       pp @recs.keys
       raise ArgumentError, "stadium not found using >#{id}< - #{h.pretty_inspect}"
    end
    rec
end

#size ⇒ Object



69
# File 'lib/fifadat/tables/stadiums.rb', line 69

def size() @recs.size; end