Skip to content

Commit 11011fe

Browse files
committed
update mixins doc for YARD
1 parent a9ca5aa commit 11011fe

File tree

3 files changed

+148
-60
lines changed

3 files changed

+148
-60
lines changed

lib/gnuplotrb/mixins/error_handling.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class GnuplotError < ArgumentError
99
# handle errors from its stderr.
1010
module ErrorHandling
1111
##
12-
# ====== Overview
1312
# Check if there were errors in previous commands.
1413
# Throws GnuplotError in case of any errors.
1514
def check_errors
@@ -28,7 +27,6 @@ def check_errors
2827
private
2928

3029
##
31-
# ====== Overview
3230
# Start new thread that will read stderr given as stream
3331
# and add errors into @err_array.
3432
def handle_stderr(stream)

lib/gnuplotrb/mixins/option_handling.rb

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
module GnuplotRB
22
##
3-
# ====== Overview
43
# This module contains methods which are mixed into several classes
54
# to set, get and convert their options.
65
module OptionHandling
76
class << self
8-
# Some values of options should be quoted to be read by gnuplot
7+
# Some values of options should be quoted to be read by gnuplot properly
98
#
10-
# TODO: update list with data from gnuplot documentation !!!
9+
# @todo update list with data from gnuplot documentation !!!
1110
QUOTED_OPTIONS = %w(
1211
title
1312
output
@@ -34,24 +33,29 @@ class << self
3433
dashtype
3534
)
3635

36+
private_constant :QUOTED_OPTIONS
37+
3738
##
38-
# Replacement '_' with ' ' is made to allow passing several options
39-
# with the same first word of key.
40-
# See issue #7 for more info.
39+
# Replace '_' with ' ' is made to allow passing several options
40+
# with the same first word of key. See issue #7 for more info.
41+
# @param key [Symbol, String] key to modify
42+
# @return [String] given key with '_' replaced with ' '
4143
def string_key(key)
4244
key.to_s.gsub(/_/) { ' ' } + ' '
4345
end
4446

4547
##
46-
# ====== Overview
4748
# Recursive function that converts Ruby option to gnuplot string
48-
# ====== Arguments
49-
# * *key* - name of option in gnuplot
50-
# * *option* - an option that should be converted
51-
# ====== Examples
52-
# option_to_string(['png', size: [300, 300]]) #=> 'png size 300,300'
53-
# option_to_string(xrange: 0..100) #=> 'xrange [0:100]'
54-
# option_to_string(multiplot: true) #=> 'multiplot'
49+
#
50+
# @param key [Symbol] name of option in gnuplot
51+
# @param option an option that should be converted
52+
# @example
53+
# option_to_string(['png', size: [300, 300]])
54+
# #=> 'png size 300,300'
55+
# option_to_string(xrange: 0..100)
56+
# #=> 'xrange [0:100]'
57+
# option_to_string(multiplot: true)
58+
# #=> 'multiplot'
5559
def option_to_string(key = nil, option)
5660
return string_key(key) if !!option == option # check for boolean
5761
value = ruby_class_to_gnuplot(option)
@@ -62,6 +66,7 @@ def option_to_string(key = nil, option)
6266
end
6367

6468
##
69+
# @private
6570
# Method for inner use.
6671
# Needed to convert several ruby classes into
6772
# value that should be piped to gnuplot.
@@ -81,23 +86,20 @@ def ruby_class_to_gnuplot(option_object)
8186
end
8287

8388
##
84-
# ====== Overview
8589
# Check if given terminal available for use.
86-
# ====== Arguments
87-
# * *terminal* - terminal to check (e.g. 'png', 'qt', 'gif')
90+
#
91+
# @param terminal [String] terminal to check (e.g. 'png', 'qt', 'gif')
92+
# @return [Boolean] true or false
8893
def valid_terminal?(terminal)
8994
Settings.available_terminals.include?(terminal)
9095
end
9196

9297
##
93-
# ====== Overview
9498
# Check if given options are valid for gnuplot.
9599
# Raises ArgumentError if invalid options found.
96-
# ====== Arguments
97-
# * *options* - Hash of options to check
98-
# (e.g. {term: 'qt', title: 'Plot title'})
99-
#
100100
# Now checks only terminal name.
101+
#
102+
# @param options [Hash] options to check (e.g. "{ term: 'qt', title: 'Plot title' }")
101103
def validate_terminal_options(options)
102104
terminal = options[:term]
103105
return unless terminal
@@ -110,34 +112,35 @@ def validate_terminal_options(options)
110112
end
111113

112114
##
115+
# @private
113116
# You should implement #initialize in classes that use OptionsHelper
114117
def initialize(*_)
115118
fail NotImplementedError, 'You should implement #initialize' \
116119
' in classes that use OptionsHelper!'
117120
end
118121

