Class: ValuesReader
- Inherits:
-
Object
- Object
- ValuesReader
- Defined in:
- lib/worlddb/readers/values_reader.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #each_line ⇒ Object
-
#initialize(logger, path, more_values = {}) ⇒ ValuesReader
constructor
A new instance of ValuesReader.
Constructor Details
#initialize(logger, path, more_values = {}) ⇒ ValuesReader
Returns a new instance of ValuesReader.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/worlddb/readers/values_reader.rb', line 5 def initialize( logger, path, more_values={} ) ## todo: check - can we make logger=nil a default arg too? if logger.nil? @logger = Logger.new(STDOUT) @logger.level = Logger::INFO else @logger = logger end @path = path @more_values = more_values @data = File.read_utf8( @path ) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
21 22 23 |
# File 'lib/worlddb/readers/values_reader.rb', line 21 def logger @logger end |
Instance Method Details
#each_line ⇒ Object
23 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 |
# File 'lib/worlddb/readers/values_reader.rb', line 23 def each_line @data.each_line do |line| if 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 # remove leading and trailing whitespace line = line.strip puts "line: >>#{line}<<" values = line.split(',') # pass 1) remove leading and trailing whitespace for values values = values.map { |value| value.strip } # pass 2) remove comment columns values = values.select do |value| if value =~ /^#/ ## start with # treat it as a comment column; e.g. remove it puts " removing column with value >>#{value}<<" false else true end end # pass 3) remove comments inside columns values = values.map do |value| value = value.sub( /\s+#.+$/, '' ) value end puts " values: >>#{values.join('<< >>')}<<" attribs = { key: values[0] } ## title (split of optional synonyms) # e.g. FC Bayern Muenchen|Bayern Muenchen|Bayern titles = values[1].split('|') attribs[ :title ] = titles[0] ## add optional synonyms attribs[ :synonyms ] = titles[1..-1].join('|') if titles.size > 1 attribs = attribs.merge( @more_values ) # e.g. merge country_id and other defaults if present yield( attribs, values[2..-1] ) end # each lines end |