score-formats 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ce70736d26500e8d3866ae96a2f75e6ce8c1725
4
- data.tar.gz: 16e29461a44bb4c232039b82f38d805596b47acf
3
+ metadata.gz: fde9c3127903e7a2bcd4d631a1a286ef1b3faf5a
4
+ data.tar.gz: ec30d9beb6fd14b726dbe80f549b6371e8411426
5
5
  SHA512:
6
- metadata.gz: 28ed77625082a2f1b29713b03cf92354df0799c8b0189f5e40e4ccddbad34cb6e14b286b580adee97304cb3b84e8c14064dd26c33efcfea80e94e4caa7354d6e
7
- data.tar.gz: bf023b724232b5ca987615d223c8d8aef928a3afcf7b9807e1f1b9167fbabfc2a80d78404220cb5de6633585be885ea5e9f05c0770d28da59a1cb89d827b63b7
6
+ metadata.gz: e3de1af6dd9d46f17861c31ed8a415a546f904d47a3aae525fb6ef15f169303d9edf574fe154340ee1e9cf1dfe58650e38a95e274dddc0e0ce272315a9353bde
7
+ data.tar.gz: 5b760ecbaf5b5163a620a729353142af71446641535254fe0a8dd7bc4491dc4b0c7970b9a0103bb97c6a06e699f2b565623a614e5e1c68d13b905543869559a8
@@ -5,9 +5,11 @@ Rakefile
5
5
  lib/score-formats.rb
6
6
  lib/score-formats/formats.rb
7
7
  lib/score-formats/parser.rb
8
+ lib/score-formats/printer.rb
8
9
  lib/score-formats/score.rb
9
10
  lib/score-formats/version.rb
10
11
  lib/score/formats.rb
11
12
  test/helper.rb
12
13
  test/test_formats.rb
14
+ test/test_printer.rb
13
15
  test/test_score.rb
