Module: GnuplotRB::OptionHandling
- Included in:
- Plottable
- Defined in:
- lib/gnuplotrb/mixins/option_handling.rb
Overview
Overview
This module contains methods which are mixed into several classes to set, get and convert their options.
Constant Summary collapse
- QUOTED_OPTIONS =
Some values of options should be quoted to be read by gnuplot
TODO: update list with data from gnuplot documentation !!!
%w( title output xlabel x2label ylabel y2label clabel cblabel zlabel rgb font background format format_x format_y format_xy format_x2 format_y2 format_z format_cb timefmt )
Class Method Summary collapse
-
.option_to_string(key = nil, option) ⇒ Object
Overview Recursive function that converts Ruby option to gnuplot string ====== Arguments * key - name of option in gnuplot * option - an option that should be converted ====== Examples option_to_string([‘png’, size: [300, 300]]) #=> ‘png size 300,300’ option_to_string(xrange: 0..100) #=> ‘xrange [0:100]’ option_to_string(multiplot: true) #=> ‘multiplot’.
-
.ruby_class_to_gnuplot(option_object) ⇒ Object
Method for inner use.
-
.string_key(key) ⇒ Object
Replacement ‘_’ with ‘ ’ is made to allow passing several options with the same first word of key.
-
.valid_terminal?(terminal) ⇒ Boolean
Overview Check if given terminal available for use.
-
.validate_terminal_options(options) ⇒ Object
Overview Check if given options are valid for gnuplot.
Instance Method Summary collapse
-
#initialize(*_) ⇒ Object
You should implement #initialize in classes that use OptionsHelper.
-
#new_with_options(*_) ⇒ Object
You should implement #new_with_options in classes that use OptionsHelper.
-
#options(**options) ⇒ Object
Overview Create new Plot (or Dataset or Splot or Multiplot) object where current options are merged with given.
Class Method Details
.option_to_string(key = nil, option) ⇒ Object
Overview
Recursive function that converts Ruby option to gnuplot string
Arguments
-
key - name of option in gnuplot
-
option - an option that should be converted
Examples
option_to_string(['png', size: [300, 300]]) #=> 'png size 300,300'
option_to_string(xrange: 0..100) #=> 'xrange [0:100]'
option_to_string(multiplot: true) #=> 'multiplot'
53 54 55 56 57 58 59 60 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 53 def option_to_string(key = nil, option) return string_key(key) if !!option == option # check for boolean value = ruby_class_to_gnuplot(option) value = "\"#{value}\"" if QUOTED_OPTIONS.include?(key.to_s) ## :+ here is necessary, because using #{value} will remove quotes value = string_key(key) + value if key value end |
.ruby_class_to_gnuplot(option_object) ⇒ Object
Method for inner use. Needed to convert several ruby classes into value that should be piped to gnuplot.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 66 def ruby_class_to_gnuplot(option_object) case option_object when Array option_object.map { |el| option_to_string(el) } .join(option_object[0].is_a?(Numeric) ? ',' : ' ') when Hash option_object.map { |i_key, i_val| option_to_string(i_key, i_val) } .join(' ') when Range "[#{option_object.begin}:#{option_object.end}]" else option_object.to_s end end |
.string_key(key) ⇒ Object
Replacement ‘_’ with ‘ ’ is made to allow passing several options with the same first word of key. See issue #7 for more info.
39 40 41 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 39 def string_key(key) key.to_s.gsub(/_/) { ' ' } + ' ' end |
.valid_terminal?(terminal) ⇒ Boolean
Overview
Check if given terminal available for use.
Arguments
-
terminal - terminal to check (e.g. ‘png’, ‘qt’, ‘gif’)
86 87 88 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 86 def valid_terminal?(terminal) Settings.available_terminals.include?(terminal) end |
.validate_terminal_options(options) ⇒ Object
Overview
Check if given options are valid for gnuplot. Raises ArgumentError if invalid options found.
Arguments
-
options - Hash of options to check (e.g. ‘qt’, title: ‘Plot title’)
Now checks only terminal name.
99 100 101 102 103 104 105 106 107 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 99 def () terminal = [:term] return unless terminal terminal = terminal[0] if terminal.is_a?(Array) = 'Seems like your Gnuplot does not ' \ 'support that terminal, please see supported ' \ 'terminals with Settings::available_terminals' fail(ArgumentError, ) unless valid_terminal?(terminal) end |
Instance Method Details
#initialize(*_) ⇒ Object
You should implement #initialize in classes that use OptionsHelper
112 113 114 115 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 112 def initialize(*_) fail NotImplementedError, 'You should implement #initialize' \ ' in classes that use OptionsHelper!' end |
#new_with_options(*_) ⇒ Object
You should implement #new_with_options in classes that use OptionsHelper
119 120 121 122 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 119 def (*_) fail NotImplementedError, 'You should implement #new_with_options' \ ' in classes that use OptionsHelper!' end |
#options(**options) ⇒ Object
Overview
Create new Plot (or Dataset or Splot or Multiplot) object where current options are merged with given. If no options given it will just return existing set of options.
Arguments
-
options - options to add. If no options given, current options are returned.
Example
sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
sin_graph.plot
sin_graph_update = sin_graph.(title: 'Sin on [-10:10]', xrange: -10..10)
sin_graph_update.plot
# this may also be considered as
# sin_graph.title(...).xrange(...)
139 140 141 142 143 144 145 146 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 139 def (**) ||= Hamster::Hash.new if .empty? else (.merge()) end end |