Class: Footty::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/footty/dataset.rb

Direct Known Subclasses

OpenfootballDataset

Instance Method Summary collapse

Instance Method Details

#end_dateObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/footty/dataset.rb', line 16

def end_date
   @end_date ||= begin
                    end_date = nil
                    matches.each do |match|
                       date = Date.strptime(match['date'], '%Y-%m-%d' )
                       end_date = date  if end_date.nil? ||
                                           date > end_date
                    end
                    end_date
                 end
end

#league_nameObject

Raises:

  • (ArgumentError)


11
12
13
# File 'lib/footty/dataset.rb', line 11

def league_name
  raise ArgumentError, "method league_name must be implemented by concrete class!!"
end

#matchesObject

Raises:

  • (ArgumentError)


7
8
9
# File 'lib/footty/dataset.rb', line 7

def matches
   raise ArgumentError, "method matches must be implemented by concrete class!!"
end

#matches_for(date) ⇒ Object



44
45
46
47
48
49
# File 'lib/footty/dataset.rb', line 44

def matches_for( date )
  matches = select_matches do |match| 
                              date == Date.parse( match['date'] ) 
                           end
  matches
end

#matches_within(start_date, end_date) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/footty/dataset.rb', line 54

def matches_within( start_date, end_date )
  matches = select_matches do |match| 
                             date = Date.parse( match['date'] )
                             date >= start_date && date <= end_date  
                           end
  matches
end

#past_matches(date: Date.today) ⇒ Object



92
93
94
95
96
97
# File 'lib/footty/dataset.rb', line 92

def past_matches( date: Date.today )
  matches = select_matches { |match| date > Date.parse( match['date'] ) }
  ## note reveserve matches (chronological order/last first)
  ## matches.reverse
  matches
end

#query(q) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/footty/dataset.rb', line 63

def query( q )
    ## query/check for team name match for now
    rx =  /#{Regexp.escape(q)}/i   ## use case-insensitive regex match
    
    matches = select_matches do |match|
                      if rx.match( match['team1'] ) ||
                         rx.match( match['team2'] )  
                         true
                      else
                         false
                      end
              end
    matches 
end

#start_dateObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/footty/dataset.rb', line 28

def start_date
   @start_date ||= begin
                    start_date = nil
                    matches.each do |match|
                       date = Date.strptime(match['date'], '%Y-%m-%d' )
                       start_date = date  if start_date.nil? ||
                                           date < start_date
                    end
                    start_date
                   end
end

#todays_matches(date: Date.today) ⇒ Object



40
# File 'lib/footty/dataset.rb', line 40

def todays_matches( date: Date.today )      matches_for( date ); end

#tomorrows_matches(date: Date.today) ⇒ Object



41
# File 'lib/footty/dataset.rb', line 41

def tomorrows_matches( date: Date.today )   matches_for( date+1 );  end

#upcoming_matches(date: Date.today, limit: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/footty/dataset.rb', line 80

def upcoming_matches( date: Date.today,
                      limit: nil )
  ## note: includes todays matches for now
  matches = select_matches { |match| date <= Date.parse( match['date'] ) }

  if limit
    matches[0, limit]  ## cut-off
  else
    matches
  end
end

#weeks_matches(week_start, week_end) ⇒ Object



52
# File 'lib/footty/dataset.rb', line 52

def weeks_matches( week_start, week_end )  matches_within( week_start, week_end);  end

#yesterdays_matches(date: Date.today) ⇒ Object



42
# File 'lib/footty/dataset.rb', line 42

def yesterdays_matches( date: Date.today )  matches_for( date-1 );  end