data/README.md CHANGED
@@ -8,11 +8,162 @@
8
8
  * forum :: [opensport](http://groups.google.com/group/opensport)
9
9
 
10
10
 
11
+
11
12
  ## Usage
12
13
 
13
14
 
14
- to be done
15
+ The idea is to follow the `Date` class and make `Score`
16
+ into a top-level free-standing class. Let's say you have the match score:
17
+
18
+ 6-5 pen. 2-2 a.e.t. 1-1 (1-0)
19
+
20
+ Using
21
+
22
+ ``` ruby
23
+ require "score/formats"
24
+
25
+ score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
26
+ score.ht #=> [1,0]
27
+ score.ft #=> [1,1]
28
+ score.et #=> [2,2]
29
+ score.p #=> [6,5]
30
+ ```
31
+
32
+ you can parse the score into its components, that is, the
33
+ half time (ht), full time (ft), extra time (et)
34
+ and the penalties (p) shootout score.
35
+
36
+ Like `Date` you can initialize `Score` with "to-the-metal"
37
+ integer numbers e.g.:
38
+
39
+ ``` ruby
40
+ score = Score.new( 1, 0, 1, 1, 2, 2, 6, 5 )
41
+ score.ht #=> [1,0]
42
+ score.ft #=> [1,1]
43
+ score.et #=> [2,2]
44
+ score.p #=> [6,5]
45
+ ```
46
+
47
+ For now `Score` offers in addition to the read-only `ht`, `ft`, `et`, `p` accessors some more methods:
48
+
49
+
50
+ Use `ht?`, `ft?`, `et?`, `p?` for checking if the score components are present e.g.
51
+
52
+ ``` ruby
53
+ score = Score.new
54
+ score.ht? #=> false
55
+ score.ft? #=> false
56
+ score.et? #=> false
57
+ score.p? #=> false
58
+
59
+ # -or-
60
+
61
+ score = Score.parse( "8-2 (4-1)" )
62
+ score.ht? #=> true
63
+ score.ft? #=> true
64
+ score.et? #=> false
65
+ score.p? #=> false
66
+ ```
67
+
68
+ Use `to_a` to get an array of score component pairs (or an empty array for none) e.g.
69
+
70
+ ``` ruby
71
+ score = Score.parse( "8-2 (4-1)" )
72
+ score.to_a #=> [[4,1], [8-2]]
73
+
74
+ # -or-
75
+ score = Score.parse( "0-0" )
76
+ score.to_a #=> [0,0]
77
+ ```
78
+
79
+ Use `values` to get an array of "flat" integer numbers e.g.
80
+
81
+ ``` ruby
82
+ score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
83
+ score.values #=> [1,0,1,1,2,2,6,5]
84
+ ```
85
+
86
+ Use `to_h` to get a hash with key / value pairs e.g.
87
+
88
+ ``` ruby
89
+ score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
90
+ score.to_h #=> { ht: [1,0],
91
+ # ft: [1,1],
92
+ # et: [2,2],
93
+ # p: [6,5] }
94
+
95
+ # -or -
96
+ score = Score.parse( "8-2 (4-1)" )
97
+ score.to_h #=> { ht: [4,1],
98
+ # ft: [8,2] }
99
+ ```
100
+
101
+ Use the `:db` format to get a hash with "flat" key / value pairs e.g.
102
+
103
+ ``` ruby
104
+ score = Score.parse( "6-5 pen. 2-2 a.e.t. 1-1 (1-0)" )
105
+ score.to_h( :db ) #=> { score1i: 1, score2i: 0,
106
+ # score1: 1, score2: 1,
107
+ # score1et: 2, score2et: 2,
108
+ # score1p: 6, score2p: 5 }
109
+
110
+ # -or -
111
+ score = Score.parse( "8-2 (4-1)" )
112
+ score.to_h( :db ) #=> { score1i: 4, score2i: 1,
113
+ # score1: 8, score2: 2,
114
+ # score1et: nil, score2et: nil,
115
+ # score1p: nil, score2p: nil}
116
+ ```
117
+
118
+ Use `to_s` to pretty print / get the score (as string) e.g.
119
+
120
+ ``` ruby
121
+ score = Score.new( 1, 0, 1, 1, 2, 2, 6, 5 )
122
+ score.to_s #=> "6-5 pen. 2-2 a.e.t. 1-1 (1-0)"
123
+
124
+ # -or -
125
+
126
+ score = Score.new( 0, 0, 0, 0 )
127
+ score.to_s #=> "0-0"
128
+
129
+ # -or -
130
+
131
+ score = Score.new
132
+ score.to_s #=> "-"
133
+ ```
134
+
135
+
136
+
137
+ ## Bonus: Multi-language internationalization (i18n) support
138
+
139
+ _Sprichst du Deutsch? • ¿Hablas español? • Parles tu français?_
140
+
141
+ Using the `lang` option you can switch the language.
142
+ For now only `de`, that is, German (Deutsch) is built-in / supported.
143
+ Example:
144
+
145
+ ``` ruby
146
+ score = Score.parse( "2:2 (1:1, 1:0) n.V. 6:5 i.E.", lang: "de" )
147
+ score.ht #=> [1,0]
148
+ score.ft #=> [1,1]
149
+ score.et #=> [2,2]
150
+ score.p #=> [6,5]
151
+ ```
152
+
153
+ That's all for now.
154
+
155
+
156
+
157
+
158
+ ## Installation
159
+
160
+ Use
161
+
162
+ gem install score-formats
163
+
164
+ or add the gem to your Gemfile
15
165
 
166
+ gem 'score-formats'
16
167
 
17
168
 
18
169
 
@@ -23,6 +23,7 @@ end # module ScoreFormats
23
23
  require 'score-formats/score'
24
24
  require 'score-formats/formats'
25
25
  require 'score-formats/parser'
26
+ require 'score-formats/printer'
26
27
 
27
28
 
28
29
 
@@ -40,7 +41,7 @@ module ScoreFormats
40
41
 
41
42
  ## note: cache all "built-in" lang versions (e.g. formats == nil)
42
43
  @@parser ||= {}
43
- parser = @@parser[ lang ] ||= ScoreParser.new( lang: lang )
44
+ @@parser[ lang ] ||= ScoreParser.new( lang: lang )
44
45
  end
45
46
 
46
47
  def self.parse( line, lang: ScoreFormats.lang )
@@ -80,7 +80,7 @@ private
80
80
 
81
81
  ## puts "[parse_date_time] match_data:"
82
82
  ## pp h
83
- logger.debug " [parse_matchdata] hash: >#{h.inspect}<"
83
+ ## logger.debug " [parse_matchdata] hash: >#{h.inspect}<"
84
84
 
85
85
  score1i = nil # half time (ht) scores
86
86
  score2i = nil
@@ -0,0 +1,89 @@
1
+ class Score
2
+
3
+ def to_formatted_s( format=:default, lang: ScoreFormats.lang )
4
+ ## note: format gets ignored for now (only one available)
5
+ case lang.to_sym
6
+ when :de then format_de( format )
7
+ else format_en( format ) # note: for now always fallback to english
8
+ end
9
+ end
10
+ alias_method :to_s, :to_formatted_s
11
+
12
+
13
+
14
+ def format_en( format=:default )
15
+ ## note: format gets ignored for now (only one available)
16
+
17
+ buf = String.new('')
18
+ ## note: also allow (minimal) scores only with a.e.t. (and no full time)
19
+ if ft? || et?
20
+ if p?
21
+ buf << "#{@score1p}-#{@score2p} pen."
22
+ end
23
+ if et?
24
+ buf << " #{@score1et}-#{@score2et} a.e.t."
25
+ end
26
+ if ft?
27
+ if buf.empty?
28
+ buf << " #{@score1}-#{@score2}"
29
+ ## note:
30
+ ## avoid 0-0 (0-0)
31
+ ## only print if score1 & score2 NOT 0-0
32
+ if ht? && ft != [0,0]
33
+ buf << " (#{@score1i}-#{@score2i})"
34
+ end
35
+ else ## assume pen. and/or a.e.t.
36
+ buf << " (#{@score1}-#{@score2}"
37
+ if ht? && ft != [0,0]
38
+ buf << ", #{@score1i}-#{@score2i}"
39
+ end
40
+ buf << ")"
41
+ end
42
+ end
43
+ else # assume empty / unknown score
44
+ buf << '-'
45
+ end
46
+ buf.strip
47
+ end
48
+
49
+
50
+ def format_de( format=:default )
51
+ ## note: format gets ignored for now (only one available)
52
+
53
+ buf = String.new('')
54
+ ## note: also allow (minimal) scores only with a.e.t. (and no full time)
55
+ if ft? || et? # 2-2 (1-1) n.V. 5-1 i.E.
56
+ if et?
57
+ buf << "#{@score1et}:#{@score2et}"
58
+ end
59
+ if ft?
60
+ if buf.empty?
61
+ buf << " #{@score1}:#{@score2}"
62
+ ## note:
63
+ ## avoid 0-0 (0-0)
64
+ ## only print if score1 & score2 NOT 0-0
65
+ if ht? && ft != [0,0]
66
+ buf << " (#{@score1i}:#{@score2i})"
67
+ end
68
+ else ## assume pen. and/or a.e.t.
69
+ buf << " (#{@score1}:#{@score2}"
70
+ if ht? && ft != [0,0]
71
+ buf << ", #{@score1i}:#{@score2i}"
72
+ end
73
+ buf << ")"
74
+ end
75
+ end
76
+ if et?
77
+ buf << " n.V."
78
+ end
79
+ if p?
80
+ buf << " #{@score1p}:#{@score2p} i.E."
81
+ end
82
+ else # assume empty / unknown score
83
+ buf << '-'
84
+ end
85
+ buf.strip
86
+ end
87
+
88
+
89
+ end # class Score
@@ -15,7 +15,7 @@ class Score
15
15
  ## e.g. allow/support
16
16
  ## 1-1 or 1 - 1 - "english" style
17
17
  ## 1:1 - "german / deutsch" style
18
- ## 1x1 1X1 - "brazil/portugese" style
18
+ ## 1x1 1X1 - "brazil - português / portuguese" style
19
19
 
20
20
  ## note: add unicode "fancy" dash too (e.g. –)
21
21
  ## add some more - why? why not?
@@ -23,16 +23,15 @@ class Score
23
23
  if m=SCORE_SPLIT_RE.match(str)
24
24
  [m[1].to_i, m[2].to_i]
25
25
  else
26
- # no match - warn if str is not empty? why? why not?
27
- ##
28
- ## todo/fix:
29
- ## do NOT warn on
30
- ## assert_equal [], Score.split( '-' )
31
- ## assert_equal [], Score.split( '-:-' )
32
- ## assert_equal [], Score.split( '?' )
33
- ## for now - add more?
34
-
35
- puts "!! WARN - cannot match (split) score format >#{str}<" unless str.empty?
26
+ # no match - warn if str is NOT empty? why? why not?
27
+
28
+ if str.empty? || ['-', '-:-', '?'].include?( str )
29
+ ## do NOT warn for known "good" empty scores for now - why? why not?
30
+ ## add some more?? use Score.empty? or such - why? why not?
31
+ else
32
+ puts "!! WARN - cannot match (split) score format >#{str}<"
33
+ end
34
+
36
35
  []
37
36
  end
38
37
  end
@@ -123,7 +122,6 @@ class Score
123
122
  exit 1
124
123
  end
125
124
  end
126
- alias_method :to_hash, :to_h ## add alias - why? why not?
127
125
 
128
126
 
129
127
  def values
@@ -1,8 +1,8 @@
1
1
 
2
2
  module ScoreFormats
3
3
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
4
- MINOR = 0
5
- PATCH = 1
4
+ MINOR = 1
5
+ PATCH = 0
6
6
  VERSION = [MAJOR,MINOR,PATCH].join('.')
7
7
 
8
8
  def self.version
@@ -0,0 +1,51 @@
1
+ ###
2
+ # to run use
3
+ # ruby -I ./lib -I ./test test/test_printer.rb
4
+
5
+
6
+ require 'helper'
7
+
8
+ class TestPrinter < MiniTest::Test
9
+
10
+ def test_en
11
+ [['0-0', [nil,nil, 0, 0]],
12
+ ['0-0', [0, 0, 0, 0]],
13
+ ['1-0', [nil,nil, 1, 0]],
14
+ ['3-2 (1-0)', [1, 0, 3, 2]],
15
+ ['2-0 (0-0)', [0, 0, 2, 0]],
16
+ ['5-4 a.e.t.', [nil,nil,nil,nil, 5, 4]],
17
+ ['5-4 a.e.t. (3-2, 1-0)', [1, 0, 3, 2, 5, 4]],
18
+ ['5-4 a.e.t. (3-2)', [nil,nil, 3, 2, 5, 4]],
19
+ ['6-3 pen. (3-2, 1-0)', [1, 0, 3, 2, nil, nil, 6, 3]],
20
+ ['6-3 pen. (3-2)', [nil,nil, 3, 2, nil, nil, 6, 3]],
21
+ ['6-3 pen. 5-4 a.e.t. (3-2, 1-0)', [1, 0, 3, 2, 5, 4, 6, 3]],
22
+ ].each do |rec|
23
+ exp = rec[0]
24
+ values = rec[1]
25
+
26
+ assert_equal exp, Score.new( *values ).to_s( lang: 'en' )
27
+ end
28
+ end
29
+
30
+ def test_de
31
+ [['0:0', [nil,nil, 0, 0]],
32
+ ['0:0', [0, 0, 0, 0]],
33
+ ['1:0', [nil,nil, 1, 0]],
34
+ ['3:2 (1:0)', [1, 0, 3, 2]],
35
+ ['2:0 (0:0)', [0, 0, 2, 0]],
36
+ ['5:4 n.V.', [nil,nil,nil,nil, 5, 4]],
37
+ ['5:4 (3:2, 1:0) n.V.', [1, 0, 3, 2, 5, 4]],
38
+ ['5:4 (3:2) n.V.', [nil,nil, 3, 2, 5, 4]],
39
+ ['3:2 (1:0) 6:3 i.E.', [1, 0, 3, 2, nil, nil, 6, 3]],
40
+ ['3:2 6:3 i.E.', [nil,nil, 3, 2, nil, nil, 6, 3]],
41
+ ['5:4 (3:2, 1:0) n.V. 6:3 i.E.', [1, 0, 3, 2, 5, 4, 6, 3]],
42
+ ].each do |rec|
43
+ exp = rec[0]
44
+ values = rec[1]
45
+
46
+ assert_equal exp, Score.new( *values ).to_s( lang: 'de' )
47
+ end
48
+ end
49
+
50
+ end # class TestPrinter
51
+
@@ -71,6 +71,6 @@ class TestScore < MiniTest::Test
71
71
  score1: 2, score2: 3,
72
72
  score1et: nil, score2et: nil,
73
73
  score1p: nil, score2p: nil ), score.to_h( :db )
74
- end
74
+ end
75
75
 
76
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: score-formats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-17 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -55,11 +55,13 @@ files:
55
55
  - lib/score-formats.rb
56
56
  - lib/score-formats/formats.rb
57
57
  - lib/score-formats/parser.rb
58
+ - lib/score-formats/printer.rb
58
59
  - lib/score-formats/score.rb
59
60
  - lib/score-formats/version.rb
60
61
  - lib/score/formats.rb
61
62
  - test/helper.rb
62
63
  - test/test_formats.rb
64
+ - test/test_printer.rb
63
65
  - test/test_score.rb
64
66
  homepage: https://github.com/sportdb/sport.db
65
67
  licenses: