Class: Fbtxt::Model::Match

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Match

Returns a new instance of Match.



38
39
40
# File 'lib/fbtxt/document/models/match.rb', line 38

def initialize( **kwargs )
  update( **kwargs )  unless kwargs.empty?
end

Instance Attribute Details

#attObject (readonly)

Returns the value of attribute att.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def att
  @att
end

#bookingsObject

use cards - why? why not?



31
32
33
# File 'lib/fbtxt/document/models/match.rb', line 31

def bookings
  @bookings
end

#dateObject (readonly)

Returns the value of attribute date.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def date
  @date
end

#goalsObject

e.g. support (flat/undefined) [], or (nested/paired) [[],[]]



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

def goals
  @goals
end

#groundObject (readonly)

Returns the value of attribute ground.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def ground
  @ground
end

#groupObject (readonly)

Returns the value of attribute group.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def group
  @group
end

#lineupObject

add details match (report) info lineup,bench,subs,bookings(y/yr/r/etc.)



30
31
32
# File 'lib/fbtxt/document/models/match.rb', line 30

def lineup
  @lineup
end

#numObject (readonly)

Returns the value of attribute num.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def num
  @num
end

#penaltiesObject

Returns the value of attribute penalties.



35
36
37
# File 'lib/fbtxt/document/models/match.rb', line 35

def penalties
  @penalties
end

#refereesObject

Returns the value of attribute referees.



33
34
35
# File 'lib/fbtxt/document/models/match.rb', line 33

def referees
  @referees
end

#roundObject (readonly)

Returns the value of attribute round.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def round
  @round
end

#scoreObject (readonly)

Returns the value of attribute score.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def score
  @score
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def status
  @status
end

#team1Object (readonly)

Returns the value of attribute team1.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def team1
  @team1
end

#team2Object (readonly)

Returns the value of attribute team2.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def team2
  @team2
end

#timeObject (readonly)

Returns the value of attribute time.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def time
  @time
end

#time_localObject (readonly)

Returns the value of attribute time_local.



11
12
13
# File 'lib/fbtxt/document/models/match.rb', line 11

def time_local
  @time_local
end

Instance Method Details

#as_jsonObject



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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/fbtxt/document/models/match.rb', line 72

def as_json
  #####
  ##  note - use string keys (NOT symbol for data keys)
  ##            for easier json compatibility
  data = {}

  ## check round
  if @round
    data['round'] = if round.is_a?( Integer )
                      "Matchday #{@round}"
                    else ## assume string
                      @round
                    end
  end


  data['num'] = @num    if @num


  if @date
    ## assume 2020-09-19 date format!!
    data['date']  = @date.is_a?( String ) ? @date : @date.strftime('%Y-%m-%d')

    data['time']        = @time           if @time
    data['time_local']  = @time_local     if @time_local
  end


  data['team1'] =  @team1.is_a?( String ) ? @team1 : @team1.name

  ## note - for match status bye  team2 is nil!!!
  ##           e.g.     Queen's Park       bye
  ##                    Wanderers          bye
  ##   todo/check - keep bye as a match - why? why not?
  ##                      has no date/time & venue & score etc.
  if @team2
    data['team2'] =  @team2.is_a?( String ) ? @team2 : @team2.name
  end

  ## note - score might be
  ##           1) array e.g. [0,1]   -- e.g. uses reported key internally
  ##           2) hash  e.g. { 'ft' [0,1] } etc.
  ##  note - w/o (walkout)  do NOT add empty score

   ## note:
   ##   use/assume full-time (ft) if reported - why? why not?
   ##     in future check for score note or such
   ##      to  use "plain" array or such - why? why not?

  data['score'] = @score.as_json      if @score



  ### check for goals
  if @goals.is_a?(Array) && @goals.size > 0
    data['goals1'] = []
    data['goals2'] = []

    @goals.each do |goal|
         if goal.team == 1
            data['goals1']  << goal.as_json
          else  ## assume 2  (or better raise except if NOT 2)
            data['goals2']  << goal.as_json
          end
     end  # each goal
  end

  ### check for penalties (shootout)
  if @penalties.is_a?(Array) && @penalties.size > 0
     data['penalties'] = @penalties.as_json
  end



  data['status'] = @status  if @status

  data['group']  = @group   if @group

  if @ground
       ## note: might be array of string e.g. ['Wembley', 'London']
       ##
       ##  todo/check - auto-join to string - why? why not?
       ##                   e.g.  ['Wembley', 'London']
       ##                     to   'Wembley, London'
       ##   note - auto-join geo tree for now
       data['ground'] = @ground.join(', ')
  end

  data['attendance'] = @att   if @att


###
#  check for match (report) details
#    quick & dirty first try/version/dump
#      refine (serialize) format later
    if @lineup.is_a?(Array) && @lineup.size > 0
        data['lineup'] = @lineup.as_json
    end


    if @bookings.is_a?(Array) && @bookings.size > 0
       if @bookings[0].empty? && @bookings[1].empty?
          ##  skip empty bookings
          ## "bookings": [[], []]
       else
         data['bookings'] = @bookings.as_json
       end
    end


    if @referees.is_a?(Array) && @referees.size > 0
        data['referees'] = @referees.as_json
    end


  data
end

#update(**kwargs) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fbtxt/document/models/match.rb', line 43

def update( **kwargs )
  @num      = kwargs[:num]     if kwargs.has_key?( :num )

  ## note: check with has_key?  because value might be nil!!!
  @date       = kwargs[:date]        if kwargs.has_key?( :date )
  @time       = kwargs[:time]        if kwargs.has_key?( :time )
  @time_local = kwargs[:time_local]  if kwargs.has_key?( :time_local )

  ## todo/fix: use team1_name, team2_name or similar - for compat with db activerecord version? why? why not?
  @team1    = kwargs[:team1]    if kwargs.has_key?( :team1 )
  @team2    = kwargs[:team2]    if kwargs.has_key?( :team2 )

  ## note: round is a string!!!  e.g. '1', '2' for matchday or 'Final', 'Semi-final', etc.
  ##   todo: use to_s - why? why not?
  @round    = kwargs[:round]    if kwargs.has_key?( :round )
  @group    = kwargs[:group]    if kwargs.has_key?( :group )
  @status   = kwargs[:status]   if kwargs.has_key?( :status )

  @ground   = kwargs[:ground]   if kwargs.has_key?( :ground )
  @att      = kwargs[:att]      if kwargs.has_key?( :att )

  ## check/expects all-in-one score struct for convenience!!!
  @score    = kwargs[:score]     if kwargs.has_key?( :score )

  self   ## note - MUST return self for chaining
end