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
- Host: GitHub
- URL: https://github.com/atomicobject/require_options
- Owner: atomicobject
- License: mit
- Created: 2010-01-29T15:39:25.000Z (about 16 years ago)
- Default Branch: master
- Last Pushed: 2012-10-09T03:30:48.000Z (over 13 years ago)
- Last Synced: 2025-11-10T13:25:46.249Z (6 months ago)
- Language: Ruby
- Homepage: http://www.atomicobject.com/pages/Software+Commons
- Size: 116 KB
- Stars: 1
- Watchers: 24
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
- License: LICENSE
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