RAA - rarg

rarg / 0.1

Short description: Utility for validating named arguments
Category: Library/Utility
Status: beta
Created: 2009-01-31 05:27:41 GMT
Last update: 2009-01-31 05:27:41 GMT
Owner: Dice (Projects of this owner)
Homepage: http://ruby.morphball.net/rarg/
Download: http://ruby.morphball.net/rarg/release/rarg-0.1.zip
License: PublicDomain
Dependency:
None
Description:

RArg is simple and small library for validating a Hash as named arguments (keyword arguments). It has three functions, requiring arguments, setting default arguments, and aliasing argument name.

       require 'rarg'

       class Foo
               attr_reader :r1, :r2, :d1

               def initialize(options = {})
                       re = RArg.parse(options) do
                               require_arg :r1, :r2
                               default_arg :d1, 0
                               alias_arg :a1, :r1
                       end

                       @r1, @r2, @d1 = re[:r1], re[:r2], re[:d1]
               end
       end

       Foo.new({}) # Exception: argument of 'r1' is required, but missing (RArg::Error)
       Foo.new({:r1 => nil}) # Exception: argument of 'r2' is required, but missing (RArg::Error)
       Foo.new({:r1 => nil, :r2 => 10, :nd => 20}) # Exception: 'nd' is not defined (RArg::Error)
       Foo.new({:r1 => nil, :r2 => 10}) # No Exception

       foo = Foo.new({:r1 => 'R1', :r2 => 'R2'})
       p foo.r2 #=> 'R2'
       p foo.d1 #=> 0 (default)

       foo = Foo.new({:a1 => 'R1', :r2 => nil, :d1 => 'D1'})
       p foo.d1 #=> 'D1' (overwritten)
       p foo.r1 #=> 'R1' (alias definition: r1 => a1)

Edit this project (for project owner)

back to RAA top