Class: SimpleCSV

Inherits:
SimpleOutput::SimpleOutputPlugin show all
Defined in:
lib/simplecsv.rb

Instance Method Summary collapse

Methods inherited from SimpleOutput::SimpleOutputPlugin

#advance_series, #annotate, #appendHash, #appendPoints, #appendXY, #append_series_name, #getDataAsPoints, #getDataAsXY, #getSeriesHashes, #newData, #new_data_check, #setHash, #setOptions, #setPoints, #setXData, #setXY, #setYData, #set_y_callback, #translate_name

Constructor Details

#initialize(file_template) ⇒ SimpleCSV

Returns a new instance of SimpleCSV.



21
22
23
24
25
# File 'lib/simplecsv.rb', line 21

def initialize(file_template)
   super()
   @filename = file_template
   @series_next = 0
end

Instance Method Details

#append_callback(x, y, name, options) ⇒ Object



54
55
56
57
58
# File 'lib/simplecsv.rb', line 54

def append_callback(x,y,name,options)
  if !.has_key?(name)
    new_data_callback(name)
  end
end

#check_title(name, options) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/simplecsv.rb', line 36

def check_title(name, options)
  if options.has_key?('series')
    [name]['series_titles'] << options['series']
  else
    [name]['series_titles'] << "#{@metadata[name]['ylabel']}#{@series_next}"
    @series_next += 1
  end
end

#new_data_callback(name) ⇒ Object



49
50
51
52
# File 'lib/simplecsv.rb', line 49

def new_data_callback(name)
  name = translate_name(name)
  [name] = {'xlabel' => "#{name}_x", 'ylabel' => "#{name}_y", 'series_titles' => []}
end

#options_callback(options) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/simplecsv.rb', line 27

def options_callback(options)
  if options.has_key?('xlabel')
    [@current_name]['xlabel'] = options['xlabel']
  end
  if options.has_key?('ylabel')
    [@current_name]['ylabel'] = options['ylabel']
  end
end

#saveObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/simplecsv.rb', line 60

def save()
  data = self.getDataAsXY()
  data.each do |set_name, series|
    CSV.open("#{@filename}_#{set_name}.csv", "wb") do |csv|
      xlabel = [set_name]['xlabel']
      
      series.each_with_index do |values, i|
        values[0].unshift(xlabel)
        csv << values[0]
        ylabel = [set_name]['series_titles'].empty? ? [set_name]['ylabel'] : [set_name]['series_titles'][i] 
        values[1].unshift(ylabel)
        csv << values[1]
      end
    end
  end
end

#set_x_callback(data, name, options) ⇒ Object



45
46
47
# File 'lib/simplecsv.rb', line 45

def set_x_callback(data, name, options)
  check_title(name, options)
end