Skip to content
Enforces key requirements in a hash http://www.atomicobject.com/pages/Software+Commons
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
pkg
spec
LICENSE
README.rdoc
Rakefile
TODO
VERSION
init.rb
require_options.gemspec

README.rdoc

Description

Simply extract named arguments from a hash. Require or allow a set of keys.

Rationale

When using hashes to accomplish named-argument semantics for methods, we noticed a common pattern:

  • We often pull options out into local vars at the head of a method

  • We often raise argument errors for missing keys in the hash

RequireOptions accomplishes both tasks succinctly.

Install

script/plugin git://github.com/atomicobject/require_options.git

or

gem install require_options

Example

class Car
  include RequireOptions    

  def drive(opts)
    gear, speed = require_options(opts, :speed, :gear) 
    # opts 
  end    

  def put_in_trunk(opts)
    allow_options opts, :flare, :tire, :luggage
    # opts may contain any and all of the named keys, but no others
  end

  def register(opts)
    require_at_least_one opts, :license, :ssn
    # This method wants to use either opts[:license] OR opts[:ssn]
  end

  def park(opts)
    filter_options(opts, :spot)
    # filter out keys besides :spot, for security purposes, e.g.
  end
end

Authors

  • Justin DeWind (dewind@atomicobject.com)

  • David Crosby (crosby@atomicobject.com)

  • © 2009-2011 Atomic Object

  • More Atomic Object open source projects

Something went wrong with that request. Please try again.