Skip to content

Commit 62a7122

Browse files
committed
update docs of staff classes for YARD
1 parent 11011fe commit 62a7122

File tree

4 files changed

+205
-113
lines changed

4 files changed

+205
-113
lines changed

lib/gnuplotrb/staff/datablock.rb

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
module GnuplotRB
22
##
3-
# === Overview
43
# This class corresponds to points we want to plot. It may be
54
# stored in temporary file (to allow fast update) or inside
65
# "$DATA << EOD ... EOD" construction. Datablock stores data passed
76
# to constructor and keeps datablock name or path to file where it is stored.
87
class Datablock
98
##
10-
# ====== Parameters
11-
# * *data* - sequence of anything with +#to_gnuplot_points+ method.
12-
# * *stored_in_file* true here will force this datablock to store its data
9+
# @param data [#to_gnuplot_points] anything with #to_gnuplot_points method
10+
# @param stored_in_file [Boolean] true here will force this datablock to store its data
1311
# in temporary file.
1412
def initialize(data, stored_in_file = false)
1513
@stored_in_file = stored_in_file
@@ -25,12 +23,32 @@ def initialize(data, stored_in_file = false)
2523
end
2624

2725
##
28-
# ====== Overview
2926
# Instantiate one more Datablock with updated data
3027
# if data stored in here-doc. Append update to file
3128
# if data stored there.
32-
# ====== Parameters
33-
# * *data* - anything with +#to_gnuplot_points+ method
29+
#
30+
# @param data [#to_gnuplot_points] anything with #to_gnuplot_points method
31+
# @return [Datablock] self if data stored in file (see constructor)
32+
# @return [Datablock] new datablock with updated data otherwise
33+
#
34+
# @example
35+
# data = [[0, 1, 2, 3], [0, 1, 4, 9]] # y = x**2
36+
# db = Datablock.new(data, false)
37+
# update = [[4, 5], [16, 25]]
38+
# updated_db = db.update(update)
39+
# # now db and updated_db contain DIFFERENT data
40+
# # db - points with x from 0 up to 3
41+
# # updated_db - points with x from 0 to 5
42+
#
43+
# @example
44+
# data = [[0, 1, 2, 3], [0, 1, 4, 9]] # y = x**2
45+
# db = Datablock.new(data, true)
46+
# update = [[4, 5], [16, 25]]
47+
# updated_db = db.update(update)
48+
# # now db and updated_db contain THE SAME data
49+
# # because they linked with the same temporary file
50+
# # db - points with x from 0 up to 5
51+
# # updated_db - points with x from 0 to 5
3452
def update(data)
3553
data_str = data.to_gnuplot_points
3654
if @stored_in_file
@@ -41,6 +59,19 @@ def update(data)
4159
end
4260
end
4361

62+
##
63+
# Update existing Datablock with new data.
64+
# Destructive version of #update.
65+
#
66+
# @param data [#to_gnuplot_points] anything with #to_gnuplot_points method
67+
# @return [Datablock] self
68+
#
69+
# @example
70+
# data = [[0, 1, 2, 3], [0, 1, 4, 9]] # y = x**2
71+
# db = Datablock.new(data, false)
72+
# update = [[4, 5], [16, 25]]
73+
# db.update!(update)
74+
# # now db contains points with x from 0 up to 5
4475
def update!(data)
4576
data_str = data.to_gnuplot_points
4677
if @stored_in_file
@@ -52,10 +83,12 @@ def update!(data)
5283
end
5384

5485
##
55-
# ====== Overview
56-
# Returns quoted filename if datablock stored in file or outputs
57-
# datablock to gnuplot and returns its name otherwise.
58-
# * *gnuplot_term* should be given if datablock not stored in file.
86+
# Get quoted filename if datablock stored in file or output
87+
# datablock to gnuplot and return its name otherwise.
88+
#
89+
# @param gnuplot_term [Terminal] should be given if datablock not stored in file
90+
# @return [String] quoted filename if data stored in file (see contructor)
91+
# @return [String] Gnuplot's datablock name otherwise
5992
def name(gnuplot_term = nil)
6093
if @stored_in_file
6194
"'#{@file_name}'"
@@ -68,7 +101,6 @@ def name(gnuplot_term = nil)
68101
alias_method :to_s, :name
69102

