Class: Score

Inherits:
Object
  • Object
show all
Defined in:
lib/score-formats.rb,
lib/score-formats/score.rb

Overview

note: make Score top-level and use like Date - yes, yes, yes - why? why not?

Constant Summary collapse

SCORE_SPLIT_RE =
%r{^  [ ]*
                        ([0-9]+)
[ ]*
[:x–-]      ## note: allow some unicode dashes too
[ ]*
                        ([0-9]+)
[ ]*  $}xi

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ Score

Returns a new instance of Score.



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
# File 'lib/score-formats/score.rb', line 67

def initialize( *values )
  ## note: for now always assumes integers
  ##  todo/check - check/require integer args - why? why not?

  ### todo/fix: add more init options
  ##   allow kwargs (keyword args) via hash - why? why not?
  ##     use kwargs for "perfect" init where you can only set the half time (ht) score
  ##          or only the penalty or other "edge" cases
  ##   allow int pairs e.g. [1,2], [2,2]
  ##   allow values array MUST be of size 8 (or 4 or 6) - why? why not?

  raise ArgumentError, "expected even integer number (pairs), but got #{values.size}"   if values.size % 2 == 1

  if values.size == 2
    @score1   = values[0]    # full time (ft) score
    @score2   = values[1]

    @score1i  = @score2i  = nil
    @score1et = @score2et = nil
    @score1p  = @score2p  = nil
  else
    @score1i  = values[0]    # half time (ht) score
    @score2i  = values[1]

    @score1   = values[2]    # full time (ft) score
    @score2   = values[3]

    @score1et = values[4]    # extra time (et) score
    @score2et = values[5]

    @score1p  = values[6]    # penalty (p) score
    @score2p  = values[7]
  end
end

Instance Attribute Details

#score1Object (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score1
  @score1
end

#score1etObject (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score1et
  @score1et
end

#score1iObject (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score1i
  @score1i
end

#score1pObject (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score1p
  @score1p
end

#score2Object (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score2
  @score2
end

#score2etObject (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score2et
  @score2et
end

#score2iObject (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score2i
  @score2i
end

#score2pObject (readonly)

half time (ht) score



43
44
45
# File 'lib/score-formats/score.rb', line 43

def score2p
  @score2p
end

Class Method Details

.find!(line, lang: ScoreFormats.lang) ⇒ Object



65
66
67
# File 'lib/score-formats.rb', line 65

def self.find!( line, lang: ScoreFormats.lang )
  ScoreFormats.find!( line, lang: lang )
end

.parse(line, lang: ScoreFormats.lang) ⇒ Object



61
62
63
# File 'lib/score-formats.rb', line 61

def self.parse( line, lang: ScoreFormats.lang )
  ScoreFormats.parse( line, lang: lang )
end

.split(str) ⇒ Object

note: return array of two integers or empty array



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/score-formats/score.rb', line 14

def self.split( str )   ## note: return array of two integers or empty array
  ## e.g. allow/support
  ##      1-1 or 1 - 1      - "english" style
  ##      1:1               - "german / deutsch" style
  ##      1x1 1X1           - "brazil/portugese" style

  ## note: add unicode "fancy" dash too (e.g. –)
  ## add some more - why? why not?

  if m=SCORE_SPLIT_RE.match(str)
    [m[1].to_i, m[2].to_i]
  else
    # no match - warn if str is not empty? why? why not?
    ##
    ## todo/fix:
    ## do NOT warn on
    ##    assert_equal [], Score.split( '-' )
    ##    assert_equal [], Score.split( '-:-' )
    ##    assert_equal [], Score.split( '?' )
    ## for now - add more?

    puts "!! WARN - cannot match (split) score format >#{str}<"  unless str.empty?
    []
  end
end

Instance Method Details

#etObject

e.g. 90+15mins



53
# File 'lib/score-formats/score.rb', line 53

def et()  [@score1et, @score2et]; end

