require_options 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Atomic Object
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
data/README.rdoc ADDED
@@ -0,0 +1,34 @@
1
+ == Description
2
+ Simply extract named arguments from a hash. Require or allow a set of keys.
3
+
4
+ == Rationale
5
+ When using hashes to accomplish named-argument semantics for methods, we
6
+ noticed a common pattern:
7
+
8
+ * We often pull options out into local vars at the head of a method
9
+ * We often raise argument errors for missing keys in the hash
10
+
11
+ RequireOptions accomplishes both tasks succinctly.
12
+
13
+ == Install
14
+ script/plugin git://github.com/atomicobject/require_options.git
15
+
16
+ == Example
17
+ class Car
18
+ include RequireOptions
19
+
20
+ def drive(opts)
21
+ gear, speed = require_options(opts, :speed, :gear)
22
+ # opts
23
+ end
24
+
25
+ def put_in_trunk(opts)
26
+ allow_options opts, :flare, :tire, :luggage
27
+ # opts may contain any and all of the named keys, but no others
28
+ end
29
+
30
+ def register(opts)
31
+ require_at_least_one opts, :license, :ssn
32
+ # This method wants to use either opts[:license] OR opts[:ssn]
33
+ end
34
+ end
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ task :default => :spec
4
+
5
+ Spec::Rake::SpecTask.new do |t|
6
+ t.spec_files = "spec/**/*_spec.rb"
7
+ t.spec_opts = ["-f s -c"]
8
+ end
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |s|
13
+ s.name = "require_options"
14
+ s.summary = "Simply extract named arguments from a hash. Require or allow a set of keys."
15
+ s.description = "Simply extract named arguments from a hash. Require or allow a set of keys."
16
+ s.email = "github@atomicobject.com"
17
+ s.homepage = "http://github.com/atomicobject/require_options"
18
+ s.authors = ["Atomic Object"]
19
+ s.executables = []
20
+ s.files = FileList["lib/**/*.rb", "LICENSE", "Rakefile"]
21
+ s.test_files = FileList["spec/**/*.rb"]
22
+ end
23
+
24
+ Jeweler::GemcutterTasks.new
25
+ rescue LoadError
26
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
data/TODO ADDED
@@ -0,0 +1 @@
1
+ * Make gem
@@ -0,0 +1,20 @@
1
+ module RequireOptions
2
+ def require_options(options, *required_keys)
3
+ values = required_keys.flatten.map do |key|
4
+ val = options[key]
5
+ raise "#{key} required" unless options[key]
6
+ val
7
+ end
8
+ values.size == 1 ? values.first : values
9
+ end
10
+
11
+ def allow_options(options, *allowed_options)
12
+ options.keys.each do |key|
13
+ raise "#{key} not allowed" unless allowed_options.include?(key)
14
+ end
15
+ end
16
+
17
+ def require_at_least_one(options, *keys)
18
+ raise "#{keys.join(' or ')} is required" unless keys.any? { |key| !options[key].nil? && !options[key].empty?}
19
+ end
20
+ end
@@ -0,0 +1,77 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "/../lib/require_options")
2
+
3
+ describe RequireOptions do
4
+ include RequireOptions
5
+
6
+ describe "#require_options" do
7
+ it "returns the list of values from the specified keys" do
8
+ opts = {:gear => 2, :speed => 200}
9
+ gear, speed = require_options(opts, :gear, :speed)
10
+
11
+ gear.should == 2
12
+ speed.should == 200
13
+ end
14
+
15
+ it "returns a value rather than a list of values if only one key is given" do
16
+ opts = {:name => "John"}
17
+ name = require_options(opts, :name)
18
+ name.should == "John"
19
+ end
20
+
21
+ it "does NOT reject extraneous keys" do
22
+ opts = {:name => "John", :tractor => "Deere"}
23
+ name = require_options(opts, :name)
24
+ name.should == "John"
25
+ end
26
+
27
+ it "raises an error if given key does not exist in hash" do
28
+ opts = {:gear => 2}
29
+ lambda {
30
+ require_options(opts, :gear, :speed)
31
+ }.should raise_error("speed required")
32
+ end
33
+ end
34
+
35
+ describe "#allow_options" do
36
+ it "disallows keys if they have not been specified" do
37
+ opts = {:name => "Jason", :age => 22}
38
+ lambda {
39
+ allow_options(opts, :age)
40
+ }.should raise_error("name not allowed")
41
+ end
42
+
43
+ it "does nothing if keys are allowed" do
44
+ lambda {
45
+ allow_options({:name => "Josh"}, :name)
46
+ }.should_not raise_error
47
+ end
48
+ end
49
+
50
+ describe "#require_at_least_one" do
51
+ it "ensures that at least one of the given keys is present" do
52
+ opts = {:license => "1234"}
53
+
54
+ lambda {
55
+ require_at_least_one(opts, :license, :ssn)
56
+ }.should_not raise_error
57
+
58
+ opts = {:ssn => "4311"}
59
+
60
+ lambda {
61
+ require_at_least_one(opts, :license, :ssn)
62
+ }.should_not raise_error
63
+
64
+ opts = {:name => "Jane"}
65
+
66
+ lambda {
67
+ require_at_least_one(opts, :license, :ssn)
68
+ }.should raise_error("license or ssn is required")
69
+
70
+ opts = {:ssn => ""}
71
+
72
+ lambda {
73
+ require_at_least_one(opts, :license, :ssn)
74
+ }.should raise_error("license or ssn is required")
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: require_options
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Atomic Object
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-31 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simply extract named arguments from a hash. Require or allow a set of keys.
17
+ email: github@atomicobject.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ - TODO
26
+ files:
27
+ - LICENSE
28
+ - Rakefile
29
+ - lib/require_options.rb
30
+ - README.rdoc
31
+ - TODO
32
+ has_rdoc: true
33
+ homepage: http://github.com/atomicobject/require_options
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Simply extract named arguments from a hash. Require or allow a set of keys.
60
+ test_files:
61
+ - spec/require_options_spec.rb