Module: TextUtils::ValueHelper
- Included in:
- ValuesReader
- Defined in:
- lib/textutils/helper/value_helper.rb
Instance Method Summary collapse
-
#find_grade(value) ⇒ Object
NB: returns ary [grade,value] / two values.
-
#find_key_n_title(values) ⇒ Object
NB: returns ary [attribs,more_values] / two values.
- #is_address?(value) ⇒ Boolean
- #is_region?(value) ⇒ Boolean
- #is_taglist?(value) ⇒ Boolean
- #is_website?(value) ⇒ Boolean
- #is_year?(value) ⇒ Boolean
- #match_brewery(value) ⇒ Object
-
#match_city(value) ⇒ Object
NB: might be nil (city not found).
-
#match_country(value) ⇒ Object
todo/check: add to pair of matchers?? e.g.
- #match_km_squared(value) ⇒ Object
- #match_metro(value) ⇒ Object
- #match_metro_flag(value) ⇒ Object
- #match_metro_pop(value) ⇒ Object
- #match_number(value) ⇒ Object
-
#match_region_for_country(value, country_id) ⇒ Object
fix/todo: use match_region_for_country! w/ !!! why? why not?.
- #match_supra(value) ⇒ Object
-
#match_supra_flag(value) ⇒ Object
supranational (country).
- #match_website(value) ⇒ Object
- #match_year(value) ⇒ Object
Instance Method Details
#find_grade(value) ⇒ Object
NB: returns ary [grade,value] / two values
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/textutils/helper/value_helper.rb', line 196 def find_grade( value ) # NB: returns ary [grade,value] / two values grade = 4 # defaults to grade 4 e.g *** => 1, ** => 2, * => 3, -/- => 4 value = value.sub( /\s+(\*{1,3})\s*$/ ) do |_| # NB: stars must end field/value if $1 == '***' grade = 1 elsif $1 == '**' grade = 2 elsif $1 == '*' grade = 3 else # unknown grade; not possible, is'it? end '' # remove * from title if found end [grade,value] end |
#find_key_n_title(values) ⇒ Object
NB: returns ary [attribs,more_values] / two values
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/textutils/helper/value_helper.rb', line 216 def find_key_n_title( values ) # NB: returns ary [attribs,more_values] / two values ## fix: add/configure logger for ActiveRecord!!! logger = LogKernel::Logger.root ### todo/fix: allow check - do NOT allow mixed use of with key and w/o key ## either use keys or do NOT use keys; do NOT mix in a single fixture file ### support autogenerate key from first title value # if it looks like a key (only a-z lower case allowed); assume it's a key # - also allow . in keys e.g. world.quali.america, at.cup, etc. # - also allow 0-9 in keys e.g. at.2, at.3.1, etc. # fix/todo: add support for leading underscore _ # or allow keys starting w/ digits? if values[0] =~ /^([a-z][a-z0-9.]*[a-z0-9]|[a-z])$/ # NB: key must start w/ a-z letter (NB: minimum one letter possible) key_col = values[0] title_col = values[1] more_values = values[2..-1] else key_col = '<auto>' title_col = values[0] more_values = values[1..-1] end attribs = {} ## check title_col for grade (e.g. ***/**/*) and use returned stripped title_col if exits grade, title_col = find_grade( title_col ) # NB: for now - do NOT include default grade e.g. if grade (***/**/*) not present; attrib will not be present too if grade == 1 || grade == 2 || grade == 3 # grade found/present logger.debug " found grade #{grade} in title" attribs[:grade] = grade end ## title (split of optional synonyms) # e.g. FC Bayern Muenchen|Bayern Muenchen|Bayern titles = title_col.split('|') attribs[ :title ] = titles[0] ## add optional synonyms if present attribs[ :synonyms ] = titles[1..-1].join('|') if titles.size > 1 if key_col == '<auto>' ## autogenerate key from first title key_col = TextUtils.title_to_key( titles[0] ) logger.debug " autogen key »#{key_col}« from title »#{titles[0]}«" end attribs[ :key ] = key_col [attribs, more_values] end |
#is_address?(value) ⇒ Boolean
186 187 188 189 |
# File 'lib/textutils/helper/value_helper.rb', line 186 def is_address?( value ) # if value includes // assume address e.g. 3970 Weitra // Sparkasseplatz 160 value =~ /\/{2}/ end |
#is_region?(value) ⇒ Boolean
46 47 48 49 |
# File 'lib/textutils/helper/value_helper.rb', line 46 def is_region?( value ) # assume region code e.g. TX or N value =~ /^[A-Z]{1,2}$/ end |
#is_taglist?(value) ⇒ Boolean
191 192 193 |
# File 'lib/textutils/helper/value_helper.rb', line 191 def is_taglist?( value ) value =~ /^[a-z0-9\|_ ]+$/ end |
#is_website?(value) ⇒ Boolean
165 166 167 168 169 170 171 172 |
# File 'lib/textutils/helper/value_helper.rb', line 165 def is_website?( value ) # check for url/internet address e.g. www.ottakringer.at # - must start w/ www. or # - must end w/ .com # # fix: support more url format (e.g. w/o www. - look for .com .country code etc.) value =~ /^www\.|\.com$/ end |
#is_year?(value) ⇒ Boolean
125 126 127 128 |
# File 'lib/textutils/helper/value_helper.rb', line 125 def is_year?( value ) # founded/established year e.g. 1776 value =~ /^[0-9]{4}$/ end |
#match_brewery(value) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/textutils/helper/value_helper.rb', line 113 def match_brewery( value ) if value =~ /^by:/ ## by: -brewed by/brewery brewery_key = value[3..-1] ## cut off by: prefix brewery = Brewery.find_by_key!( brewery_key ) yield( brewery ) true # bingo - match found else false # no match found end end |
#match_city(value) ⇒ Object
NB: might be nil (city not found)
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/textutils/helper/value_helper.rb', line 68 def match_city( value ) # NB: might be nil (city not found) if value =~ /^city:/ ## city: city_key = value[5..-1] ## cut off city: prefix city = City.find_by_key( city_key ) yield( city ) # NB: might be nil (city not found) true # bingo - match found else false # no match found end end |
#match_country(value) ⇒ Object
todo/check: add to pair of matchers?? e.g. match_country and match_country!
- match_country will use find_by_key and match_country will use find_by_key! - why? why not?
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/textutils/helper/value_helper.rb', line 12 def match_country( value ) if value =~ /^country:/ # country: country_key = value[8..-1] # cut off country: prefix country = Country.find_by_key!( country_key ) yield( country ) true # bingo - match found else false # no match found end end |
#match_km_squared(value) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/textutils/helper/value_helper.rb', line 140 def match_km_squared( value ) ## allow numbers like 453 km² or 45_000 km2 if value =~ /^([0-9][0-9 _]+[0-9]|[0-9]{1,2})(?:\s*(?:km2|km²)\s*)$/ num = value.gsub( 'km2', '').gsub( 'km²', '' ).gsub(/[ _]/, '').to_i yield( num ) true # bingo - match found else false # no match found end end |
#match_metro(value) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/textutils/helper/value_helper.rb', line 80 def match_metro( value ) if value =~ /^metro:/ ## metro: city_key = value[6..-1] ## cut off metro: prefix city = City.find_by_key!( city_key ) # NB: parent city/metro required, that is, lookup w/ ! yield( city ) true # bingo - match found else false # no match found end end |
#match_metro_flag(value) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/textutils/helper/value_helper.rb', line 91 def match_metro_flag( value ) if value =~ /^metro$/ # metro(politan area) yield( true ) true # bingo - match found else false # no match found end end |
#match_metro_pop(value) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/textutils/helper/value_helper.rb', line 100 def match_metro_pop( value ) if value =~ /^m:/ # m: num = value[2..-1].gsub(/[ _]/, '').to_i # cut off m: prefix; allow space and _ in number yield( num ) true # bingo - match found else false # no match found end end |
#match_number(value) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/textutils/helper/value_helper.rb', line 151 def match_number( value ) ## numeric (nb: can use any _ or spaces inside digits e.g. 1_000_000 or 1 000 000) if value =~ /^([0-9][0-9 _]+[0-9])|([0-9]{1,2})$/ num = value.gsub(/[ _]/, '').to_i yield( num ) true # bingo - match found else false # no match found end end |
#match_region_for_country(value, country_id) ⇒ Object
fix/todo: use match_region_for_country! w/ !!! why? why not?
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/textutils/helper/value_helper.rb', line 52 def match_region_for_country( value, country_id ) ## NB: required country_id if value =~ /^region:/ ## region: region_key = value[7..-1] ## cut off region: prefix region = Region.find_by_key_and_country_id!( region_key, country_id ) yield( region ) true # bingo - match found elsif is_region?( value ) ## assume region code e.g. TX or N region = Region.find_by_key_and_country_id!( value.downcase, country_id ) yield( region ) true # bingo - match found else false # no match found end end |
#match_supra(value) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/textutils/helper/value_helper.rb', line 23 def match_supra( value ) if value =~ /^supra:/ # supra: country_key = value[6..-1] # cut off supra: prefix country = Country.find_by_key!( country_key ) yield( country ) true # bingo - match found else false # no match found end end |
#match_supra_flag(value) ⇒ Object
supranational (country)
34 35 36 37 38 39 40 41 |
# File 'lib/textutils/helper/value_helper.rb', line 34 def match_supra_flag( value ) # supranational (country) if value =~ /^supra$/ # supra(national) yield( true ) true # bingo - match found else false # no match found end end |
#match_website(value) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/textutils/helper/value_helper.rb', line 174 def match_website( value ) if is_website?( value ) # check for url/internet address e.g. www.ottakringer.at # fix: support more url format (e.g. w/o www. - look for .com .country code etc.) yield( value ) true # bingo - match found else false # no match found end end |
#match_year(value) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/textutils/helper/value_helper.rb', line 130 def match_year( value ) if is_year?( value ) # founded/established year e.g. 1776 yield( value.to_i ) true # bingo - match found else false # no match found end end |