Skip to content

Commit 6ca2353

Browse files
committed
add destructive twins for most methods [see ievgrafov#32]
1 parent be19886 commit 6ca2353

File tree

7 files changed

+95
-1
lines changed

7 files changed

+95
-1
lines changed

lib/gnuplotrb/animation.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Animation < Multiplot
2727
alias_method :add_frame, :add_plot
2828
alias_method :add_frames, :add_plots
2929
alias_method :remove_frame, :remove_plot
30+
alias_method :update_frame!, :update_plot!
31+
alias_method :replace_frame!, :replace_plot!
32+
alias_method :add_frame!, :add_plot!
33+
alias_method :add_frames!, :add_plots!
34+
alias_method :remove_frame!, :remove_plot!
3035

3136
##
3237
# ====== Overview

lib/gnuplotrb/mixins/option_handling.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ def options(**options)
147147
end
148148
end
149149

150+
##
151+
# Destructive twin of #options
152+
def options!(**options)
153+
@options = @options ? @options.merge(options) : Hamster::Hash.new(options)
154+
self
155+
end
156+
150157
private
151158

152159
##
@@ -162,5 +169,14 @@ def option(key, *value)
162169
options(key => value)
163170
end
164171
end
172+
173+
def option!(key, *value)
174+
options!(key => value)
175+
end
176+
177+
def option=(key, *value)
178+
options!(key => value)
179+
@options[key]
180+
end
165181
end
166182
end

lib/gnuplotrb/mixins/plottable.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ def plot(*_)
4646
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
4747
def method_missing(meth_id, *args)
4848
meth = meth_id.id2name
49-
if meth[0..2] == 'to_'
49+
case
50+
when meth[0..2] == 'to_'
5051
term = meth[3..-1]
5152
super unless OptionHandling.valid_terminal?(term)
5253
to_specific_term(term, *args)
54+
when meth[-1] == '!'
55+
option!(meth[0..-2].to_sym, *args)
56+
when meth[-1] == '='
57+
option!(meth[0..-2].to_sym, *args)
58+
option(meth[0..-2].to_sym)
5359
else
5460
option(meth_id, *args)
5561
end

lib/gnuplotrb/multiplot.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ def update_plot(position = 0, **options)
8383

8484
alias_method :update, :update_plot
8585

86+
def update_plot!(position = 0, **options)
87+
return self unless block_given? if options.empty?
88+
replacement = @plots[position].options!(options)
89+
yield(replacement) if block_given?
90+
end
91+
92+
alias_method :update!, :update_plot!
93+
8694
##
8795
# ====== Overview
8896
# Create new Multiplot object where plot (Plot or Splot object)
@@ -100,6 +108,13 @@ def replace_plot(position = 0, plot)
100108

101109
alias_method :replace, :replace_plot
102110

111+
def replace_plot!(position = 0, plot)
112+
@plots = @plots.set(position, plot)
113+
end
114+
115+
alias_method :replace!, :replace_plot!
116+
alias_method :[]=, :replace_plot!
117+
103118
##
104119
# ====== Overview
105120
# Create new Multiplot with given *plots* added before plot at given *position*.
@@ -119,6 +134,14 @@ def add_plots(*plots)
119134
alias_method :<<, :add_plots
120135
alias_method :add, :add_plots
121136

137+
def add_plots!(*plots)
138+
plots.unshift(0) unless plots[0].is_a?(Numeric)
139+
@plots = @plots.insert(*plots)
140+
end
141+
142+
alias_method :add_plot!, :add_plots!
143+
alias_method :add!, :add_plots!
144+
122145
##
123146
# ====== Overview
124147
# Create new Multiplot without plot at given position
@@ -134,6 +157,12 @@ def remove_plot(position = -1)
134157

135158
alias_method :remove, :remove_plot
136159

160+
def remove_plot!(position = -1)
161+
@plots = @plots.delete_at(position)
162+
end
163+
164+
alias_method :remove!, :remove_plot!
165+
137166
##
138167
# ====== Overview
139168
# Equal to #plots[*args]

lib/gnuplotrb/plot.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def update_dataset(position = 0, data: nil, **options)
107107
new_ds.equal?(old_ds) ? self : replace_dataset(position, new_ds)
108108
end
109109

110+
def update_dataset!(position = 0, data: nil, **options)
111+
@datasets[position].update!(data, options)
112+
end
113+
110114
##
111115
# ====== Overview
112116
# Create new Plot object where dataset at *position* will
@@ -124,6 +128,12 @@ def replace_dataset(position = 0, dataset)
124128
self.class.new(@datasets.set(position, dataset_from_any(dataset)), @options)
125129
end
126130

131+
def replace_dataset!(position = 0, dataset)
132+
@datasets = @datasets.set(position, dataset_from_any(dataset))
133+
end
134+
135+
alias_method :[]=, :replace_dataset!
136+
127137
##
128138
# ====== Overview
129139
# Create new Plot object where given datasets will
@@ -139,13 +149,21 @@ def replace_dataset(position = 0, dataset)
139149
# cosx_and_sinx = sinx << ['cos(x)']
140150
def add_datasets(*datasets)
141151
datasets.map! { |ds| ds.is_a?(Numeric) ? ds : dataset_from_any(ds) }
152+
# first element is position where to add datasets
142153
datasets.unshift(0) unless datasets[0].is_a?(Numeric)
143154
self.class.new(@datasets.insert(*datasets), @options)
144155
end
145156

146157
alias_method :add_dataset, :add_datasets
147158
alias_method :<<, :add_datasets
148159

160+
def add_datasets(*datasets)
161+
datasets.map! { |ds| ds.is_a?(Numeric) ? ds : dataset_from_any(ds) }
162+
# first element is position where to add datasets
163+
datasets.unshift(0) unless datasets[0].is_a?(Numeric)
164+
@datasets = @datasets.insert(*datasets)
165+
end
166+
149167
##
150168
# ====== Overview
151169
# Create new Plot object where dataset at given position
@@ -161,6 +179,10 @@ def remove_dataset(position = -1)
161179
self.class.new(@datasets.delete_at(position), @options)
162180
end
163181

182+
def remove_dataset!(position = -1)
183+
@datasets = @datasets.delete_at(position)
184+
end
185+
164186
##
165187
# ====== Overview
166188
# The same as Plot#datasets[args]

lib/gnuplotrb/staff/datablock.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ def update(data)
4141
end
4242
end
4343

44+
def update!(data)
45+
data_str = data.to_gnuplot_points
46+
if @stored_in_file
47+
File.open(@file_name, 'a') { |f| f.puts "\n#{data_str}" }
48+
else
49+
@data = "#{@data}\n#{data_str}"
50+
end
51+
self
52+
end
53+
4454
##
4555
# ====== Overview
4656
# Returns quoted filename if datablock stored in file or outputs

lib/gnuplotrb/staff/dataset.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def update(data = nil, **options)
123123
end
124124
end
125125

126+
def update!(data = nil, **options)
127+
@data.update!(data) if data
128+
options!(options)
129+
self
130+
end
131+
126132
##
127133
# ====== Overview
128134
# Own implementation of #clone. Creates new Dataset if

0 commit comments

Comments
 (0)