score-formats gem - read / parse and print sports match scores (incl. half time, full time, extra time, penalties and more) e.g. 6-5 pen. 2-2 a.e.t. 1-1 (1-0) or 8-4 (4-2)

Hello,

   I've extracted a little stand-alone (no-dependencies) gem called
score-formats [1] that lets you read / parse and print sports match
scores (incl. half time, full time, extra time, penalties and more)
e.g.
6-5 pen. 2-2 a.e.t. 1-1 (1-0) or 8-4 (4-2) or even in German style e.g.
i.E. 6:5, n.V. 2:2 (1:1, 1:0).

The idea is to follow the `Date` class and make `Score` into a
top-level free-standing class. Let's say you have the match score:

  6-5 pen. 2-2 a.e.t. 1-1 (1-0)

Using

require "score/formats"

score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
score.ht  #=> [1,0]
score.ft  #=> [1,1]
score.et  #=> [2,2]
score.p   #=> [6,5]

you can parse the score into its components, that is, the
half time (ht), full time (ft), extra time (et)
and the penalties (p) shootout score.

Like `Date` you can initialize `Score` with "to-the-metal"
integer numbers e.g.:

score = Score.new( 1, 0, 1, 1, 2, 2, 6, 5 )
score.ht  #=> [1,0]
score.ft  #=> [1,1]
score.et  #=> [2,2]
score.p   #=> [6,5]

For now `Score` offers in addition to the read-only `ht`, `ft`, `et`,
`p` accesors some more methods:

Use `ht?`, `ft?`, `et?`, `p?` for checking if the score components are
present e.g.

score = Score.new
score.ht?  #=> false
score.ft?  #=> false
score.et?  #=> false
score.p?   #=> false

# -or-

score = Score.parse( "8-2 (4-1)" )
score.ht?  #=> true
score.ft?  #=> true
score.et?  #=> false
score.p?   #=> false

Use `to_a` to get an array of score component pairs (or an empty array
for none) e.g.

score = Score.parse( "8-2 (4-1)" )
score.to_a  #=> [[4,1], [8-2]]

# -or-
score = Score.parse( "0-0" )
score.to_a  #=> [0,0]

Use `values` to get an array of "flat" integer numbers e.g.

score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
score.values #=> [1,0,1,1,2,2,6,5]

Use `to_h` to get a hash with key / value pairs e.g.

score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
score.to_h #=> { ht: [1,0],
           #     ft: [1,1],
           #     et: [2,2],
           #     p:  [6,5] }

# -or -
score = Score.parse( "8-2 (4-1)" )
score.to_h #=> { ht: [4,1],
           #     ft: [8,2] }

Use the `:db` format to get a hash with "flat" key / value pairs e.g.

score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
score.to_h( :db ) #=> { score1i:  1, score2i:  0,
                  #     score1:   1, score2:   1,
                  #     score1et: 2, score2et: 2,
                  #     score1p:  6, score2p:  5 }

# -or -
score = Score.parse( "8-2 (4-1)" )
score.to_h( :db ) #=> { score1i:  4,   score2i:  1,
                  #     score1:   8,   score2:   2,
                  #     score1et: nil, score2et: nil,
                  #     score1p:  nil, score2p:  nil}

  That's it for now.

   Enjoy the beautiful game (or other sports) with ruby. Happy data
wrangling. Cheers. Prost.

[1] https://github.com/sportdb/sport.db/tree/master/score-formats