Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Declarative relations between object attributes.
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
COPYING
README
cell_slicing.rb
cells.rb
model-view.rb
motor-control.rb
test.rb

README

ruby-cells: Declarative relations between object attributes.
============================================================

ruby-cells was inspired by the Cells extension to the Common Lisp Object System
(CLOS), which in turn was inspired by spreadsheet applications. See
http://common-lisp.net/project/cells/

Basically, by loading the cells.rb library, you can define special attributes
called cells (shamelessly ripping the standard Cells example ;-):

class Motor
	cell :status
end

Like attr_accessors, this will define getter and setter methods for @status.
Hower, these accessor methods differ from the standard ones in that they allow
you to establish dynamic relationships between cells:

class Motor
	cell :temperature
	def initialize
		self.temperature = 0
		calculate :status do
			if self.temperature < 100
				:on
			else
				:off
			end
		end
	end
end

That is, when creating a new Motor instance, the block associated with calculate
is run and @status is assigned the value :on (since temperature was previously
initialized to 0). Nothing fancy so far. However, whenever the value of
temperature is changed (assuming its setter method is used), the block will
be automatically re-run and @status will be updated accordingly.

Also, you can register custom observers for changes to particular cells:

motor = Motor.new
motor.observe :status do |old, new|
	puts "Motor is now #{new}."
end

A slightly more sophisticated example is provided in motor-control.rb.

For an idea of how to apply this to model/view design (in the context of Qt),
see model-view.rb.

Something went wrong with that request. Please try again.