119122
##
123+
# @private
120124
# You should implement #new_with_options in classes that use OptionsHelper
121125
def new_with_options(*_)
122126
fail NotImplementedError, 'You should implement #new_with_options' \
123127
' in classes that use OptionsHelper!'
124128
end
125129

126130
##
127-
# ====== Overview
128131
# Create new Plot (or Dataset or Splot or Multiplot) object where current
129132
# options are merged with given. If no options
130133
# given it will just return existing set of options.
131-
# ====== Arguments
132-
# * *options* - options to add. If no options given, current
133-
# options are returned.
134-
# ====== Example
134+
#
135+
# @param options [Hash] options to add
136+
# @return [Dataset, Splot, Multiplot] new object created with given options
137+
# @return [Hamster::Hash] current options if given options empty
138+
# @example
135139
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
136140
# sin_graph.plot
137141
# sin_graph_update = sin_graph.options(title: 'Sin on [-10:10]', xrange: -10..10)
138142
# sin_graph_update.plot
139-
# # this may also be considered as
140-
# # sin_graph.title(...).xrange(...)
143+
# # sin_graph IS NOT affected
141144
def options(**options)
142145
@options ||= Hamster::Hash.new
143146
if options.empty?
@@ -148,7 +151,16 @@ def options(**options)
148151
end
149152

150153
##
151-
# Destructive twin of #options
154+
# Update existing Plot (or Dataset or Splot or Multiplot) object with given options.
155+
#
156+
# @param options [Hash] options to add
157+
# @return [Dataset, Splot, Multiplot] self
158+
# @example
159+
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
160+
# sin_graph.plot
161+
# sin_graph.options!(title: 'Sin on [-10:10]', xrange: -10..10)
162+
# sin_graph.plot
163+
# # second #plot call will plot not the same as first, sin_graph IS affected
152164
def options!(**options)
153165
@options = @options ? @options.merge(options) : Hamster::Hash.new(options)
154166
self
@@ -157,9 +169,8 @@ def options!(**options)
157169
private
158170

159171
##
160-
# ====== Arguments
161-
# * *key* - [Symbol] - option key
162-
# * *value* - anything treated as options value in gnuplot gem
172+
# Return current option value if no value given. Create new
173+
# object with given option set if value given.
163174
def option(key, *value)
164175
if value.empty?
165176
value = options[key]
@@ -170,6 +181,8 @@ def option(key, *value)
170181
end
171182
end
172183

184+
##
185+
# Just set an option.
173186
def option!(key, *value)
174187
options!(key => value)
175188
end

lib/gnuplotrb/mixins/plottable.rb

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,39 @@ module Plottable
77
include OptionHandling
88

99
##
10+
# @private
1011
# You should implement #plot in classes that are Plottable
1112
def plot(*_)
1213
fail NotImplementedError, 'You should implement #plot in classes that are Plottable!'
1314
end
1415

1516
##
16-
# ====== Overview
1717
# In this gem #method_missing is used both to handle
1818
# options and to handle plotting to specific terminal.
1919
#
20-
# ====== Options handling
21-
# ======= Overview
20+
# == Options handling
21+
# === Overview
2222
# You may set options using #option_name(option_value) method.
2323
# A new object will be constructed with selected option set.
2424
# And finally you can get current value of any option using
2525
# #options_name without arguments.
26-
# ======= Arguments
26+
# === Arguments
2727
# * *option_value* - value to set an option. If none given
2828
# method will just return current option's value
29-
# ======= Examples
29+
# === Examples
3030
# plot = Splot.new
3131
# new_plot = plot.title('Awesome plot')
3232
# plot.title #=> nil
3333
# new_plot.title #=> 'Awesome plot'
3434
#
35-
# ====== Plotting to specific term
36-
# ======= Overview
35+
# == Plotting to specific term
36+
# === Overview
3737
# Gnuplot offers possibility to output graphics to many image formats.
3838
# The easiest way to to so is to use #to_<plot_name> methods.
39-
# ======= Arguments
39+
# === Arguments
4040
# * *options* - set of options related to terminal (size, font etc).
4141
# Be careful, some terminals have their own specific options.
42-
# ======= Examples
42+
# === Examples
4343
# # font options specific for png term
4444
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
4545
# # font options specific for svg term
@@ -62,8 +62,9 @@ def method_missing(meth_id, *args)
6262
end
6363

6464
##
65-
# Returns true foe existing methods and
66-
# #to_<term_name> when name is a valid terminal type.
65+
# @return [true] for existing methods and
66+
# #to_|term_name| when name is a valid terminal type.
67+
# @return [false] otherwise
6768
def respond_to?(meth_id)
6869
# Next line is here to force iRuby use #to_iruby
6970
# instead of #to_svg.
@@ -74,8 +75,7 @@ def respond_to?(meth_id)
7475
end
7576

