Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Saturday 21 September 2013

Reading and writing RubyStructs to yaml ruby processing

Since ruby 1.9.3 it is possible to read and write RubyStructs to YAML (using Psych), this makes for a more efficient way of storing and restoring data, whilst maintaining human readability see included yaml file. It is essential that the Struct is declared, and this is easiest done in our bubble library (see below).
######################################
# Yet another examples of reading and
# writing to some form of markup,
# appropriatetly yaml using ruby structs 
# by Martin Prout after Dan Shiffman
# ###################################
load_library :bubble

attr_reader :bubbles, :bubble_data


def setup()
  size(640, 360)
  # load data from file
  load_data
end

def draw
  background 255
  bubbles.each do|bubble|
    bubble.display
    bubble.rollover(mouse_x, mouse_y)
  end
end

def load_data
  yaml = Psych.load_file("data/struct_data.yaml")
  # 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[:bubbles]
  @bubbles = []
  # iterate the bubble_data array, and populate the array of bubbles
  data.each do |pt|
    bubbles << Bubble.new(pt.x, pt.y, pt.diameter, pt.label)
  end
end

def save_data
  # demonstrate how easy it is to create yaml object from a hash in ruby
  yaml = bubble_data.to_hash.to_yaml
  # overwite existing 'data.yaml' 
  open("data/struct_data.yaml", 'w') {|f| f.write(yaml) }
end

def mouse_pressed
  # create a new bubble instance, where mouse was clicked
  @bubble_data = BubbleData.new bubbles
  @bubble_data.add_bubble(Bubble.new(mouse_x, mouse_y, rand(40 .. 80), "new label"))
  save_data
  # reload the yaml data from the freshly created file
  load_data
end


class BubbleData
  attr_reader :data
  def initialize data
    @data = data
  end

  def add_bubble bubble
    data << bubble
    if (data.size > 10)
    # Delete the oldest bubble
      data.shift
    end
  end

  # Using symbol as hash key for a change.  Thus demonstrating only to show we can store 
  # more complex data strucures quite readily.
  def to_hash
    {bubbles: data.map{|point| point.to_struct}}
  end

end


The bubble library "library/bubble/bubble.rb"
# The bubble library, include BubbleStruct

class Bubble
  include Processing::Proxy

  attr_reader :x, :y, :diameter, :name, :over

  # Create  the Bubble
  def initialize(x, y, diameter, name)
    @x = x
    @y = y
    @diameter = diameter
    @name = name
    @over = false
  end

  # Checking if mouse is over the Bubble
  def rollover(px, py)
    d = dist(px,py,x,y)
    @over = (d < diameter/2)? true : false
  end

  # Display the Bubble
  def display
    stroke(0)
    stroke_weight(2)
    noFill
    ellipse(x,y,diameter,diameter)
    if (over)
      fill(0)
      text_align(CENTER)
      text(name, x, y + diameter/2 + 20)
    end
  end

  def to_struct
    BubbleStruct.new(x, y, diameter, name)
  end
end

BubbleStruct = Struct.new(:x, :y, :diameter, :label)

The data file "struct_data.yaml"
---
:bubbles:
- !ruby/struct:BubbleStruct
  x: 160
  y: 103
  diameter: 43.2
  label: Happy
- !ruby/struct:BubbleStruct
  x: 372
  y: 137
  diameter: 52.4
  label: Sad
- !ruby/struct:BubbleStruct
  x: 273
  y: 235
  diameter: 61.0
  label: Joyous
- !ruby/struct:BubbleStruct
  x: 121
  y: 179
  diameter: 44.7
  label: Melancholy

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2