Showing posts with label argible. Show all posts
Showing posts with label argible. Show all posts

September 10, 2007

Argible: impromptu presentation at chirb

Tonight I ended up giving an impromptu overview of Argible at the monthly Chicago Area Ruby Group. Utilizing annotations in Ruby code seemed foreign to several of the folks there. So, I quickly went over the Argible source code to illustrate how annotations can be implemented in Ruby (via the Module#method_added(name) callback). This code alone seemed to interest several of the people there.

Additionally, I tried to stress that the concept of using an argument name as meta-data is what I personally find most interesting about Argible. The idea behind Argible should NOT be limited simply to Rails based applications. Anyone have ideas for other situation where this might be useful?

August 29, 2007

Argible: testing argible-ized actions

Last week Jay Fields and I were discussing Argible and he asked, "How's testing an action that uses Argible?", the answer ... easy.

Suppose we have the following Controller with an argible-ized action:

class CalculatorController < ApplicationController

argible(:first => :to_i, :second => :to_i)
def add(first, second)
@result = first + second
end

end

And here is the associated RSpec test:

describe CalculatorController, "add action" do
controller_name :calculator

it "should add argument values" do
post :add, :first => "18", :second => "2"
controller.instance_variable_get(:@result).should == 20
end

end

You can see from the test that you do not need to do anything special to test your argible annotated action methods. Just call the action with whatever parameter name and values your action requires.

August 22, 2007

Argible: automatically set instance variables

As I discussed in my last post Argible makes it easy to resolve method argument values through simple convention. Peter Ryan had an excellent idea of using Argible to automatically set an instance variable's value through simple convention. Have a look at the example below:

class MyController < ApplicationController

argible(:foo => :@bar)
def action_one
p "Bar instance variable was set to: #{@bar}"
end

end

Notice that the action method, action_one, has NO arguments. But the argible annotation was passed :foo => :@bar. Argible will interpret that you want the resolved value of 'foo' to be set upon the instance variable named '@bar'. This happens because the symbol on the right hand side is prefixed with the character '@'. Without this Argible will raise an Error informing you that there is no such argument.

August 20, 2007

Introducing Argible: Simplifing your Ruby on Rails Controllers

Argible is a RubyForge project that was created to simplify Action methods 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 these argument values. Have a look at the FoobarController code below:

class FoobarController < ApplicationController

argible
def action_one(alpha)
...
end

argible(:date => Date.method(:parse))
def action_two(date)
...
end

argible(:date => lambda {|v| Date.parse(v) } )
def action_three(date)
...
end

argible(:value => :to_i)
def action_four(value)
...
end
end

The first method action_one(alpha) is annotated with the method argible. When the action_one method is executed Argible will intercept the call and look up the request parameter value associated with alpha. Next the action_one method will be called with this resolved value.

The second action, action_two(date), provides an example of how more complex processing can occur on argument values. As in the first example the argument, this time named date, is resolved against the request parameters. The resulting value will then be passed to the Date#parse method. The result from Date#parse will then be used when action_two is called. **NOTE: any method can be used, Date#parse is simply used as an example.

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

The fourth example action action_four(value) illustrates how you can call methods directly on the string value returned from
the request parameter.

Argible 0.1.0 is available for immediate use from http://rubyforge.org/projects/argible. RDoc can be found at http://argible.rubyforge.org.