Class: GnuplotRB::Datablock

Inherits:
Object
  • Object
show all
Defined in:
lib/gnuplotrb/staff/datablock.rb

Overview

Overview

This class corresponds to points we want to plot. It may be stored in temporary file (to allow fast update) or inside “$DATA << EOD … EOD” construction. Datablock stores data passed to constructor and keeps datablock name or path to file where it is stored.

Instance Method Summary collapse

Constructor Details

#initialize(data, stored_in_file = false) ⇒ Datablock

Parameters
  • data - sequence of anything with #to_gnuplot_points method.

  • stored_in_file true here will force this datablock to store its data in temporary file.


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gnuplotrb/staff/datablock.rb', line 14

def initialize(data, stored_in_file = false)
  @stored_in_file = stored_in_file
  data_str = data.to_gnuplot_points
  if @stored_in_file
    @file_name = Dir::Tmpname.make_tmpname('tmp_data', 0)
    File.write(@file_name, data_str)
    name = File.join(Dir.pwd, @file_name)
    ObjectSpace.define_finalizer(self, proc { File.delete(name) })
  else
    @data = data_str
  end
end

Instance Method Details

#cloneObject

Overview

Overridden #clone. Since datablock which store data in temporary files should not be cloned (otherwise it will cause double attempt to delete file), this #clone returns self for such cases. For other cases it just calls default #clone.


66
67
68
# File 'lib/gnuplotrb/staff/datablock.rb', line 66

def clone
  @stored_in_file ? self : super
end

#name(gnuplot_term = nil) ⇒ Object Also known as: to_s

Overview

Returns quoted filename if datablock stored in file or outputs datablock to gnuplot and returns its name otherwise.

  • gnuplot_term should be given if datablock not stored in file.


49
50
51
52
53
54
55
56
# File 'lib/gnuplotrb/staff/datablock.rb', line 49

def name(gnuplot_term = nil)
  if @stored_in_file
    "'#{@file_name}'"
  else
    fail(ArgumentError, 'No terminal given to output datablock') unless gnuplot_term
    gnuplot_term.store_datablock(@data)
  end
end

#update(data) ⇒ Object

Overview

Instantiate one more Datablock with updated data if data stored in here-doc. Append update to file if data stored there.

Parameters
  • data - anything with #to_gnuplot_points method


34
35
36
37
38
39
40
41
42
# File 'lib/gnuplotrb/staff/datablock.rb', line 34

def update(data)
  data_str = data.to_gnuplot_points
  if @stored_in_file
    File.open(@file_name, 'a') { |f| f.puts "\n#{data_str}" }
    self
  else
    Datablock.new("#{@data}\n#{data_str}", false)
  end
end