itrigga-param_fu 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +21 -0
- data/Gemfile.lock +25 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/trigga/param_fu/param_fu.rb +52 -0
- data/lib/trigga/param_fu.rb +6 -0
- data/spec/param_fu_spec.rb +120 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +12 -0
- metadata +157 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "1.3.0"
|
10
|
+
gem "rspec-rails", "1.3.2"
|
11
|
+
gem "bundler", "~> 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.6.4"
|
13
|
+
gem "rcov", ">= 0"
|
14
|
+
end
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem "rspec", "1.3.0"
|
18
|
+
gem "rspec-rails", "1.3.2"
|
19
|
+
gem "bundler", "~> 1.0.0"
|
20
|
+
gem "rcov", ">= 0"
|
21
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rack (1.1.0)
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rspec (1.3.0)
|
13
|
+
rspec-rails (1.3.2)
|
14
|
+
rack (>= 1.0.0)
|
15
|
+
rspec (>= 1.3.0)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
ruby
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
bundler (~> 1.0.0)
|
22
|
+
jeweler (~> 1.6.4)
|
23
|
+
rcov
|
24
|
+
rspec (= 1.3.0)
|
25
|
+
rspec-rails (= 1.3.2)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Anson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= itrigga-param_fu
|
2
|
+
|
3
|
+
Validates params to methods are present and correct
|
4
|
+
|
5
|
+
== Contributing to itrigga-param_fu
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Anson. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "itrigga-param_fu"
|
18
|
+
gem.homepage = "http://gemcutter.com/itrigga-param_fu"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Validates parameters to methods}
|
21
|
+
gem.description = %Q{Validates parameters to methods are present and are the required types}
|
22
|
+
gem.email = "support@itrigga.com"
|
23
|
+
gem.authors = ["Al Davidson","Anson Kelly"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'spec/version'
|
29
|
+
require 'spec/rake/spectask'
|
30
|
+
require 'spec/ruby'
|
31
|
+
|
32
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
33
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
34
|
+
spec.spec_opts = ['--options', 'spec/spec.opts']
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
desc "Run all specs with rcov"
|
40
|
+
Spec::Rake::SpecTask.new(:rcov) do |t|
|
41
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
42
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
43
|
+
t.rcov = true
|
44
|
+
t.rcov_dir = 'coverage'
|
45
|
+
t.rcov_opts = ['--exclude', "features,kernel,load-diff-lcs\.rb,instance_exec\.rb,lib/spec.rb,lib/spec/runner.rb,^spec/*,bin/spec,examples,/gems,/Library/Ruby,\.autotest,#{ENV['GEM_HOME']}"]
|
46
|
+
t.rcov_opts << '--sort coverage --text-summary --aggregate coverage.data'
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Trigga
|
2
|
+
module ParamFu
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def require_obj_or_id(opts, key)
|
10
|
+
obj_or_id(opts,key)
|
11
|
+
raise ArgumentError.new("#{key} or #{key_with_id(key)} are required") unless opts[key_with_id(key)]
|
12
|
+
opts
|
13
|
+
end
|
14
|
+
|
15
|
+
def require_param(opts, *keys)
|
16
|
+
keys.to_a.each do |k|
|
17
|
+
raise ArgumentError.new("#{k} is required") unless opts[k]
|
18
|
+
end
|
19
|
+
opts
|
20
|
+
end
|
21
|
+
|
22
|
+
def obj_or_id(opts, key)
|
23
|
+
opts[key_with_id(key)] ||= opts[key].id if opts[key]
|
24
|
+
end
|
25
|
+
|
26
|
+
def key_with_id(key)
|
27
|
+
(key.to_s + '_id').to_sym
|
28
|
+
end
|
29
|
+
|
30
|
+
def require_one_of( opts, *keys )
|
31
|
+
present = (opts.keys & keys)
|
32
|
+
raise ArgumentError.new( "at least one of the arguments #{keys.inspect} is required" ) if present.empty?
|
33
|
+
return present
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module InstanceMethods
|
38
|
+
def require_obj_or_id(opts,key)
|
39
|
+
self.class.require_obj_or_id(opts, key)
|
40
|
+
end
|
41
|
+
def obj_or_id(opts, key)
|
42
|
+
self.class.obj_or_id(opts, key)
|
43
|
+
end
|
44
|
+
def require_param(opts, *keys)
|
45
|
+
self.class.require_param(opts, *keys)
|
46
|
+
end
|
47
|
+
def require_one_of( opts, *keys )
|
48
|
+
self.class.require_one_of( opts, *keys )
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class Tester
|
4
|
+
include ::Trigga::ParamFu
|
5
|
+
end
|
6
|
+
|
7
|
+
describe ::Trigga::ParamFu do
|
8
|
+
before(:each) do
|
9
|
+
@t = Tester.new
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "class methods" do
|
13
|
+
|
14
|
+
describe "key_with_id" do
|
15
|
+
it "should add '_id' onto the given symbol" do
|
16
|
+
Tester.key_with_id(:blart).should == :blart_id
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "obj_or_id" do
|
21
|
+
describe "when the given hash already contains :(given_key)_id" do
|
22
|
+
it "should not change the value of that _id key" do
|
23
|
+
@my_obj = mock("object")
|
24
|
+
@my_obj.stub!(:id).and_return(456)
|
25
|
+
@my_hash = {:my_key_id=>123, :my_obj=>@my_obj}
|
26
|
+
Tester.obj_or_id( @my_hash, :my_key )
|
27
|
+
@my_hash[:my_key_id].should == 123
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "when the given hash does not already contain :(given_key)_id" do
|
32
|
+
|
33
|
+
describe "and the given hash contains the given key without an added _id" do
|
34
|
+
it "should set the :(given_key)_id to the id of the object" do
|
35
|
+
@mock_obj = mock("obj")
|
36
|
+
@mock_obj.stub!(:id).and_return(123)
|
37
|
+
Tester.obj_or_id( {:my_obj=>@mock_obj}, :my_obj ).should == 123
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "require_param" do
|
44
|
+
context "when given a single key" do
|
45
|
+
|
46
|
+
context "when key is not present in opts" do
|
47
|
+
it "should raise an ArgumentError" do
|
48
|
+
lambda{ Tester.require_param( {:key1=>'value1'}, :key2 ) }.should raise_error(ArgumentError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
context "when key is present in opts" do
|
52
|
+
it "should not raise an error" do
|
53
|
+
lambda{ Tester.require_param( {:key1=>'value1'}, :key1 ) }.should_not raise_error(ArgumentError)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
context "when given multiple keys" do
|
58
|
+
context "when all keys are present" do
|
59
|
+
it "should not raise an error" do
|
60
|
+
lambda{ Tester.require_param( {:key1=>'value1', :key2=>'value2'}, :key2, :key1 ) }.should_not raise_error(ArgumentError)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
context "when at least one key is not present" do
|
64
|
+
it "should raise an ArgumentError " do
|
65
|
+
lambda{ Tester.require_param( {:key1=>'value1', :key2=>'value2'}, :key2, :key1, :key3 ) }.should raise_error(ArgumentError)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "require_obj_or_id" do
|
72
|
+
context "when the key is present" do
|
73
|
+
it "should not raise an error" do
|
74
|
+
lambda{ Tester.require_obj_or_id( {:obj_name=>'value1'}, :obj_name ) }.should_not raise_error(ArgumentError)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
context "when the key is not present" do
|
78
|
+
context "when the key suffixed with _id is present" do
|
79
|
+
it "should not raise an error" do
|
80
|
+
lambda{ Tester.require_obj_or_id( {:obj_id=>'value1'}, :obj ) }.should_not raise_error(ArgumentError)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
context "when the key is not present even suffixed with _id" do
|
84
|
+
it "should raise an error" do
|
85
|
+
lambda{ Tester.require_obj_or_id( {:key1=>'value1', :key2_id=>123}, :key3 ) }.should raise_error(ArgumentError)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "require_one_of" do
|
92
|
+
context "when at least one of the given keys is contained in opts" do
|
93
|
+
it "should not raise an error" do
|
94
|
+
lambda{ Tester.require_one_of( {:obj=>'value1'}, :foo, :bar, :obj ) }.should_not raise_error(ArgumentError)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
context "when none of the given keys are contained in opts" do
|
98
|
+
it "should raise an error" do
|
99
|
+
lambda{ Tester.require_one_of( {:obj=>'value1'}, :foo, :bar ) }.should raise_error(ArgumentError)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "instance_methods" do
|
106
|
+
describe "require_obj_or_id" do
|
107
|
+
it "should call the class method require_obj_or_id" do
|
108
|
+
Tester.should_receive(:require_obj_or_id).with( {:key=>'val'}, :key )
|
109
|
+
@t.require_obj_or_id( {:key=>'val'}, :key )
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "obj_or_id" do
|
114
|
+
it "should call the class method obj_or_id" do
|
115
|
+
Tester.should_receive(:obj_or_id).with( {:key=>'val'}, :key )
|
116
|
+
@t.obj_or_id( {:key=>'val'}, :key )
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
RAILS_ENV = 'test'
|
2
|
+
ENV['RAILS_ENV'] = 'test'
|
3
|
+
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(RAILS_ROOT, 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
require 'lib/trigga/param_fu'
|
9
|
+
|
10
|
+
# Requires supporting files with custom matchers and macros, etc,
|
11
|
+
# in ./support/ and its subdirectories.
|
12
|
+
Dir["RAILS_ROOT}/support/**/*.rb"].each {|f| require f}
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: itrigga-param_fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Al Davidson
|
14
|
+
- Anson Kelly
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-10-07 00:00:00 +01:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
prerelease: false
|
24
|
+
name: rspec
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - "="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 27
|
31
|
+
segments:
|
32
|
+
- 1
|
33
|
+
- 3
|
34
|
+
- 0
|
35
|
+
version: 1.3.0
|
36
|
+
requirement: *id001
|
37
|
+
type: :development
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
prerelease: false
|
40
|
+
name: rspec-rails
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - "="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 31
|
47
|
+
segments:
|
48
|
+
- 1
|
49
|
+
- 3
|
50
|
+
- 2
|
51
|
+
version: 1.3.2
|
52
|
+
requirement: *id002
|
53
|
+
type: :development
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
prerelease: false
|
56
|
+
name: bundler
|
57
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 23
|
63
|
+
segments:
|
64
|
+
- 1
|
65
|
+
- 0
|
66
|
+
- 0
|
67
|
+
version: 1.0.0
|
68
|
+
requirement: *id003
|
69
|
+
type: :development
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
prerelease: false
|
72
|
+
name: jeweler
|
73
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 7
|
79
|
+
segments:
|
80
|
+
- 1
|
81
|
+
- 6
|
82
|
+
- 4
|
83
|
+
version: 1.6.4
|
84
|
+
requirement: *id004
|
85
|
+
type: :development
|
86
|
+
- !ruby/object:Gem::Dependency
|
87
|
+
prerelease: false
|
88
|
+
name: rcov
|
89
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirement: *id005
|
99
|
+
type: :development
|
100
|
+
description: Validates parameters to methods are present and are the required types
|
101
|
+
email: support@itrigga.com
|
102
|
+
executables: []
|
103
|
+
|
104
|
+
extensions: []
|
105
|
+
|
106
|
+
extra_rdoc_files:
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.rdoc
|
109
|
+
files:
|
110
|
+
- .document
|
111
|
+
- Gemfile
|
112
|
+
- Gemfile.lock
|
113
|
+
- LICENSE.txt
|
114
|
+
- README.rdoc
|
115
|
+
- Rakefile
|
116
|
+
- VERSION
|
117
|
+
- lib/trigga/param_fu.rb
|
118
|
+
- lib/trigga/param_fu/param_fu.rb
|
119
|
+
- spec/param_fu_spec.rb
|
120
|
+
- spec/spec.opts
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
has_rdoc: true
|
123
|
+
homepage: http://gemcutter.com/itrigga-param_fu
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
hash: 3
|
137
|
+
segments:
|
138
|
+
- 0
|
139
|
+
version: "0"
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
hash: 3
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
version: "0"
|
149
|
+
requirements: []
|
150
|
+
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 1.3.7
|
153
|
+
signing_key:
|
154
|
+
specification_version: 3
|
155
|
+
summary: Validates parameters to methods
|
156
|
+
test_files: []
|
157
|
+
|