Skip to content

Commit 684a8b0

Browse files
author
monkstone
committed
tidy up json example
1 parent d4cc459 commit 684a8b0

File tree

2 files changed

+70
-72
lines changed

2 files changed

+70
-72
lines changed
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
{
22
"bubbles": [
3-
{
4-
"position": {
5-
"x": 160,
6-
"y": 103
7-
},
8-
"diameter": 43.19838,
9-
"label": "Happy"
3+
{
4+
"position": {
5+
"x": 160,
6+
"y": 103
107
},
11-
{
12-
"position": {
13-
"x": 372,
14-
"y": 137
15-
},
16-
"diameter": 52.42526,
17-
"label": "Sad"
8+
"diameter": 43.19838,
9+
"label": "Happy"
10+
},
11+
{
12+
"position": {
13+
"x": 372,
14+
"y": 137
1815
},
19-
{
20-
"position": {
21-
"x": 273,
22-
"y": 235
23-
},
24-
"diameter": 61.14072,
25-
"label": "Joyous"
16+
"diameter": 52.42526,
17+
"label": "Sad"
18+
},
19+
{
20+
"position": {
21+
"x": 273,
22+
"y": 235
2623
},
27-
{
28-
"position": {
29-
"x": 121,
30-
"y": 179
31-
},
32-
"diameter": 44.758068,
33-
"label": "Melancholy"
34-
}
35-
]
36-
}
24+
"diameter": 61.14072,
25+
"label": "Joyous"
26+
},
27+
{
28+
"position": {
29+
"x": 121,
30+
"y": 179
31+
},
32+
"diameter": 44.758068,
33+
"label": "Melancholy"
34+
}
35+
]
36+
}
Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# This example demonstrates how easily 'sketch data' can be retrieved from a json file
2-
# in ruby-processing. Note this sketch re-uses the Bubble class from the bubble library.
3-
# The BubbleData class, can load, store and create instances of Bubble (and request them
4-
# to display and/or show their label, when 'mouse over').
1+
# This example demonstrates how 'sketch data' can be retrieved from a json file
2+
# in ruby-processing. Note this sketch re-uses the Bubble class from the bubble
3+
# library. The BubbleData class, can load, store and create instances of Bubble
4+
# (and request them to display and/or show their label, when 'mouse over').
55
# @author Martin Prout, after Daniel Shiffmans version for processing
6-
#
6+
require 'forwardable'
77
require 'json'
88

99
load_library :bubble
@@ -28,66 +28,64 @@ def mouse_pressed
2828
bubble_data.create_new_bubble(mouse_x, mouse_y)
2929
end
3030

31-
class BubbleData
31+
# This class can load store and create instances of Bubble
32+
class BubbleData
33+
extend Forwardable
34+
def_delegators(:@bubbles, :each, :<<, :size, :shift, :clear)
3235
include Enumerable
33-
36+
3437
MAX_BUBBLE = 10
35-
38+
3639
attr_reader :key, :path, :bubbles
37-
40+
3841
# @param key String for top level hash
39-
40-
def initialize key
42+
43+
def initialize(key)
4144
@key = key
4245
@bubbles = []
4346
end
44-
45-
def each &block
46-
bubbles.each &block
47-
end
48-
49-
def create_new_bubble x, y
50-
self.add Bubble.new(x, y, rand(40..80), 'new label')
51-
save_data
47+
48+
def create_new_bubble(x, y)
49+
add Bubble.new(x, y, rand(40..80), 'new label')
50+
save_data
5251
load_data path
5352
end
54-
55-
def display x, y
56-
self.each do |bubble|
53+
54+
def display(x, y)
55+
each do |bubble|
5756
bubble.display
5857
bubble.rollover(x, y)
5958
end
6059
end
61-
60+
6261
# @param path to json file
63-
64-
def load_data path
62+
63+
def load_data(path)
6564
@path = path
66-
source_string = open(path, 'r'){ |file| file.read }
67-
data = JSON.parse(source_string)[key]
68-
bubbles.clear
65+
file = File.read(path)
66+
data = JSON.parse(file)[key]
67+
clear
6968
# iterate the bubble_data array, and create an array of bubbles
7069
data.each do |point|
71-
self.add Bubble.new(
70+
add Bubble.new(
7271
point['position']['x'],
7372
point['position']['y'],
7473
point['diameter'],
7574
point['label'])
7675
end
7776
end
78-
79-
def add bubble
77+
78+
def add(bubble)
8079
bubbles << bubble
81-
bubbles.shift if bubbles.size > MAX_BUBBLE
82-
end
83-
84-
private
85-
80+
shift if size > MAX_BUBBLE
81+
end
82+
83+
private
84+
8685
def save_data
87-
hash = { key => self.map{ |point| point.to_hash } }
88-
json = JSON.pretty_generate(hash) # generate pretty output
89-
open(path, 'w') { |f| f.write(json) }
86+
hash = { key => map(&:to_hash) }
87+
File.open(path, 'w') do |json|
88+
json.write(JSON.pretty_generate(hash)) # generate pretty output
89+
end
9090
end
91-
9291
end
93-

0 commit comments

Comments
 (0)