Skip to content

Commit 1e67f03

Browse files
author
monkstone
committed
tidy up yaml examples
1 parent 684a8b0 commit 1e67f03

File tree

2 files changed

+53
-55
lines changed

2 files changed

+53
-55
lines changed

samples/processing_app/topics/advanced_data/load_save_struct_yaml.rb

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
attr_reader :bubble_data
1010

11-
1211
def setup
1312
size(640, 360)
1413
@bubble_data = BubbleData.new :bubbles
@@ -20,14 +19,14 @@ def draw
2019
bubble_data.display mouse_x, mouse_y
2120
end
2221

23-
2422
def mouse_pressed
2523
# create a new bubble instance, where mouse was clicked
2624
bubble_data.create_new_bubble(mouse_x, mouse_y)
2725
end
2826

2927
require 'forwardable'
3028

29+
# Enemerable class to store and display bubble data
3130
class BubbleData
3231
include Enumerable
3332
extend Forwardable
@@ -36,37 +35,37 @@ class BubbleData
3635
MAX_BUBBLE = 10
3736

3837
attr_reader :path, :bubble_array
39-
def initialize key
38+
def initialize(key)
4039
@bubble_array = []
4140
@key = key
4241
end
4342

44-
def create_new_bubble x, y
45-
self.add Bubble.new(x, y, rand(40..80), 'new label')
43+
def create_new_bubble(x, y)
44+
add Bubble.new(x, y, rand(40..80), 'new label')
4645
save_data
4746
load_data path
4847
end
4948

50-
def add bubble
49+
def add(bubble)
5150
self << bubble
52-
self.shift if self.size > MAX_BUBBLE
51+
shift if size > MAX_BUBBLE
5352
end
5453

55-
def load_data path
56-
@path = path
57-
yaml = Psych.load_file(path)
58-
# we are storing the data as an array of RubyStruct, in a hash with
59-
# a symbol as the key (the latter only to show we can, it makes no sense)
60-
data = yaml[@key]
61-
self.clear
62-
# iterate the bubble_data array, and populate the array of bubbles
63-
data.each do |pt|
64-
self.add Bubble.new(pt.x, pt.y, pt.diameter, pt.label)
65-
end
54+
def load_data(path)
55+
@path = path
56+
yaml = Psych.load_file(path)
57+
# we are storing the data as an array of RubyStruct, in a hash with
58+
# a symbol as the key (the latter only to show we can, it makes no sense)
59+
data = yaml[@key]
60+
clear
61+
# iterate the bubble_data array, and populate the array of bubbles
62+
data.each do |pt|
63+
add Bubble.new(pt.x, pt.y, pt.diameter, pt.label)
64+
end
6665
end
6766

68-
def display x, y
69-
self.each do |bubble|
67+
def display(x, y)
68+
each do |bubble|
7069
bubble.display
7170
bubble.rollover(x, y)
7271
end
@@ -75,10 +74,9 @@ def display x, y
7574
private
7675

7776
def save_data
78-
hash = { @key => self.map { |point| point.to_struct } }
77+
hash = { @key => map(&:to_struct) }
7978
yaml = hash.to_yaml
8079
# overwite existing 'struct_data.yaml'
8180
open(path, 'w:UTF-8') { |f| f.write(yaml) }
8281
end
83-
8482
end

samples/processing_app/topics/advanced_data/load_save_yaml.rb

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
# appropriately yaml.
55
# by Martin Prout after Dan Shiffman
66
# ###################################
7+
require 'forwardable'
8+
require 'psych'
79
load_library :bubble
810

911
attr_reader :bubble_data
1012

11-
def setup()
13+
def setup
1214
size(640, 360)
1315
# load data from file
1416
@bubble_data = BubbleData.new 'bubbles'
@@ -25,63 +27,61 @@ def mouse_pressed
2527
@bubble_data.create_new_bubble(mouse_x, mouse_y)
2628
end
2729

28-
class BubbleData
30+
# Bubble class can create and display bubble data from a yaml file
31+
class BubbleData
32+
extend Forwardable
33+
def_delegators(:@bubbles, :each, :<<, :size, :shift, :clear)
2934
include Enumerable
30-
35+
3136
MAX_BUBBLE = 10
32-
37+
3338
attr_reader :key, :path, :bubbles
34-
def initialize key
39+
def initialize(key)
3540
@key = key
3641
@bubbles = []
3742
end
38-
39-
def each &block
40-
bubbles.each &block
41-
end
42-
43-
def create_new_bubble x, y
44-
self.add Bubble.new(x, y, rand(40..80), 'new label')
45-
save_data
43+
44+
def create_new_bubble(x, y)
45+
add Bubble.new(x, y, rand(40..80), 'new label')
46+
save_data
4647
load_data path
4748
end
48-
49-
def display x, y
50-
self.each do |bubble|
49+
50+
def display(x, y)
51+
each do |bubble|
5152
bubble.display
5253
bubble.rollover(x, y)
5354
end
5455
end
55-
56+
5657
# @param path to yaml file
57-
58-
def load_data path
58+
def load_data(path)
5959
@path = path
6060
yaml = Psych.load_file('data/data.yml')
6161
data = yaml[key]
62-
bubbles.clear
62+
clear
6363
# iterate the bubble_data array, and create an array of bubbles
6464
data.each do |point|
65-
self.add Bubble.new(
65+
add Bubble.new(
6666
point['position']['x'],
6767
point['position']['y'],
6868
point['diameter'],
6969
point['label'])
7070
end
7171
end
72-
73-
74-
def add bubble
72+
73+
def add(bubble)
7574
bubbles << bubble
7675
bubbles.shift if bubbles.size > MAX_BUBBLE
77-
end
78-
79-
private
80-
76+
end
77+
78+
private
79+
8180
def save_data
82-
hash = { key => self.map{ |point| point.to_hash } }
83-
yaml = hash.to_yaml
84-
# overwite existing 'data.yaml'
85-
open('data/data.yml', 'w:UTF-8') { |f| f.write(yaml) }
86-
end
81+
hash = { key => map(&:to_hash) }
82+
# overwite existing 'data.yaml'
83+
open('data/data.yml', 'w:UTF-8') do |f|
84+
f.write(hash.to_yaml)
85+
end
86+
end
8787
end

0 commit comments

Comments
 (0)