70103
##
71-
# ====== Overview
72104
# Overridden #clone. Since datablock which store data
73105
# in temporary files should not be cloned (otherwise it will cause
74106
# double attempt to delete file), this #clone returns self for such

lib/gnuplotrb/staff/dataset.rb

Lines changed: 90 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
module GnuplotRB
22
##
3-
# === Overview
43
# Dataset keeps control of Datablock or String (some math functions like
54
# this 'x*sin(x)' or filename) and options related to original dataset
65
# in gnuplot (with, title, using etc).
76
#
8-
# === Options
7+
# == Options
98
# Dataset options are explained in
109
# {gnuplot docs}[http://www.gnuplot.info/docs_5.0/gnuplot.pdf](pp. 80-101).
1110
# Several common options:
@@ -28,6 +27,8 @@ class Dataset
2827
# Order is significant for some options
2928
OPTION_ORDER = %w(index using axes title)
3029

30+
private_constant :OPTION_ORDER
31+
3132
##
3233
# Hash of init handlers for data given in
3334
# different containers.
@@ -41,41 +42,39 @@ class Dataset
4142
) if defined? Daru
4243

4344
##
44-
# ====== Overview
45-
# Creates new dataset out of given string with
46-
# math function or filename. If *data* isn't a string
47-
# it will create datablock to store data.
48-
# ====== Parameters
49-
# * *data* - String, Datablock or something acceptable by
50-
# Datablock.new as data (e.g. [x,y] where x and y are arrays)
51-
# * *options* - hash of options specific for gnuplot
52-
# dataset, and some special options ('file: true' will
53-
# make data to be stored inside temporary file).
54-
# ====== Examples
55-
# Math function:
45+
# Create new dataset out of given string with math function or filename.
46+
# If *data* isn't a string it will create datablock to store data.
47+
#
48+
# @param data [String, Datablock, #to_gnuplot_points] String, Datablock or something acceptable
49+
# by Datablock.new as data (e.g. [x,y] where x and y are arrays)
50+
# @param options [Hash] options specific for gnuplot
51+
# dataset (see Dataset top level doc), and some special options ('file: true' will
52+
# make data to be stored inside temporary file)
53+
#
54+
# @example Math function:
5655
# Dataset.new('x*sin(x)', with: 'lines', lw: 4)
57-
# File with points:
56+
# @example File with points:
5857
# Dataset.new('points.data', with: 'lines', title: 'Points from file')
59-
# Some data (creates datablock stored in memory):
58+
# @example Some data (creates datablock stored in memory):
6059
# x = (0..5000).to_a
6160
# y = x.map {|xx| xx*xx }
6261
# points = [x, y]
6362
# Dataset.new(points, with: 'points', title: 'Points')
64-
# The same data but datablock stores it in temp file:
63+
# @example The same data but datablock stores it in temp file:
6564
# Dataset.new(points, with: 'points', title: 'Points', file: true)
6665
def initialize(data, **options)
6766
# run method by name
6867
send(INIT_HANDLERS[data.class], data, options)
6968
end
7069

7170
##
72-
# ====== Overview
73-
# Converts Dataset to string containing gnuplot dataset.
74-
# ====== Parameters
75-
# * *terminal* - must be given if data given as Datablock and
71+
# Convert Dataset to string containing gnuplot dataset.
72+
#
73+
# @param terminal [Terminal] must be given if data given as Datablock and
7674
# it does not use temp file so data should be piped out
77-
# to gnuplot via terminal before use.
78-
# ====== Examples
75+
# to gnuplot via terminal before use
76+
# @return [String] gnuplot dataset
77+
# @example
7978
# Dataset.new('points.data', with: 'lines', title: 'Points from file').to_s
8079
# #=> "'points.data' with lines title 'Points form file'"
8180
# Dataset.new(points, with: 'points', title: 'Points').to_s
@@ -85,26 +84,29 @@ def to_s(terminal = nil)
8584
end
8685

