= RubyLess

* http://zenadmin.org/546

== DESCRIPTION:

RubyLess is an interpreter for "safe ruby". The idea is to transform some "unsafe" ruby code into safe, type checked
ruby, eventually rewriting some variables or methods.


== GOALS:

1. give ruby scripting access to users without any security risk
2. rewrite variable names depending on compilation context
3. never raise runtime errors through compile time type checking and powerful nil handling

This library is based on Ruby2Ruby by Ryan Davis, thanks to him for sharing his work.

== SYNOPSIS:

For every class that will be involved in your RubyLess scripts, you need to declare safe methods with the 'safe_method' macro if
you want to enable methods from this class. You have to specify the return type of the method. If you have some methods that
return 'nil' instead of the declared output, you need to wrap your final ruby 'eval' with a rescue clause.

# signature is made of [method, arg_class, arg_class, ...]
class Node
include RubyLess::SafeClass
safe_method [:ancestor?, Node] => Boolean
end

# methods defined in helper

# global methods
include RubyLess::SafeClass
safe_method :prev => => Dummy, :method => 'previous', :nil => true
safe_method :node => lambda {|h| {:class => h.context[:node_class], :method => h.context[:node]}}
safe_method [:strftime, Time, String] => String
safe_method_for String, [:==, String] => Boolean
safe_method_for String, [:to_s] => String

You can also redefine 'safe_method_type' for any class or for the main helper in order to do some more complicated renaming. Note
also that you should add ':nil => true' declaration to any method that could return a nil value so that RubyLess can render
code that will not break during runtime (adding nil checking in the form of "foo ? foo.name : nil").

Or you can group all declarations in a single place with 'safe_method_for':

RubyLess::SafeClass.safe_method_for Dummy, :prev => => Dummy, :method => 'previous', :nil => true,
:node => lambda {|h| {:class => h.context[:node_class], :method => h.context[:node]}}

You can now parse some ruby code:

RubyLess.translate("!prev.ancestor?(main) && !node.ancestor?(main)", self)
=> "(not previous.ancestor?(@node) and not var1.ancestor?(@node))"

RubyLess.translate("id > 45 and (3 > -id or 3+3)", self)
=> "(var1.zip>45 and ((3>-var1.zip) or (3+3)))"

RubyLess.translate("strftime(now, '%Y')", self)
=> "strftime(Time.now, \"%Y\")"

RubyLess.translate("log_info(spouse, spouse.name)", self)
=> "(var1.spouse ? log_info(var1.spouse, var1.spouse.name) : nil)"

You can look at the tests for an idea of how to declare things. If you have more questions, ask on zena's mailing list:

http://zenadmin.org/community

== REQUIREMENTS:

* parse_tree

== INSTALL:

sudo gem install rubyless

== LICENSE:

(The MIT License)

Copyright (c) 2009 Gaspard Bucher

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.