Class: SportDb::Lang

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/langs/lang.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLang

Returns a new instance of Lang.



11
12
13
14
# File 'lib/sportdb/langs/lang.rb', line 11

def initialize
  # fix/todo: load on demand; only if no fixtures loaded/configured use builtin
  load_builtin_words
end

Instance Attribute Details

#langObject

Returns the value of attribute lang.



9
10
11
# File 'lib/sportdb/langs/lang.rb', line 9

def lang
  @lang
end

Instance Method Details

#build_groupObject



82
83
84
85
86
87
# File 'lib/sportdb/langs/lang.rb', line 82

def build_group
  h = @words[ @lang ]
  values = ""   # Note: always construct a new string (do NOT use a reference to hash value)
  values << h['group']
  values
end

#build_knockout_roundObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sportdb/langs/lang.rb', line 125

def build_knockout_round
  h = @words[ @lang ]
  values = ""  # NB: 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_leg1Object



111
112
113
114
115
116
# File 'lib/sportdb/langs/lang.rb', line 111

def build_leg1
  h = @words[ @lang ]
  values = ""  # NB: always construct a new string (do NOT use a reference to hash value)
  values << h['leg1']
  values
end

#build_leg2Object



118
119
120
121
122
123
# File 'lib/sportdb/langs/lang.rb', line 118

def build_leg2
  h = @words[ @lang ]
  values = ""  # NB: always construct a new string (do NOT use a reference to hash value)
  values << h['leg2']
  values
end

#build_roundObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sportdb/langs/lang.rb', line 89

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 = ""   # NB: 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

#groupObject



46
# File 'lib/sportdb/langs/lang.rb', line 46

def group()           @cache[ @lang ][ :group ] ||= build_group;  end

#group_reObject 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



68
# File 'lib/sportdb/langs/lang.rb', line 68

def group_re()           @cache[ @lang ][ :group_re ] ||= /#{group}/i; end

#knockout_roundObject



48
# File 'lib/sportdb/langs/lang.rb', line 48

def knockout_round()  @cache[ @lang ][ :knockout_round ] ||= build_knockout_round;  end

#knockout_round_reObject Also known as: regex_knockout_round



70
# File 'lib/sportdb/langs/lang.rb', line 70

def knockout_round_re()  @cache[ @lang ][ :knockout_round_re ] ||= /#{knockout_round}/i; end

#leg1Object



49
# File 'lib/sportdb/langs/lang.rb', line 49

def leg1()            @cache[ @lang ][ :leg1 ] ||= build_leg1; end

#leg1_reObject Also known as: regex_leg1



71
# File 'lib/sportdb/langs/lang.rb', line 71

def leg1_re()            @cache[ @lang ][ :leg1_re ] ||= /#{leg1}/i; end

#leg2Object



50
# File 'lib/sportdb/langs/lang.rb', line 50

def leg2()            @cache[ @lang ][ :leg2 ] ||= build_leg2; end

#leg2_reObject Also known as: regex_leg2



72
# File 'lib/sportdb/langs/lang.rb', line 72

def leg2_re()            @cache[ @lang ][ :leg2_re ] ||= /#{leg2}/i; end

#load_builtin_wordsObject



17
18
19
20
21
# File 'lib/sportdb/langs/lang.rb', line 17

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sportdb/langs/lang.rb', line 24

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

#roundObject



47
# File 'lib/sportdb/langs/lang.rb', line 47

def round()           @cache[ @lang ][ :round ] ||= build_round;  end

#round_reObject Also known as: regex_round



69
# File 'lib/sportdb/langs/lang.rb', line 69

def round_re()           @cache[ @lang ][ :round_re ] ||= /#{round}/i; end