An open API service indexing awesome lists of open source software.

https://github.com/atomicobject/require_options

Enforces key requirements in a hash
https://github.com/atomicobject/require_options

Last synced: 2 months ago
JSON representation

Enforces key requirements in a hash

Awesome Lists containing this project

README

          

== 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}[http://www.atomicobject.com]
* More Atomic Object {open source}[http://www.atomicobject.com/pages/Software+Commons] projects