Class: Terragona::Generic::CSVParser

Inherits:
Object
  • Object
show all
Defined in:
lib/terragona/generic.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ CSVParser

Returns a new instance of CSVParser.



6
7
8
9
10
11
12
13
# File 'lib/terragona/generic.rb', line 6

def initialize(args = {})
  filename = args[:csv_filename]

  abort "Missing CSV filename. Aborting." unless filename

  @csv = CSV.table(filename)
  @id_counter = 0
end

Instance Method Details

#search(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/terragona/generic.rb', line 14

def search(options = {})
  @id_counter +=1

  name = options[:name]

  points = []

  if name == 'CSV'
    children = []

    @csv.each {|p|
      points.push({:x => p[:x], :y => p[:y]})
      children.push({:name => p[:name]})
    }

    children.uniq!

    children_places = (children.count > 1 )? children : []
  else
    @csv.select{|row| row[:name] == name}.each {|p| points.push({:x => p[:x], :y => p[:y]})}
    children_places = []
  end

  {:children_places=>children_places, :points => points, :place_name=> name, :place_id=>@id_counter}
end