[ANN] Dependency 1.0.0

Discussion in 'Ruby' started by Tim Fletcher, Oct 20, 2006.

  1. Tim Fletcher

    Tim Fletcher Guest

    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
    Tim Fletcher, Oct 20, 2006
    #1
    1. Advertising

  2. Tim Fletcher wrote:
    > 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)
    ---
    price = 15 (was 10)
    ---
    price = 20 (was 15)
    ---
    selling at 25
    price = 25 (was 20)
    ---
    selling at 30
    price = 30 (was 25)
    ---


    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Oct 20, 2006
    #2
    1. Advertising

  3. Tim Fletcher

    Tim Fletcher Guest

    Tim Fletcher, Nov 3, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    522
  2. Joel VanderWerf
    Replies:
    6
    Views:
    108
    Joel VanderWerf
    Nov 15, 2004
  3. Nikolai Weibull

    [ANN] My dependency-injection library

    Nikolai Weibull, Sep 25, 2005, in forum: Ruby
    Replies:
    2
    Views:
    133
    Nikolai Weibull
    Sep 26, 2005
  4. Tony Arcieri
    Replies:
    0
    Views:
    137
    Tony Arcieri
    Oct 26, 2010
  5. manos
    Replies:
    0
    Views:
    218
    manos
    Oct 6, 2007
Loading...

Share This Page