Class: Fbtxt::Document

Inherits:
Object
  • Object
show all
Includes:
Debuggable
Defined in:
lib/fbtxt/document/quick_match_reader.rb

Constant Summary collapse

SEASON_IN_HEADING_RE =

try to find season in heading e.g. Ă–sterr. Bundesliga 2015/16 or 2015-16 World Cup 2018 or 2018 World Cup etc.

%r{
    \b
(?<season>
    \d{4}
   (?: [\/-]
       \d{1,4} )?       ## optional 2nd year in season
  )
    \b
}x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt, start: nil) ⇒ Document

keep/store a reference to (source) txt - why? why not?



32
33
34
# File 'lib/fbtxt/document/quick_match_reader.rb', line 32

def initialize( txt, start: nil )
  _parse( txt, start: start )
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



27
28
29
# File 'lib/fbtxt/document/quick_match_reader.rb', line 27

def errors
  @errors
end

#groupsObject (readonly)

Returns the value of attribute groups.



24
25
26
# File 'lib/fbtxt/document/quick_match_reader.rb', line 24

def groups
  @groups
end

#matchesObject (readonly)

Returns the value of attribute matches.



21
22
23
# File 'lib/fbtxt/document/quick_match_reader.rb', line 21

def matches
  @matches
end

#roundsObject (readonly)

Returns the value of attribute rounds.



25
26
27
# File 'lib/fbtxt/document/quick_match_reader.rb', line 25

def rounds
  @rounds
end

#teamsObject (readonly)

Returns the value of attribute teams.



23
24
25
# File 'lib/fbtxt/document/quick_match_reader.rb', line 23

def teams
  @teams
end

#titleObject (readonly)

e.g. league name (England | Premier League 2026/27) or such



20
21
22
# File 'lib/fbtxt/document/quick_match_reader.rb', line 20

def title
  @title
end

Class Method Details

.parse(txt) ⇒ Object



13
14
15
# File 'lib/fbtxt/document/quick_match_reader.rb', line 13

def self.parse( txt )
    new( txt )
end

.read(path) ⇒ Object



7
8
9
10
11
# File 'lib/fbtxt/document/quick_match_reader.rb', line 7

def self.read( path )
  ##  note - use read_text from cocos - why? why not?
  txt = read_text( path )
  parse( txt )
end

Instance Method Details

#_parse(txt, start: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
127
128
129
130
131
132
133
134
# File 'lib/fbtxt/document/quick_match_reader.rb', line 52

def _parse( txt, start: nil )
    ## note: every (new) read call - resets errors list to empty
    @title   = nil     ## or use '? or use '' - why? why not?
    @matches = []

    @teams   = []
    @groups  = []
    @rounds  = []

    @errors  = []


    parser = Parser.new( txt )   ## use own parser instance (not shared) - why? why not?
    tree, errors = parser.parse_with_errors

    @errors = errors

## note - source file MUST always start with heading 1 for now

##
## !! (QUICK) PARSE ERROR - source MUST start with Heading1; got 34 nodes:
## [<BlankLine>,
##  <BlankLine>,
## <Heading1 World Cup 1930>,

      ## remove leading BlankLines if any!!
      while tree[0].is_a? RaccMatchParser::BlankLine
          tree.shift  ## remove (leading) blank line from parse tree
      end


    ## try to get league and season
    if tree[0].is_a? RaccMatchParser::Heading1
         @title = tree[0].text

         if (m = SEASON_IN_HEADING_RE.match( @title ))
           season = Season.parse( m[:season] )   ## convert (str) to season obj!!!
           start = if season.year?
                     Date.new( season.start_year, 1, 1 )
                   else
                     Date.new( season.start_year, 7, 1 )
                   end
         else
           puts "!! (DOCUMENT) WARN - no season found in Heading1 >#{@title}<"
         end
    else
        ### report error - source MUST start with heading 1
       puts "!! (DOCUMENT) WARN - source SHOULD start with Heading1; got #{tree.size} nodes:"
       pp tree
    end



    ############
    ### "walk" tree to get structs (matches/teams/etc.)
    conv = MatchTree.new( tree, start: start )

    teams, matches, rounds, groups = conv.convert


      ## auto-add "upstream" errors from parser
      ## @errors += parser.errors  if parser.errors?

      if debug?
        puts ">>> #{teams.size} teams:"
        pp teams
        puts ">>> #{groups.size} groups:"
        pp groups
        puts ">>> #{rounds.size} rounds:"
        pp rounds
        puts ">>> #{matches.size} matches:"
        ## pp matches
      end


      @matches = matches

      @teams    = teams
      @groups   = groups
      @rounds   = rounds

      self
end

#errors?Boolean

Returns:

  • (Boolean)


28
# File 'lib/fbtxt/document/quick_match_reader.rb', line 28

def errors?() @errors.size > 0; end