ARGIBLE: A dynamic method argument resolver for Ruby

by Michael Ward

DESCRIPTION:

A gem that allows for automatic resolution of a methods argument values.

SYNOPSIS:

Argible was created to simplify Actions within your Ruby on Rails Controllers. Argible annotated action methods will allow your actions to define argument. These argument names will be used in conjunction with request parameters to automatically resolve of these argument values.

INSTALL:

You can download Argible from here or install it with the following command.

$ gem install argible

To use as a Rails plug-in unpack the gem into your applications vendor/plugin directory:

$ gem unpack argible

USAGE:

Have a look at the following Controller:

class FoobarController < ApplicationController

  argible
  def action_one(alpha, beta)
    ...
  end

  argible(:date => Date.method(:parse)) # process argument value for 'date' by passing it to Date#parse
  def action_two(alpha, date)
    ...
  end

  argible(:date => lambda {|v| Date.parse(v) } ) # process argument value for 'date' by passing it to the Proc
  def action_three(date)
    ...
  end

  argible(:count => :to_i) # invoke the :to_i method on the resolved value for 'count'
  def action_four(count)
    ...
  end

  argible(:the_name => :@first_name) # resolves the value for 'the_name' and sets it to the instance variable @first_name
  def action_five
    p @first_name # this instance variable has been set automatically by Argible
    ...
  end
end

The first method, action_one, is annotated with the method argible. When this action method is executed Argible will intercept the call and look up the request parameter value associated with the argument names alpha and beta. Then the action_one method will be called with these resolved values.

The second action, action_two, provides an example of how more complex processing can occur on argument values. As in the first example the both arguments, alpha and date, will be resolved against the request parameters. However the resolved value for date will then be passed to the Date#parse method. The returned value from Date#parse method call will then be used as the argument value for date when action_two is called. NOTE: any method can be used, Date#parse is simply used as an example.

The third example action, action_three, provides an example of using a Proc (lambda) as alternative to a named method.

The fourth example action, action_four, illustrates how you can call methods directly on the String value returned from the request parameter. Assume request parameter hash was {'count' => "1985"} then the Integer value 1985 would be passed as the argument value for the argument named 'count'.

The final example action, action_five, demonstrates how you can automatically set the value of an instance variable without requiring a method argument of the same name. This example works because :my_name points to a symbol begining with a ‘@’ character. Without this ‘@’ on the right hand side Argible would raise a Argible::UnknownArgumentError.

LICENSE:

(The MIT License)

Copyright © 2007 Michael Ward

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.