Class: SportDb::Lang
- Inherits:
-
Object
- Object
- SportDb::Lang
- Includes:
- LogUtils::Logging
- Defined in:
- lib/sportdb/langs/lang.rb
Instance Attribute Summary collapse
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#words ⇒ Object
readonly
note: allow access to builtin data / word lists.
Instance Method Summary collapse
- #build_group ⇒ Object
- #build_knockout_round ⇒ Object
- #build_leg1 ⇒ Object
- #build_leg2 ⇒ Object
- #build_round ⇒ Object
- #group ⇒ Object
-
#group_re ⇒ Object
(also: #regex_group)
regex helpers todo/fix: escape for regex? note: let’s ignore case (that is, UPCASE,downcase); always use /i flag todo: escape for regex? todo/fix: sort by length - biggest words go first? does regex match biggest word automatically?? - check todo/fix: make - optional e.g.
-
#initialize ⇒ Lang
constructor
A new instance of Lang.
- #knockout_round ⇒ Object
- #knockout_round_re ⇒ Object (also: #regex_knockout_round)
- #leg1 ⇒ Object
- #leg1_re ⇒ Object (also: #regex_leg1)
- #leg2 ⇒ Object
- #leg2_re ⇒ Object (also: #regex_leg2)
- #load_builtin_words ⇒ Object
- #load_words(langs, include_path) ⇒ Object
- #round ⇒ Object
- #round_re ⇒ Object (also: #regex_round)
Constructor Details
#initialize ⇒ Lang
Returns a new instance of Lang.
12 13 14 15 |
# File 'lib/sportdb/langs/lang.rb', line 12 def initialize # fix/todo: load on demand; only if no fixtures loaded/configured use builtin load_builtin_words end |
Instance Attribute Details
#lang ⇒ Object
Returns the value of attribute lang.
9 10 11 |
# File 'lib/sportdb/langs/lang.rb', line 9 def lang @lang end |
#words ⇒ Object (readonly)
note: allow access to builtin data / word lists
10 11 12 |
# File 'lib/sportdb/langs/lang.rb', line 10 def words @words end |
Instance Method Details
#build_group ⇒ Object
83 84 85 86 87 88 |
# File 'lib/sportdb/langs/lang.rb', line 83 def build_group h = @words[ @lang ] values = String.new # note: always construct a new string (do NOT use a reference to hash value) values << h['group'] values end |
#build_knockout_round ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/sportdb/langs/lang.rb', line 126 def build_knockout_round h = @words[ @lang ] values = String.new # note: always construct a new string (do NOT use a reference to hash value) values << h['round32'] values << "|" << h['round16'] values << "|" << h['quarterfinals'] values << "|" << h['semifinals'] values << "|" << h['fifthplace'] if h['fifthplace'] # nb: allow empty/is optional!! values << "|" << h['thirdplace'] values << "|" << h['final'] values << "|" << h['playoffs'] if h['playoffs'] # nb: allow empty/is optional!! values end |
#build_leg1 ⇒ Object
112 113 114 115 116 117 |
# File 'lib/sportdb/langs/lang.rb', line 112 def build_leg1 h = @words[ @lang ] values = String.new # note: always construct a new string (do NOT use a reference to hash value) values << h['leg1'] values end |
#build_leg2 ⇒ Object
119 120 121 122 123 124 |
# File 'lib/sportdb/langs/lang.rb', line 119 def build_leg2 h = @words[ @lang ] values = String.new # note: always construct a new string (do NOT use a reference to hash value) values << h['leg2'] values end |
#build_round ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sportdb/langs/lang.rb', line 90 def build_round # e.g. Spieltag|Runde|Achtelfinale|Viertelfinale|Halbfinale|Finale ## fix/todo: ## sort by length first - to allow best match e.g. ## 3rd place play-off instead of Play-off ?? etc. - why? why not? h = @words[ @lang ] values = String.new # note: always construct a new string (do NOT use a reference to hash value) values << h['round'] ### add knockout rounds values too values << "|" << h['round32'] values << "|" << h['round16'] values << "|" << h['quarterfinals'] values << "|" << h['semifinals'] values << "|" << h['fifthplace'] if h['fifthplace'] # nb: allow empty/is optional!! values << "|" << h['thirdplace'] values << "|" << h['final'] values << "|" << h['playoffs'] if h['playoffs'] # nb: allow empty/is optional!! values end |
#group ⇒ Object
47 |
# File 'lib/sportdb/langs/lang.rb', line 47 def group() @cache[ @lang ][ :group ] ||= build_group; end |
#group_re ⇒ Object Also known as: regex_group
regex helpers todo/fix: escape for regex? note: let’s ignore case (that is, UPCASE,downcase); always use /i flag todo: escape for regex? todo/fix: sort by length - biggest words go first? does regex match biggest word automatically?? - check
todo/fix: make - optional e.g. convert to ( |-) or better [ \-] ??
note: let’s ignore case (that is, UPCASE,downcase); always use /i flag todo/fix: escape for regex? todo/fix: sort by length - biggest words go first? does regex match biggest word automatically?? - check
todo/fix: make - optional e.g. convert to ( |-) or better [ \-] ??
note: let’s ignore case (that is, UPCASE,downcase); always use /i flag todo/fix: escape for regex? note: let’s ignore case (that is, UPCASE,downcase); always use /i flag todo/fix: escape for regex? note: let’s ignore case (that is, UPCASE,downcase); always use /i flag
69 |
# File 'lib/sportdb/langs/lang.rb', line 69 def group_re() @cache[ @lang ][ :group_re ] ||= /#{group}/i; end |
#knockout_round ⇒ Object
49 |
# File 'lib/sportdb/langs/lang.rb', line 49 def knockout_round() @cache[ @lang ][ :knockout_round ] ||= build_knockout_round; end |
#knockout_round_re ⇒ Object Also known as: regex_knockout_round
71 |
# File 'lib/sportdb/langs/lang.rb', line 71 def knockout_round_re() @cache[ @lang ][ :knockout_round_re ] ||= /#{knockout_round}/i; end |
#leg1 ⇒ Object
50 |
# File 'lib/sportdb/langs/lang.rb', line 50 def leg1() @cache[ @lang ][ :leg1 ] ||= build_leg1; end |
#leg1_re ⇒ Object Also known as: regex_leg1
72 |
# File 'lib/sportdb/langs/lang.rb', line 72 def leg1_re() @cache[ @lang ][ :leg1_re ] ||= /#{leg1}/i; end |
#leg2 ⇒ Object
51 |
# File 'lib/sportdb/langs/lang.rb', line 51 def leg2() @cache[ @lang ][ :leg2 ] ||= build_leg2; end |
#leg2_re ⇒ Object Also known as: regex_leg2
73 |
# File 'lib/sportdb/langs/lang.rb', line 73 def leg2_re() @cache[ @lang ][ :leg2_re ] ||= /#{leg2}/i; end |
#load_builtin_words ⇒ Object
18 19 20 21 22 |
# File 'lib/sportdb/langs/lang.rb', line 18 def load_builtin_words langs = %w[en de es fr it pt ro] load_words( langs, SportDb::Langs.config_path ) end |
#load_words(langs, include_path) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sportdb/langs/lang.rb', line 25 def load_words( langs, include_path ) @lang = 'en' # make default lang english/en @words = {} # resets fixtures @cache = {} # reset cached values langs.each_with_index do |lang,i| path = "#{include_path}/#{lang}.yml" logger.debug( "loading words #{lang} (#{i+1}/#{langs.size}) in (#{path})..." ) @words[ lang ] = YAML.load( File.open( path, 'r:utf-8' ).read ) @cache[ lang ] = {} ## setup empty cache (hash) end end |
#round ⇒ Object
48 |
# File 'lib/sportdb/langs/lang.rb', line 48 def round() @cache[ @lang ][ :round ] ||= build_round; end |
#round_re ⇒ Object Also known as: regex_round
70 |
# File 'lib/sportdb/langs/lang.rb', line 70 def round_re() @cache[ @lang ][ :round_re ] ||= /#{round}/i; end |