Class: Sub

Inherits:
Object show all
Defined in:
lib/fifadat/models/substitution.rb

Overview

Substitution/SubOnOff Event

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(off:, on:, minute:) ⇒ Sub

Returns a new instance of Sub.



109
110
111
112
113
# File 'lib/fifadat/models/substitution.rb', line 109

def initialize( off:, on:, minute: )
  @off    = off
  @on     = on
  @minute = minute
end

Instance Attribute Details

#minute ⇒ Object (readonly)

Returns the value of attribute minute.



107
108
109
# File 'lib/fifadat/models/substitution.rb', line 107

def minute
  @minute
end

#off ⇒ Object (readonly)

Returns the value of attribute off.



107
108
109
# File 'lib/fifadat/models/substitution.rb', line 107

def off
  @off
end

#on ⇒ Object (readonly)

Returns the value of attribute on.



107
108
109
# File 'lib/fifadat/models/substitution.rb', line 107

def on
  @on
end

Class Method Details

._build_ary(recs, players:) ⇒ Object



100
101
102
103
# File 'lib/fifadat/models/substitution.rb', line 100

def self._build_ary( recs, players: )
    recs = recs.map  { |h| build( h, players: players ) }
    recs
end

._build_sub_minute(h) ⇒ Object



39
40
41
42
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
69
70
71
72
# File 'lib/fifadat/models/substitution.rb', line 39

def self._build_sub_minute( h )

    ## split into minute
    ##  and offset (stoppage/injury/added time)
    ##  e.g. 90'+11'

     minute_str = h['Minute']

     ##
     ## check what is "Period"=>4
     ##   minute is "" empty!!

          if minute_str.nil? || minute_str.empty?
            if h['Period'] == 4  ## quick fix - use 46' half-time sub??
                minute_str = "46'"
            elsif h['Period'] == 8 ## quick fix - use 116' 1st half-time extra time?
                minute_str = "116'"
            elsif h['Period'] == 17  ## quick fix- what is period 17 beyond pens??
                minute_str = "121'"
            else
              puts "!! minute in sub is nil or empty:"
              pp h
              exit 1
            end
          end

     ##  _fmt_minute( *_parse_minute( minute_str ))

     minute, offset  = _parse_minute( minute_str )

     Minute.new( m:      minute,
                 offset: offset,
                 period: h['Period'] )
end

.build(h, players:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fifadat/models/substitution.rb', line 75

def self.build( h, players: )

      minute = _build_sub_minute( h )

      playerOff = players.find( h['IdPlayerOff'] )
      playerOn  = players.find( h['IdPlayerOn'] )

       ## fix-fix-fix
       ##  report warning if playerON nil!!!
       ## use N.N.  for  nil - why? why not?
       playerOn = Player::PLAYER_NIL      if playerOn.nil?

      assert( playerOff && playerOn,
                  "playerOff and/or playerOn is NIL in #{h.pretty_inspect}")

      rec = { off:     playerOff,
              on:      playerOn,
              minute:  minute
            }

      new( **rec )
end

Instance Method Details

#as_json ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/fifadat/models/substitution.rb', line 115

def as_json(*)
   h = {
         'off'     => off.name,
         'on'      => on.name,
         'minute'   => minute
       }
   h
end