required_options 0.5.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/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ required_options (0.1.0)
5
+
6
+ GEM
7
+ specs:
8
+ diff-lcs (1.1.3)
9
+ rspec (2.6.0)
10
+ rspec-core (~> 2.6.0)
11
+ rspec-expectations (~> 2.6.0)
12
+ rspec-mocks (~> 2.6.0)
13
+ rspec-core (2.6.4)
14
+ rspec-expectations (2.6.0)
15
+ diff-lcs (~> 1.1.2)
16
+ rspec-mocks (2.6.0)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ required_options!
23
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Tom Preston-Werner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ RequiredOptions
2
+ =======
3
+
4
+ # DESCRIPTION
5
+
6
+ Guard your methods against those slacker options that 'forget' to come to class.
7
+
8
+ # INSTALLATION
9
+
10
+ Add gem to your project.
11
+
12
+ If using bundler
13
+
14
+ gem 'required_options'
15
+
16
+ # USAGE
17
+
18
+ Just declare which components of your options hash that you can't live without
19
+
20
+ def some_method_with_options(arg, opts = {})
21
+ required_options opts, :track_name, :band_name
22
+
23
+ # do some really smart work here
24
+ end
25
+
26
+ If a required option is not passed into the method, it will raise a MissingOptions exception that displays the forgotten option.
data/Rakefile ADDED
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rake/rdoctask'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
142
+ unless libfiles.empty?
143
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
144
+ exit!
145
+ end
146
+ unless Dir['VERSION*'].empty?
147
+ puts "A `VERSION` file at root level violates Gem best practices."
148
+ exit!
149
+ end
150
+ end
@@ -0,0 +1,22 @@
1
+ module RequiredOptions
2
+ VERSION = '0.5.0'
3
+
4
+ def required_options(options, *required_options)
5
+ included_opts, missing_opts = required_options.partition do |o|
6
+ options.include?(o)
7
+ end
8
+
9
+ raise MissingOptions, exception_text_for(missing_opts) if !missing_opts.empty?
10
+ end
11
+
12
+ private
13
+ def exception_text_for(missing_options)
14
+ missing_options.map(&:inspect).join(", ").gsub("\"", "")
15
+ end
16
+ end
17
+
18
+ class Object
19
+ include RequiredOptions
20
+ end
21
+
22
+ class MissingOptions < ArgumentError; end
@@ -0,0 +1,53 @@
1
+ Gem::Specification.new do |s|
2
+ s.specification_version = 2 if s.respond_to? :specification_version=
3
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
+ s.rubygems_version = '1.3.5'
5
+
6
+ ## Leave these as is they will be modified for you by the rake gemspec task.
7
+ ## If your rubyforge_project name is different, then edit it and comment out
8
+ ## the sub! line in the Rakefile
9
+ s.name = 'required_options'
10
+ s.version = '0.5.0'
11
+ s.date = '2011-09-26'
12
+
13
+ ## Make sure your summary is short. The description may be as long
14
+ ## as you like.
15
+ s.summary = "Guard your methods against those slacker options that 'forget' to come to class."
16
+ s.description = "RequiredOptions allows you to verbosely guard methods from options you rely on when using an options hash."
17
+
18
+ ## List the primary authors. If there are a bunch of authors, it's probably
19
+ ## better to set the email to an email list or something. If you don't have
20
+ ## a custom homepage, consider using your GitHub URL or the like.
21
+ s.authors = ["Matt Polito"]
22
+ s.email = 'matt.polito@gmail.com'
23
+ s.homepage = 'http://github.com/mattpolito/required_options'
24
+
25
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
26
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
27
+ s.require_paths = %w[lib]
28
+
29
+ ## Specify any RDoc options here. You'll want to add your README and
30
+ ## LICENSE files to the extra_rdoc_files list.
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.extra_rdoc_files = %w[README.md LICENSE]
33
+
34
+ s.add_development_dependency('rspec')
35
+
36
+ ## Leave this section as-is. It will be automatically generated from the
37
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
38
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
39
+
40
+ # = MANIFEST =
41
+ s.files = %w[
42
+ Gemfile
43
+ Gemfile.lock
44
+ LICENSE
45
+ README.md
46
+ Rakefile
47
+ lib/required_options.rb
48
+ required_options.gemspec
49
+ spec/required_options_spec.rb
50
+ ]
51
+ # = MANIFEST =
52
+ s.test_files = s.files.select { |path| path =~ /^spec\/*._spec\.rb/ }
53
+ end
@@ -0,0 +1,53 @@
1
+ require 'rspec'
2
+ require 'required_options'
3
+
4
+ describe RequiredOptions do
5
+ class Dummy
6
+ def self.major_method(opts = {})
7
+ required_options opts, :speed
8
+ end
9
+
10
+ def minor_method(options = {})
11
+ required_options options, :dyno, "rpm"
12
+ end
13
+ end
14
+
15
+ context "when required options are passed" do
16
+ context "and a class method" do
17
+ it "will not raise an exception" do
18
+ lambda do
19
+ Dummy.major_method(:speed => "")
20
+ end.should_not raise_error(MissingOptions)
21
+ end
22
+ end
23
+
24
+ context "and an instance method" do
25
+ it "will not raise an exception" do
26
+ lambda do
27
+ Dummy.new.minor_method(
28
+ :dyno => "",
29
+ "rpm" => ""
30
+ )
31
+ end.should_not raise_error(MissingOptions)
32
+ end
33
+ end
34
+ end
35
+
36
+ context "when required options are missing" do
37
+ context "and a class method" do
38
+ it "will raise a MissingOptions exception" do
39
+ lambda do
40
+ Dummy.major_method
41
+ end.should raise_error(MissingOptions, ":speed")
42
+ end
43
+ end
44
+
45
+ context "and an instance method" do
46
+ it "will raise a MissingOptions exception" do
47
+ lambda do
48
+ Dummy.new.minor_method
49
+ end.should raise_error(MissingOptions, ":dyno, rpm")
50
+ end
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: required_options
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
+ platform: ruby
12
+ authors:
13
+ - Matt Polito
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-26 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: RequiredOptions allows you to verbosely guard methods from options you rely on when using an options hash.
36
+ email: matt.polito@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ - LICENSE
44
+ files:
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - lib/required_options.rb
51
+ - required_options.gemspec
52
+ - spec/required_options_spec.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/mattpolito/required_options
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.4.2
84
+ signing_key:
85
+ specification_version: 2
86
+ summary: Guard your methods against those slacker options that 'forget' to come to class.
87
+ test_files: []
88
+