The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Wee: Got Backtracking Right, Implemented Delegate Decoration

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Michael Neumann

Posts: 66
Nickname: backflash
Registered: May, 2003

Michael Neumann is fallen in Love with Ruby
Wee: Got Backtracking Right, Implemented Delegate Decoration Posted: Oct 28, 2004 4:44 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Michael Neumann.
Original Post: Wee: Got Backtracking Right, Implemented Delegate Decoration
Feed Title: Mike's Weblog
Feed URL: http://www.ntecs.de/blog-old/index.rss?cat=ruby&count=7
Feed Description: Blogging about Ruby and other interesting stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Michael Neumann
Latest Posts From Mike's Weblog

Advertisement
I finally got backtracking right. I fighted against a hard to find bug, and it turned out, that I was modifying a snapshot (e.g. arr = []; arr << 1). Method freeze is your friend! Now, backtracking is much more flexible. You can register individual objects for beeing backtracked (nothing new), but you can now have user-defined methods for taking or applying a snapshot of this object. For this purpose, you have to define take_snapshot and apply_snapshot, very similar to marshal_dump and marshal_load.

Backtracking decorations is now possible, too, but you still have to do this on your own. This is useful, so that "call/answer" calls can be undone correctly.

Decorations

A Delegate decoration is used to implement the call/answer mechanism. I'll explain this by demonstrating a little example:

  def render_content_on(r)
    r.anchor.action(:confirm_quit).with do
      r.text('Quit')
    end
  end

  def confirm_quit
    call MessageBox.new('Do you really want to quit?'), :quit
  end

  def quit(res)
    exit if res
  end

If you click on the anchor named Quit, method confirm_quit is called. This then replaces the calling component with a message box (or at least, only the message box is shown and can handle actions, not the calling component), with two buttons OK and Cancel. If you now click on one of them, the calling component is restored and it's method quit is invoked with the return code of the message box. How the message box can be implemented is shown below:

  class MessageBox < Wee::Component
    def initialize(msg)
      @msg = msg
    end

    def ok
      answer true
    end

    def cancel
      answer false
    end

    def render_content_on(r)
      r.text @msg
      r.form do
        r.submit_button.value('OK').action(:ok)
        r.space
        r.submit_button.value('Cancel').action(:cancel)
      end
    end
  end

In further versions of Wee, it will be possible to register an action and specify some default arguments. This would make the methods ok and cancel superfluous:

  r.submit_button.value('OK').action(:answer, true)
  r.space
  r.submit_button.value('Cancel').action(:answer, false)

Uhm, 12 minutes later... it's working ;-) Found by the way two other bugs.

So how is this call/answer mechanism working? Well, it really is only half a dozen lines of code:

  class Wee::Component
    attr_accessor :caller   # who is calling us?

    # call another component
    def call(component, return_method=nil)
      component.caller = [self, return_method]
      add_decoration(Wee::Delegate.new(component))
    end

    # return from a call
    def answer(*args)
      component, return_method = self.caller
      component.remove_first_decoration
      component.send(return_method, *args) if return_method
    end
  end

Get it! Try it!

So now, get Wee and try it out: viewcvs.ntecs.de/Wee/

Or play with it here.

Read: Wee: Got Backtracking Right, Implemented Delegate Decoration

Topic: Quick Pic from EuRuKo 2004 Previous Topic   Next Topic Topic: RubyConf2004 MP3 Files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use