#et?Boolean

Returns:

  • (Boolean)


61
# File 'lib/score-formats/score.rb', line 61

def et?()  @score1et && @score2et; end

#ftObject

alternate accessor via array e.g. ft and ft



51
# File 'lib/score-formats/score.rb', line 51

def ft()  [@score1,   @score2];   end

#ft?Boolean

todo/check: allow one part missing why? why not?

e.g.  1-nil  or nil-1  - why? why not?

Returns:

  • (Boolean)


59
# File 'lib/score-formats/score.rb', line 59

def ft?()  @score1   && @score2;   end

#htObject

e.g. 45 mins



52
# File 'lib/score-formats/score.rb', line 52

def ht()  [@score1i,  @score2i];  end

#ht?Boolean

Returns:

  • (Boolean)


60
# File 'lib/score-formats/score.rb', line 60

def ht?()  @score1i  && @score2i;  end

#pObject Also known as: pen

e.g. note - starts “fresh” score from 0-0



54
# File 'lib/score-formats/score.rb', line 54

def p()   [@score1p,  @score2p];  end

#p?Boolean Also known as: pen?

Returns:

  • (Boolean)


62
# File 'lib/score-formats/score.rb', line 62

def p?()   @score1p  && @score2p;  end

#to_aObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/score-formats/score.rb', line 145

def to_a
  ## pairs with values
  pairs = []
  ## note: allow 1-nil, nil-1 for now in pairs (or use && and NOT ||) - why? why not?
  pairs << [@score1i,  @score2i]   if @score1i  || @score2i
  pairs << [@score1,   @score2]    if @score1   || @score2
  pairs << [@score1et, @score2et]  if @score1et || @score2et
  pairs << [@score1p,  @score2p]   if @score1p  || @score2p

  if pairs.empty?
    pairs   # e.g. return []
  elsif pairs.size == 1
    pairs[0]  # return single pair "unwrapped" e.g. [0,1] instead of [[0,1]] - why? why not?
  else
    pairs
  end
end

#to_h(format = :default) ⇒ Object Also known as: to_hash



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/score-formats/score.rb', line 104

def to_h( format = :default )
   case format.to_sym
   when :default, :std
     ## check/todo:  only add entries if ft, ht, etc. have values (non-null) or always - why? why not?
     h = {}
     h[:ht] = [@score1i,  @score2i]   if @score1i  || @score2i
     h[:ft] = [@score1,   @score2]    if @score1   || @score2
     h[:et] = [@score1et, @score2et]  if @score1et || @score2et
     h[:p]  = [@score1p,  @score2p]   if @score1p  || @score2p
     h
   when :db
     ## use a "flat" structure with "internal" std names
     { score1i:  @score1i,   score2i:  @score2i,
       score1:   @score1,    score2:   @score2,
       score1et: @score1et,  score2et: @score2et,
       score1p:  @score1p,   score2p:  @score2p
     }
   else
     puts "!! ERROR: unknown score to_h format >#{format}<"
     exit 1
   end
end

#valuesObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/score-formats/score.rb', line 129

def values
  ## todo/ fix: always return complete array
  ##  e.g. [score1i, score2i, score1, score2, score1et, score2et, score1p, score2p]

  ## todo: how to handle game w/o extra time
  #   but w/ optional penalty ???  e.g. used in copa liberatores, for example
  #    retrun 0,0 or nil,nil for extra time score ?? or -1, -1 ??
  #    for now use nil,nil
  score = []
  score += [@score1i,  @score2i]     if @score1p || @score2p || @score1et || @score2et || @score1 || score2 || score1i || score2i
  score += [@score1,   @score2]      if @score1p || @score2p || @score1et || @score2et || @score1 || score2
  score += [@score1et, @score2et]    if @score1p || @score2p || @score1et || @score2et
  score += [@score1p,  @score2p]     if @score1p || @score2p
  score
end