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

Instance Method Summary collapse

Class Method Details

.option_to_string(key = nil, option) ⇒ Object

Recursive function that converts Ruby option to gnuplot string

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'

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 validate_terminal_options(options)
  terminal = options[:term]
  return unless terminal
  terminal = terminal[0] if terminal.is_a?(Array)
  message = 'Seems like your Gnuplot does not ' \
            "support that terminal (#{terminal}), please see " \
            'supported terminals with Settings::available_terminals'
  fail(ArgumentError, message) 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 new_with_options(*_)
  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.

Examples:

sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
sin_graph.plot
sin_graph_update = sin_graph.options(title: 'Sin on [-10:10]', xrange: -10..10)
sin_graph_update.plot
# sin_graph IS NOT affected

144
145
146
147
148
149
150
151
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 144

def options(**options)
  @options ||= Hamster::Hash.new
  if options.empty?
    @options
  else
    new_with_options(@options.merge(options))
  end
end

#options!(**options) ⇒ Dataset, ...

Update existing Plot (or Dataset or Splot or Multiplot) object with given options.

Examples:

sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
sin_graph.plot
sin_graph.options!(title: 'Sin on [-10:10]', xrange: -10..10)
sin_graph.plot
# second #plot call will plot not the same as first, sin_graph IS affected

164
165
166
167
# File 'lib/gnuplotrb/mixins/option_handling.rb', line 164

def options!(**options)
  @options = @options ? @options.merge(options) : Hamster::Hash.new(options)
  self
end