class Score

add more convenience / shortcut helpers / named ctors to score class itself

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

Constants

SCORE_SPLIT_RE

Attributes

score1[R]
score1et[R]
score1i[R]
score1p[R]
score2[R]
score2et[R]
score2i[R]
score2p[R]

Public Class Methods

find!( line, lang: ScoreFormats.lang ) click to toggle source
# File lib/score-formats.rb, line 66
def self.find!( line, lang: ScoreFormats.lang )
  ScoreFormats.find!( line, lang: lang )
end
new( *values ) click to toggle source
# File lib/score-formats/score.rb, line 75
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
parse( line, lang: ScoreFormats.lang ) click to toggle source
# File lib/score-formats.rb, line 62
def self.parse( line, lang: ScoreFormats.lang )
  ScoreFormats.parse( line, lang: lang )
end
split( str ) click to toggle source
# 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 - português / portuguese" 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?

    if str.empty? || ['-', '-:-', '?'].include?( str )
      ## do NOT warn for known "good" empty scores for now - why? why not?
      ##   add some more?? use Score.empty? or such - why? why not?
    else
      puts "!! WARN - cannot match (split) score format >#{str}<"
    end

    []
  end
end

Public Instance Methods

et() click to toggle source
# File lib/score-formats/score.rb, line 52
def et()  [@score1et, @score2et]; end
Also aliased as: extra_time
et?() click to toggle source
# File lib/score-formats/score.rb, line 65
def et?()  @score1et && @score2et; end
Also aliased as: extra_time?
extra_time()
Alias for: et
extra_time?()
Alias for: et?
format_de( format=:default ) click to toggle source
# File lib/score-formats/printer.rb, line 50
def format_de( format=:default )
  ## note: format gets ignored for now (only one available)

  buf = String.new('')
  ## note: also allow (minimal) scores only with a.e.t. (and no full time)
  if ft? || et?        # 2-2 (1-1) n.V. 5-1 i.E.
    if et?
      buf << "#{@score1et}:#{@score2et}"
    end
    if ft?
      if buf.empty?
        buf << " #{@score1}:#{@score2}"
        ## note:
        ##   avoid 0-0 (0-0)
        ##  only print if score1 & score2 NOT 0-0
        if ht? && ft != [0,0]
          buf << " (#{@score1i}:#{@score2i})"
        end
      else  ## assume pen. and/or a.e.t.
        buf << " (#{@score1}:#{@score2}"
        if ht? && ft != [0,0]
          buf << ", #{@score1i}:#{@score2i}"
        end
        buf << ")"
      end
    end
    if et?
      buf << " n.V."
    end
    if p?
      buf << " #{@score1p}:#{@score2p} i.E."
    end
  else # assume empty / unknown score
    buf << '-'
  end
  buf.strip
end
format_en( format=:default ) click to toggle source
# File lib/score-formats/printer.rb, line 14
def format_en( format=:default )
  ## note: format gets ignored for now (only one available)

  buf = String.new('')
  ## note: also allow (minimal) scores only with a.e.t. (and no full time)
  if ft? || et?
    if p?
      buf << "#{@score1p}-#{@score2p} pen."
    end
    if et?
      buf << " #{@score1et}-#{@score2et} a.e.t."
    end
    if ft?
       if buf.empty?
         buf << " #{@score1}-#{@score2}"
         ## note:
         ##   avoid 0-0 (0-0)
         ##  only print if score1 & score2 NOT 0-0
         if ht? && ft != [0,0]
           buf << " (#{@score1i}-#{@score2i})"
         end
       else  ## assume pen. and/or a.e.t.
         buf << " (#{@score1}-#{@score2}"
         if ht? && ft != [0,0]
           buf << ", #{@score1i}-#{@score2i}"
         end
         buf << ")"
       end
    end
  else # assume empty / unknown score
     buf << '-'
  end
  buf.strip
end
ft() click to toggle source

alternate accessor via array e.g. ft and ft

# File lib/score-formats/score.rb, line 50
def ft()  [@score1,   @score2];   end
Also aliased as: full_time
ft?() click to toggle source

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

e.g.  1-nil  or nil-1  - why? why not?
# File lib/score-formats/score.rb, line 63
def ft?()  @score1   && @score2;   end
Also aliased as: full_time?
full_time()
Alias for: ft
full_time?()
Alias for: ft?
half_time()
Alias for: ht
half_time?()
Alias for: ht?
ht() click to toggle source
# File lib/score-formats/score.rb, line 51
def ht()  [@score1i,  @score2i];  end
Also aliased as: half_time
ht?() click to toggle source
# File lib/score-formats/score.rb, line 64
def ht?()  @score1i  && @score2i;  end
Also aliased as: half_time?
p() click to toggle source
# File lib/score-formats/score.rb, line 53
def p()   [@score1p,  @score2p];  end
Also aliased as: pen, penalties
p?() click to toggle source
# File lib/score-formats/score.rb, line 66
def p?()   @score1p  && @score2p;  end
Also aliased as: pen?, penalties?
pen()
Alias for: p
pen?()
Alias for: p?
penalties()
Alias for: p
penalties?()
Alias for: p?
to_a() click to toggle source
# File lib/score-formats/score.rb, line 152
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_formatted_s( format=:default, lang: ScoreFormats.lang ) click to toggle source
# File lib/score-formats/printer.rb, line 3
def to_formatted_s( format=:default, lang: ScoreFormats.lang )
  ## note: format gets ignored for now (only one available)
  case lang.to_sym
  when :de   then   format_de( format )
  else              format_en( format ) # note: for now always fallback to english
  end
end
Also aliased as: to_s
to_h( format = :default ) click to toggle source
# File lib/score-formats/score.rb, line 112
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
to_s( format=:default, lang: ScoreFormats.lang )
Alias for: to_formatted_s
values() click to toggle source
# File lib/score-formats/score.rb, line 136
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