Module: GnuplotRB::OptionHandling
- Included in:
- Plottable
- Defined in:
- lib/gnuplotrb/mixins/option_handling.rb
Overview
This module contains methods which are mixed into several classes to set, get and convert their options.
Class Method Summary collapse
-
.option_to_string(key = nil, option) ⇒ Object
Recursive function that converts Ruby option to gnuplot string.
-
.ruby_class_to_gnuplot(option_object) ⇒ Object
Method for inner use.
-
.string_key(key) ⇒ String
Replace ‘_’ with ‘ ’ is made to allow passing several options with the same first word of key.
-
.valid_terminal?(terminal) ⇒ Boolean
Check if given terminal available for use.
-
.validate_terminal_options(options) ⇒ Object
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) ⇒ Dataset, ...
Create new Plot (or Dataset or Splot or Multiplot) object where current options are merged with given.
-
#options!(**options) ⇒ Dataset, ...
Update existing Plot (or Dataset or Splot or Multiplot) object with given options.
Class Method Details
.option_to_string(key = nil, option) ⇒ Object
Recursive function that converts Ruby option to gnuplot string
59 60 61 62 63 64 65 66 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 59 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 73 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) ⇒ String
Replace ‘_’ with ‘ ’ is made to allow passing several options with the same first word of key. See issue #7 for more info.
43 44 45 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 43 def string_key(key) key.to_s.gsub(/_/) { ' ' } + ' ' end |
.valid_terminal?(terminal) ⇒ Boolean
Check if given terminal available for use.
93 94 95 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 93 def valid_terminal?(terminal) Settings.available_terminals.include?(terminal) end |
.validate_terminal_options(options) ⇒ Object
Check if given options are valid for gnuplot. Raises ArgumentError if invalid options found. Now checks only terminal name.
103 104 105 106 107 108 109 110 111 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 103 def () terminal = [:term] return unless terminal terminal = terminal[0] if terminal.is_a?(Array) = 'Seems like your Gnuplot does not ' \ "support that terminal (#{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
117 118 119 120 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 117 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
125 126 127 128 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 125 def (*_) fail NotImplementedError, 'You should implement #new_with_options' \ ' in classes that use OptionsHelper!' end |
#options(**options) ⇒ Dataset, ...
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.
144 145 146 147 148 149 150 151 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 144 def (**) @options ||= Hamster::Hash.new if .empty? @options else (@options.merge()) end end |
#options!(**options) ⇒ Dataset, ...
Update existing Plot (or Dataset or Splot or Multiplot) object with given options.
164 165 166 167 |
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 164 def (**) @options = @options ? @options.merge() : Hamster::Hash.new() self end |