8786
##
88-
# ====== Overview
89-
# Creates new dataset with updated data (given
90-
# data is appended to existing) and merged options.
87+
# Create new dataset with updated data and merged options.
88+
#
89+
# Given data is appended to existing.
9190
# Data is updated only if Dataset stores it in Datablock.
9291
# Method does nothing if no options given and data isn't stored
9392
# in in-memory Datablock.
94-
# ====== Parameters
95-
# * *data* - data to append to existing
96-
# * *options* - hash to merge with existing options
97-
# ====== Examples
98-
# Updating dataset with Math formula or filename given:
93+
#
94+
# @param data [#to_gnuplot_points] data to append to existing
95+
# @param options [Hash] new options to merge with existing options
96+
# @return self if dataset corresponds to math formula or file
97+
# (filename or temporary file if datablock)
98+
# @return [Dataset] new dataset if data is stored in 'in-memory' Datablock
99+
# @example Updating dataset with Math formula or filename given:
99100
# dataset = Dataset.new('file.data')
100101
# dataset.update(data: 'asd')
101102
# #=> nothing updated
102103
# dataset.update(data: 'asd', title: 'File')
103104
# #=> Dataset.new('file.data', title: 'File')
104-
# Updating dataset with data stored in Datablock:
105+
# @example Updating dataset with data stored in Datablock (in-memory):
105106
# in_memory_points = Dataset.new(points, title: 'Old one')
106107
# in_memory_points.update(data: some_update, title: 'Updated')
107108
# #=> Dataset.new(points + some_update, title: 'Updated')
109+
# @example Updating dataset with data stored in Datablock (in-file):
108110
# temp_file_points = Dataset.new(points, title: 'Old one', file: true)
109111
# temp_file_points.update(data: some_update)
110112
# #=> data updated but no new dataset created
@@ -123,14 +125,44 @@ def update(data = nil, **options)
123125
end
124126
end
125127

128+
##
129+
# Update Dataset with new data and options.
130+
#
131+
# Given data is appended to existing.
132+
# Data is updated only if Dataset stores it in Datablock.
133+
# Method does nothing if no options given and data isn't stored
134+
# in in-memory Datablock.
135+
#
136+
# @param data [#to_gnuplot_points] data to append to existing
137+
# @param options [Hash] new options to merge with existing options
138+
# @return self
139+
# @example Updating dataset with Math formula or filename given:
140+
# dataset = Dataset.new('file.data')
141+
# dataset.update!(data: 'asd')
142+
# #=> nothing updated
143+
# dataset.update!(data: 'asd', title: 'File')
144+
# dataset.title
145+
# #=> 'File' # data isn't updated
146+
# @example Updating dataset with data stored in Datablock (in-memory):
147+
# in_memory_points = Dataset.new(points, title: 'Old one')
148+
# in_memory_points.update!(data: some_update, title: 'Updated')
149+
# in_memory_points.data
150+
# #=> points + some_update
151+
# in_memory_points.title
152+
# #=> 'Updated'
153+
# @example Updating dataset with data stored in Datablock (in-file):
154+
# temp_file_points = Dataset.new(points, title: 'Old one', file: true)
155+
# temp_file_points.update!(data: some_update)
156+
# #=> data updated but no new dataset created
157+
# temp_file_points.update!(data: some_update, title: 'Updated')
158+
# #=> data and options updated
126159
def update!(data = nil, **options)
127160
@data.update!(data) if data
128161
options!(options)
129162
self
130163
end
131164

132165
##
133-
# ====== Overview
134166
# Own implementation of #clone. Creates new Dataset if
135167
# data stored in datablock and calls super otherwise.
136168
def clone
@@ -142,11 +174,13 @@ def clone
142174
end
143175

