Class: GnuplotRB::Datablock
- Inherits:
-
Object
- Object
- GnuplotRB::Datablock
- Defined in:
- lib/gnuplotrb/staff/datablock.rb
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
-
#clone ⇒ Object
Overridden #clone.
-
#initialize(data, stored_in_file = false) ⇒ Datablock
constructor
A new instance of Datablock.
-
#name(gnuplot_term = nil) ⇒ String
(also: #to_s)
Get quoted filename if datablock stored in file or output datablock to gnuplot and return its name otherwise.
-
#update(data) ⇒ Datablock
Instantiate one more Datablock with updated data if data stored in here-doc.
-
#update!(data) ⇒ Datablock
Update existing Datablock with new data.
Constructor Details
#initialize(data, stored_in_file = false) ⇒ Datablock
Returns a new instance of Datablock.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gnuplotrb/staff/datablock.rb', line 12 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.gnuplot_tmpname('tmp_data') 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
#clone ⇒ Object
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.
108 109 110 |
# File 'lib/gnuplotrb/staff/datablock.rb', line 108 def clone @stored_in_file ? self : super end |
#name(gnuplot_term = nil) ⇒ String Also known as: to_s
Get quoted filename if datablock stored in file or output datablock to gnuplot and return its name otherwise.
92 93 94 95 96 97 98 99 |
# File 'lib/gnuplotrb/staff/datablock.rb', line 92 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) ⇒ Datablock
Instantiate one more Datablock with updated data if data stored in here-doc. Append update to file if data stored there.
52 53 54 55 56 57 58 59 60 |
# File 'lib/gnuplotrb/staff/datablock.rb', line 52 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 |
#update!(data) ⇒ Datablock
Update existing Datablock with new data. Destructive version of #update.
75 76 77 78 79 80 81 82 83 |
# File 'lib/gnuplotrb/staff/datablock.rb', line 75 def update!(data) data_str = data.to_gnuplot_points if @stored_in_file File.open(@file_name, 'a') { |f| f.puts "\n#{data_str}" } else @data = "#{@data}\n#{data_str}" end self end |