WebTools Example, a demo for what a unified Ruby mirror API can offer you

The WebTools Sinatra application allows you to explore a Ruby application’s Classes, Modules, Methods, Constants, and Ancestors in a web browser, as well as run and debug code, or explore VM statistics and other info.

If running on MagLev, it will also allow you to explore GemStone/S Smalltalk code, and to examine detailed statistics about all processes connected to GemStone/S.

Setup

$ rvm use [maglev-head|rbx|jruby|ruby-head]
$ bundle install

### Temporary workaround on MagLev for https://github.com/MagLev/maglev/issues/58 ###
$ maglev-gem pristine rack

Viewing Ruby code

Run the WebTools Sinatra application

$ bundle exec rake

This will start WEBrick in your current window. Hit Ctl-C to stop.

Browse to localhost:9292/webtools, open a browser, click on a namespace, then click on a constant in that namespace. You will see instance methods. Select boxes show ancestors and implementors of the selected method in the hierarchy. To see class methods, switch to the “Class” tab.

Method source appears in the bottom pane, and time spent on the server/network/client in the status line. Depending on your Ruby, saving methods will work or not.

If you choose to open a workspace, you can run Ruby code. If the code has an error, a debugger will start. Depending on the capabilities of your Ruby, you can also inspect stack frames, trim the stack, step through methods and inspect arguments and locals. As a rule of thumb, almost nothing of this will work with MRI, because the MRI reflection is ad-hoc, infantile, and generally a prime example for an utter lack of – or disgregard for – engineering prinicples. Have a look at the Ruby mirrors project (github.com/timfel/rubymirrors) for more information.

MagLev specific demos

Add classes and modules from ActiveModel

WARNING: If you already have an application loaded into MagLev, do not run rake meta since it persists RubyGems and ActiveModel. Use WebTools to explore your application instead. If you run rake meta, run maglev force-reload afterwards to load an empty database.

$ rake meta

Open a new browser in WebTools. Note that AValidPerson now appears in the class list, as do a number of classes/modules from ActiveModel and ActiveSupport.

Click on the class AValidPerson and compare it with the source (meta_demo.rb). Some things to note:

  1. You can see all the methods generated by metaprogramming.

  2. You can see the file and line number of the code that generated each method. This makes it simpler to track down which part of ActiveWhatever is messing with your code.

  3. The initialize method is unchanged from the code in meta_demo.rb

  4. The instance method _callback_before_1 generated from $MAGLEV_HOME/lib/maglev/gems/1.8/gems/activesupport-3.0.5/lib/active_support/callbacks.rb shows the actual method name _callback_before_1 instead of #{method_name}.

Modify meta_demo.rb and view new generated methods

Edit the file meta_demo.rb. Add :age to both validates_presence_of and attr_accessor. The changed lines should look like

validates_presence_of :first_name, :last_name, :age
attr_accessor :first_name, :last_name, :age

Then

$ rake meta

Click on “Refresh View” in WebTools. AValidPerson will now include the new instance methods age and age=.

Add some data

$ rake demodata

Click on “Refresh View” in WebTools. Note that AAADemo now appears in the class list. Click on the class AAADemo and compare it with the source (demo_data.rb).

Viewing GemStone/S code, processes and statistics

The code for this example was inspired by the Smalltalk example found in $MAGLEV_HOME/gemstone/examples/www. The Smalltalk example code can be invoked from ruby (a demonstration of how to invoke Smalltalk from Ruby), via:

$ rake smalltalk

Then browse to localhost:8080/ and explore

Hit Ctl-C in the terminal window to stop.

Known problems

Oh, so many problems…

Contributing

  • Fork the project from github.com/MagLev/webtools

  • Start a feature or topic branch

  • Commit and push until you are happy with your contribution

  • Add some tests to ensure we don’t break things in a future release

  • Make sure to test with the latest MagLev

  • Send a pull request

MagLev Ruby code in this project is MIT licensed. See LICENSE.txt in this directory. By sending a pull request, you agree your contributed code is also MIT licensed.

This project includes a copy of CodeMirror, copyrighted by Marijn Haverbeke. See public/CodeMirror/LICENSE.