Class: Document

Inherits:
Object
  • Object
show all
Defined in:
lib/fbtxt-pp/models/document.rb

Overview

note: document is container for LeagueSeason holding teams, matches, etc.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Document

Returns a new instance of Document.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fbtxt-pp/models/document.rb', line 13

def initialize( data )
    @data = data

    @teams = Teams.new
    @teams.add( data['teams'] )
    ## puts "  #{teams.size} team(s) in season #{season}"

    @stadiums = Stadiums.new
    @stadiums.add( data['stadiums'] )
    ## puts "  #{stadiums.size} stadium(s) in season #{season}"


   ## read in stages for sorting
   ##   incl.  SequenceOrder, StageLevel (optional)
   ## stages = Stages.new
   ## stages.add( read_json( "./#{slug}/misc/#{season}_stages.json" )['Results'] )

    ## automagically sort (by groups) - why? why not?
    @data['matches'] = _sort_matches( @data['matches'] )
end

Class Method Details

.read(path) ⇒ Object



8
9
10
11
# File 'lib/fbtxt-pp/models/document.rb', line 8

def self.read( path )
     data = read_json( path )
     new( data )
end

Instance Method Details

#_sort_matches(matches) ⇒ Object

(private) helpers



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fbtxt-pp/models/document.rb', line 87

def _sort_matches( matches )
  ###
  ##   sort results by group if present

  ## add "old" sort index
  matches = matches.each_with_index.map {|m,i| m['sort']=i+1; m }


  matches =  matches.sort do |l,r|

   lhs_stage =  l['stage']
   rhs_stage =  r['stage']

   ## lhs_stage = stages.find!( lhs_stageName )
   ## rhs_stage = stages.find!( rhs_stageName )

   lhs_group  = l['group']   # optional
   rhs_group  = r['group']   # optional

   lhs_matchday  = l['matchday']   # optional
   rhs_matchday  = r['matchday']   # optional

   if (lhs_group && rhs_group) && (lhs_stage == rhs_stage)
       res = lhs_group <=> rhs_group
       res = (lhs_matchday && rhs_matchday ?
              lhs_matchday <=> rhs_matchday :  0 )  if res == 0
       ## same group; sort by old index (or) date??
       res = l['sort'] <=> r['sort']   if res == 0
       res
   else
       ### sort first by stage (seq) and than keep as is
       ### res = lhs_stage[:seq] <=> rhs_stage[:seq]
       ## res = l['sort'] <=> r['sort']    if res == 0
       ## res
       l['sort'] <=> r['sort']
   end
  end

   matches
end

#calc_start_end_datesObject

(was: collect_dates) find a better name e.g. min_max_dates / duration / collect/find_start_end_dates?



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fbtxt-pp/models/document.rb', line 59

def calc_start_end_dates
  start_date = nil
  end_date   = nil

  @data['matches'].each do |_m|

     ## date_utc       = parse_date_utc(   _m['datetime_utc'] )
     date_local     = parse_date_local( _m['datetime_local'] )

     ## note - alway use local datetime for now

     if start_date.nil? || date_local  < start_date
        start_date = date_local
      end

     if end_date.nil? || date_local > end_date
        end_date = date_local
     end
  end

  [start_date, end_date]
end

#each_match(&blk) ⇒ Object Also known as: each



47
48
49
50
51
52
53
# File 'lib/fbtxt-pp/models/document.rb', line 47

def each_match( &blk )
   @data['matches'].each do |_m|
      ## use ("typed" struct) wrapper
      m = Match.new( self, _m )
      blk.call( m )
   end
end

#find_stadium!(h) ⇒ Object



37
# File 'lib/fbtxt-pp/models/document.rb', line 37

def find_stadium!( h ) @stadiums.find!( h );  end

#find_team!(q) ⇒ Object



35
# File 'lib/fbtxt-pp/models/document.rb', line 35

def find_team!( q ) @teams.find!( q );  end

#matchesObject



44
# File 'lib/fbtxt-pp/models/document.rb', line 44

def matches()   @data['matches']; end

#stadiumsObject



43
# File 'lib/fbtxt-pp/models/document.rb', line 43

def stadiums()  @stadiums; end

#teamsObject

quick ("unsafe/low-level") access for size - keep? why? why not? use team_count, stadium_count, match_count - why? why not?



42
# File 'lib/fbtxt-pp/models/document.rb', line 42

def teams()     @teams; end