Sha256: f81b13772cda3a2b3925af50225ea9902c6a894d27faf193fc7c4f66af49a20d
Contents?: true
Size: 1.8 KB
Versions: 5
Compression:
Stored size: 1.8 KB
Contents
###################################### # Yet another examples of reading and # writing to some form of markup, # appropriately yaml using ruby structs # by Martin Prout after Dan Shiffman #################################### load_library :bubble attr_reader :bubble_data def setup size(640, 360) @bubble_data = BubbleData.new :bubbles bubble_data.load_data 'data/struct_data.yml' end def draw background 255 bubble_data.display mouse_x, mouse_y end def mouse_pressed # create a new bubble instance, where mouse was clicked bubble_data.create_new_bubble(mouse_x, mouse_y) end require 'forwardable' class BubbleData include Enumerable extend Forwardable def_delegators(:@bubble_array, :clear, :each, :<<, :shift, :size) MAX_BUBBLE = 10 attr_reader :path, :bubble_array def initialize key @bubble_array = [] @key = key end def create_new_bubble x, y self.add Bubble.new(x, y, rand(40 .. 80), 'new label') save_data load_data path end def add bubble self << bubble self.shift if self.size > MAX_BUBBLE end def load_data path @path = path yaml = Psych.load_file(path) # we are storing the data as an array of RubyStruct, in a hash with # a symbol as the key (the latter only to show we can, it makes no sense) data = yaml[@key] self.clear # iterate the bubble_data array, and populate the array of bubbles data.each do |pt| self.add Bubble.new(pt.x, pt.y, pt.diameter, pt.label) end end def display x, y self.each do |bubble| bubble.display bubble.rollover(x, y) end end private def save_data hash = { @key => self.map { |point| point.to_struct } } yaml = hash.to_yaml # overwite existing 'struct_data.yaml' open(path, 'w:UTF-8') { |f| f.write(yaml) } end end
Version data entries
5 entries across 5 versions & 1 rubygems