144176
##
145-
# ====== Overview
146-
# Creates new Plot object with only one Dataset given - self.
177+
# Create new Plot object with only one Dataset given - self.
147178
# Calls #plot on created Plot. All arguments given to this #plot
148179
# will be sent to Plot#plot instead.
149-
# ====== Example
180+
# @param args sequence of arguments all of which will be passed to Plot#plot,
181+
# see docs there
182+
# @return [Plot] new Plot object with only one Dataset - self
183+
# @example
150184
# sin = Dataset.new('sin(x)')
151185
# sin.plot(term: [qt, size: [300, 300]])
152186
# #=> shows qt window 300x300 with sin(x)
@@ -159,13 +193,13 @@ def plot(*args)
159193
private
160194

161195
##
162-
# ====== Overview
163-
# Creates new dataset with existing options merged with
196+
# Create new dataset with existing options merged with
164197
# the given ones. Does nothing if no options given.
165-
# ====== Parameters
166-
# * *options* - hash to merge with existing options
167-
# ====== Examples
168-
# Updating dataset with Math formula or filename given:
198+
#
199+
# @param options [Hash] new options to merge with existing options
200+
# @return [Dataset] self if options empty
201+
# @return [Dataset] new Dataset with updated options otherwise
202+
# @example Updating dataset with Math formula or filename given:
169203
# dataset = Dataset.new('file.data')
170204
# dataset.update_options(title: 'File')
171205
# #=> Dataset.new('file.data', title: 'File')
@@ -178,32 +212,37 @@ def update_options(**options)
178212
end
179213

180214
##
181-
# ====== Overview
182215
# Create string from own options
216+
# @return [String] options converted to Gnuplot format
183217
def options_to_string
184218
options.sort_by { |key, _| OPTION_ORDER.find_index(key.to_s) || 999 }
185219
.map { |key, value| OptionHandling.option_to_string(key, value) }
186220
.join(' ')
187221
end
188222

189223
##
190-
# Needed by OptionHandling to create new object when
191-
# options are changed.
224+
# Needed by OptionHandling to create new object when options are changed.
192225
def new_with_options(options)
193226
self.class.new(@data, options)
194227
end
195228

229+
##
230+
# Initialize Dataset from given String
196231
def init_string(data, options)
197232
@type, @data = File.exist?(data) ? [:datafile, "'#{data}'"] : [:math_function, data.clone]
198233
@options = Hamster.hash(options)
199234
end
200235

236+
##
237+
# Initialize Dataset from given Datablock
201238
def init_dblock(data, options)
202239
@type = :datablock
203240
@data = data.clone
204241
@options = Hamster.hash(options)
205242
end
206243

244+
##
245+
# Create new value for 'using' option based on column count
207246
def get_daru_columns(data, cnt)
208247
new_opt = (2..cnt).to_a.join(':')
209248
if data.index[0].is_a?(DateTime) || data.index[0].is_a?(Numeric)
@@ -213,6 +252,8 @@ def get_daru_columns(data, cnt)
213252
end
214253
end
215254

255+
##
256+
# Initialize Dataset from given Daru::DataFrame
216257
def init_daru_frame(data, options)
217258
options[:title] ||= data.name
218259
if options[:using]
@@ -229,12 +270,16 @@ def init_daru_frame(data, options)
229270
init_default(data, options)
230271
end
231272

273+
##
274+
# Initialize Dataset from given Daru::Vector
232275
def init_daru_vector(data, options)
233276
options[:using] ||= get_daru_columns(data, 2)
234277
options[:title] ||= data.name
235278
init_default(data, options)
236279
end
237280

281+
##
282+
# Initialize Dataset from given data with #to_gnuplot_points method
238283
def init_default(data, file: false, **options)
239284
@type = :datablock
240285
@data = Datablock.new(data, file)

0 commit comments

Comments
 (0)