7677
##
77-
# This method is used to embed plottable objects
78-
# into iRuby notebooks. There is
78+
# This method is used to embed plottable objects into iRuby notebooks. There is
7979
# {a notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/basic_usage.ipynb]
8080
# with examples of its usage.
8181
def to_iruby
@@ -92,20 +92,16 @@ def to_iruby
9292
end
9393

9494
##
95-
# :call-seq:
96-
# to_png('file.png') -> creates file with plot
97-
# to_svg -> svg file contents
98-
# to_canvas('plot.html', size: [300,300]) -> creates file with plot
99-
#
100-
# ====== Overview
101-
# Method which outputs plot to specific terminal (possibly some file).
95+
# @private
96+
# Output plot to specific terminal (possibly some file).
10297
# Explicit use should be avoided. This method is called from #method_missing
10398
# when it handles method names like #to_png(options).
104-
# ====== Arguments
105-
# * *path* - path to output file, if none given it will output to temp file
99+
#
100+
# @param trminal [String] terminal name ('png', 'svg' etc)
101+
# @param path [String] path to output file, if none given it will output to temp file
106102
# and then read it and return binary contents of file
107-
# * *options* - used in #plot
108-
# ====== Examples
103+
# @param options [Hash] used in #plot
104+
# @example
109105
# ## plot here may be Plot, Splot, Multiplot or any other plottable class
110106
# plot.to_png('./result.png', size: [300, 500])
111107
# contents = plot.to_svg(size: [100, 100])
@@ -123,9 +119,90 @@ def to_specific_term(terminal, path = nil, **options)
123119
end
124120

125121
##
126-
# Returns terminal object linked with this Plottable object.
122+
# @return [Terminal] terminal object linked with this Plottable object
127123
def own_terminal
128124
@terminal ||= Terminal.new
129125
end
126+
127+
##
128+
# @!method xrange(value = nil)
129+
# @!method yrange(value = nil)
130+
# @!method title(value = nil)
131+
# @!method option_name(value = nil)
132+
# Clone existing object and set new options value in created one or just return
133+
# existing value if nil given.
134+
#
135+
# Method is handled by #method_missing.
136+
#
137+
# You may set options using #option_name(option_value) method.
138+
# A new object will be constructed with selected option set.
139+
# And finally you can get current value of any option using
140+
# #options_name without arguments.
141+
#
142+
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
143+
#
144+
# @param value new value for option
145+
# @return new object with option_name set to *value* if value given
146+
# @return old option value if no value given
147+
#
148+
# @example
149+
# plot = Splot.new
150+
# new_plot = plot.title('Awesome plot')
151+
# plot.title #=> nil
152+
# new_plot.title #=> 'Awesome plot'
153+
154+
##
155+
# @!method xrange!(value)
156+
# @!method yrange!(value)
157+
# @!method title!(value)
158+
# @!method option_name!(value)
159+
# Set value for an option.
160+
#
161+
# Method is handled by #method_missing.
162+
#
163+
# You may set options using obj.option_name!(option_value) or
164+
# obj.option_name = option_value methods.
165+
#
166+
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
167+
#
168+
# @param value new value for option
169+
# @return self
170+
#
171+
# @example
172+
# plot = Splot.new
173+
# plot.title #=> nil
174+
# plot.title!('Awesome plot')
175+
# plot.title #=> 'Awesome plot'
176+
#
177+
# @example
178+
# plot = Splot.new
179+
# plot.title #=> nil
180+
# plot.title = 'Awesome plot'
181+
# plot.title #=> 'Awesome plot'
182+
183+
##
184+
# @!method to_png(path = nil, **options)
185+
# @!method to_svg(path = nil, **options)
186+
# @!method to_gif(path = nil, **options)
187+
# @!method to_canvas(path = nil, **options)
188+
# Output to plot to according image format.
189+
#
190+
# All of #to_|terminal_name| methods are handled with #method_missing.
191+
#
192+
# Gnuplot offers possibility to output graphics to many image formats.
193+
# The easiest way to to so is to use #to_<plot_name> methods.
194+
#
195+
# @param path [String] path to save plot file to.
196+
# @param options [Hash] specific terminal options like 'size',
197+
# 'font' etc
198+
#
199+
# @return [String] contents of plotted file unless path given
200+
# @return self if path given
201+
#
202+
# @example
203+
# # font options specific for png term
204+
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
205+
# # font options specific for svg term
206+
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
130207
end
131208
end

0 commit comments

Comments
 (0)