Class: ValuesReader
- Inherits:
-
Object
- Object
- ValuesReader
- Includes:
- LogUtils::Logging
- Defined in:
- lib/textutils/reader/values_reader.rb
Instance Method Summary collapse
-
#each_line ⇒ Object
support multi line records.
- #each_line_old_single_line_records_only ⇒ Object
-
#initialize(path, more_values = {}) ⇒ ValuesReader
constructor
A new instance of ValuesReader.
- #title_to_key(title) ⇒ Object
Constructor Details
#initialize(path, more_values = {}) ⇒ ValuesReader
Returns a new instance of ValuesReader.
7 8 9 10 11 12 13 |
# File 'lib/textutils/reader/values_reader.rb', line 7 def initialize( path, more_values={} ) @path = path @more_values = more_values @data = File.read_utf8( @path ) end |
Instance Method Details
#each_line ⇒ Object
support multi line records
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/textutils/reader/values_reader.rb', line 24 def each_line # support multi line records inside_line = false # todo: find a better name? e.g. line_found? attribs = {} # rename to new_attributes? more_cols = [] # rename to more_values? @data.each_line do |line| ## allow alternative comment lines ## e.g. -- comment or ## % comment ## why? # might get used by markdown for marking headers, for example ## NB: for now alternative comment lines not allowed as end of line style e.g ## some data, more data -- comment here if line =~ /^\s*#/ || line =~ /^\s*--/ || line =~ /^\s*%/ # skip komments and do NOT copy to result (keep comments secret!) logger.debug 'skipping comment line' next end if line =~ /^\s*$/ # kommentar oder leerzeile überspringen logger.debug 'skipping blank line' next end # pass 1) remove possible trailing eol comment ## e.g -> nyc, New York # Sample EOL Comment Here (with or without commas,,,,) ## becomes -> nyc, New York line = line.sub( /\s+#.+$/, '' ) # pass 2) remove leading and trailing whitespace line = line.strip ### check for multiline record ## must start with key and colon e.g. brands: if line =~ /^[a-z][a-z0-9.]*[a-z0-9]:/ # NB: every additional line is one value e.g. city:wien, etc. # allows you to use any chars logger.debug " multi-line record - add key-value >#{line}<" more_cols.unshift( line.dup ) # add value upfront to array (first value); lets us keep (optional) tags as last entry; fix!! see valuereaderEx v2 next else # NB: new record clears/ends multi-line record if inside_line # check if we already processed a line? if yes; yield last line yield( attribs, more_cols ) attribs = {} more_cols = [] end inside_line = true end ### guard escaped commas (e.g. \,) line = line.gsub( '\,', '@commma@' ) ## use generic separator (allow us to configure separator) line = line.gsub( ',', '@sep@') ## restore escaped commas (before split) line = line.gsub( '@commma@', ',' ) logger.debug "line: >>#{line}<<" values = line.split( '@sep@' ) # pass 1) remove leading and trailing whitespace for values values = values.map { |value| value.strip } ##### todo remove support of comment column? (NB: must NOT include commas) # pass 2) remove comment columns values = values.select do |value| if value =~ /^#/ ## start with # treat it as a comment column; e.g. remove it logger.debug " removing column with value >>#{value}<<" false else true end end logger.debug " values: >>#{values.join('<< >>')}<<" ### 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_cols = values[2..-1] else key_col = '<auto>' title_col = values[0] more_cols = values[1..-1] end attribs = {} ## 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 = title_to_key( titles[0] ) logger.debug " autogen key >#{key_col}< from title >#{titles[0]}<, textutils version #{TextUtils::VERSION}" end attribs[ :key ] = key_col attribs = attribs.merge( @more_values ) # e.g. merge country_id and other defaults if present end # each lines # do NOT forget to yield last line (if present/processed) if inside_line yield( attribs, more_cols ) end end |
#each_line_old_single_line_records_only ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/textutils/reader/values_reader.rb', line 173 def each_line_old_single_line_records_only @data.each_line do |line| ## allow alternative comment lines ## e.g. -- comment or ## % comment ## why? # might get used by markdown for marking headers, for example ## NB: for now alternative comment lines not allowed as end of line style e.g ## some data, more data -- comment here if line =~ /^\s*#/ || line =~ /^\s*--/ || line =~ /^\s*%/ # skip komments and do NOT copy to result (keep comments secret!) logger.debug 'skipping comment line' next end if line =~ /^\s*$/ # kommentar oder leerzeile überspringen logger.debug 'skipping blank line' next end # pass 1) remove possible trailing eol comment ## e.g -> nyc, New York # Sample EOL Comment Here (with or without commas,,,,) ## becomes -> nyc, New York line = line.sub( /\s+#.+$/, '' ) # pass 2) remove leading and trailing whitespace line = line.strip ### guard escaped commas (e.g. \,) line = line.gsub( '\,', '@commma@' ) ## use generic separator (allow us to configure separator) line = line.gsub( ',', '@sep@') ## restore escaped commas (before split) line = line.gsub( '@commma@', ',' ) logger.debug "line: >>#{line}<<" values = line.split( '@sep@' ) # pass 1) remove leading and trailing whitespace for values values = values.map { |value| value.strip } ##### todo remove support of comment column? (NB: must NOT include commas) # pass 2) remove comment columns values = values.select do |value| if value =~ /^#/ ## start with # treat it as a comment column; e.g. remove it logger.debug " removing column with value >>#{value}<<" false else true end end logger.debug " values: >>#{values.join('<< >>')}<<" ### 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_cols = values[2..-1] else key_col = '<auto>' title_col = values[0] more_cols = values[1..-1] end attribs = {} ## 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 = title_to_key( titles[0] ) logger.debug " autogen key >#{key_col}< from title >#{titles[0]}<, textutils version #{TextUtils::VERSION}" end attribs[ :key ] = key_col attribs = attribs.merge( @more_values ) # e.g. merge country_id and other defaults if present yield( attribs, more_cols ) end # each lines end |
#title_to_key(title) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/textutils/reader/values_reader.rb', line 292 def title_to_key( title ) ## NB: downcase does NOT work for accented chars (thus, include in alternatives) key = title.downcase ### remove optional english translation in square brackets ([]) e.g. Wien [Vienna] key = key.gsub( /\[.+\]/, '' ) ## remove optional longer title part in () e.g. Las Palmas (de Gran Canaria), Palma (de Mallorca) key = key.gsub( /\(.+\)/, '' ) ## remove optional longer title part in {} e.g. Ottakringer {Bio} or {Alkoholfrei} ## todo: use for autotags? e.g. {Bio} => bio key = key.gsub( /\{.+\}/, '' ) ## remove all whitespace and punctuation key = key.gsub( /[ \t_\-\.()\[\]'"\/]/, '' ) ## remove special chars (e.g. %°&) key = key.gsub( /[%&°]/, '' ) ## turn accented char into ascii look alike if possible ## ## todo: add some more ## see http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references for more ## todo: add unicode codepoint name alternatives = [ ['ß', 'ss'], ['æ', 'ae'], ['ä', 'ae'], ['ā', 'a' ], # e.g. Liepājas ['á', 'a' ], # e.g. Bogotá, Králové ['ã', 'a' ], # e.g São Paulo ['ă', 'a' ], # e.g. Chișinău ['â', 'a' ], # e.g Goiânia ['å', 'a' ], # e.g. Vålerenga ['ą', 'a' ], # e.g. Śląsk ['ç', 'c' ], # e.g. São Gonçalo, Iguaçu, Neftçi ['ć', 'c' ], # e.g. Budućnost ['č', 'c' ], # e.g. Tradiční, Výčepní ['é', 'e' ], # e.g. Vélez, Králové ['è', 'e' ], # e.g. Rivières ['ê', 'e' ], # e.g. Grêmio ['ě', 'e' ], # e.g. Budějovice ['ĕ', 'e' ], # e.g. Svĕtlý ['ė', 'e' ], # e.g. Vėtra ['ë', 'e' ], # e.g. Skënderbeu ['ğ', 'g' ], # e.g. Qarabağ ['ì', 'i' ], # e.g. Potosì ['í', 'i' ], # e.g. Ústí ['ł', 'l' ], # e.g. Wisła, Wrocław ['ñ', 'n' ], # e.g. Porteño ['ň', 'n' ], # e.g. Plzeň, Třeboň ['ö', 'oe'], ['ő', 'o' ], # e.g. Győri ['ó', 'o' ], # e.g. Colón, Łódź, Kraków ['õ', 'o' ], # e.g. Nõmme ['ø', 'o' ], # e.g. Fuglafjørdur, København ['ř', 'r' ], # e.g. Třeboň ['ș', 's' ], # e.g. Chișinău, București ['ş', 's' ], # e.g. Beşiktaş ['š', 's' ], # e.g. Košice ['ť', 't' ], # e.g. Měšťan ['ü', 'ue'], ['ú', 'u' ], # e.g. Fútbol ['ū', 'u' ], # e.g. Sūduva ['ů', 'u' ], # e.g. Sládkův ['ı', 'u' ], # e.g. Bakı # use u?? (Baku) why-why not? ['ý', 'y' ], # e.g. Nefitrovaný ['ź', 'z' ], # e.g. Łódź ['ž', 'z' ], # e.g. Domžale, Petržalka ['Č', 'c' ], # e.g. České ['İ', 'i' ], # e.g. İnter ['Í', 'i' ], # e.g. ÍBV ['Ł', 'l' ], # e.g. Łódź ['Ö', 'oe' ], # e.g. Örebro ['Ř', 'r' ], # e.g. Řezák ['Ś', 's' ], # e.g. Śląsk ['Š', 's' ], # e.g. MŠK ['Ş', 's' ], # e.g. Şüvälan ['Ú', 'u' ], # e.g. Ústí, Újpest ['Ž', 'z' ] # e.g. Žilina ] alternatives.each do |alt| key = key.gsub( alt[0], alt[1] ) end key end |