[ANN] Dependency 1.0.0

T

Tim Fletcher

A Smalltalk-inspired dependency mechanism for Ruby. Like this:

class StockWatcher
include Dependency::Methods
def initialize(stock)
stock.express_interest_in :price, :for => self, :send_back =>
:price_changed
end
def price_changed(old_price, stock)
# gets called whenever the stock object changes it's price
end
end

Completely unrelated to Rails' dependency code - similar to observers
though.

http://rubyforge.org/projects/dependency

gem install dependency
 
J

Joel VanderWerf

Tim said:
A Smalltalk-inspired dependency mechanism for Ruby. Like this:

class StockWatcher
include Dependency::Methods
def initialize(stock)
stock.express_interest_in :price, :for => self, :send_back =>
:price_changed
end
def price_changed(old_price, stock)
# gets called whenever the stock object changes it's price
end
end

Completely unrelated to Rails' dependency code - similar to observers
though.

http://rubyforge.org/projects/dependency

gem install dependency

You can do this with http://raa.ruby-lang.org/project/observable:

require 'observable'

include Observable
include Observable::Match

class Stock
extend Observable

observable :price

def initialize price
@price = price
end

def sell
puts "selling at #{price}"
end
end

class StockWatcher
GREATER_THAN = proc do |price|
MatchProc.new { |test_price| test_price > price }
end

def initialize(stock)
stock.when_price CHANGES do |price, old_price|
puts "price = #{price} (was #{old_price.inspect})"
end

stock.when_price GREATER_THAN[20] do
stock.sell
end
end
end

acme = Stock.new(10)
watcher = StockWatcher.new(acme)

10.step(30, 5) do |acme.price|
puts "---"
end

__END__

Output:

price = 10 (was nil)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top