param_checker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in param_checker.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ module ParamChecker
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ module ParamChecker
2
+ def self.check_integer_param(param, default, min = nil, max = nil)
3
+ min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
4
+ max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
5
+
6
+ if (param && param.strip =~ /^-?[0-9]+$/ && min_lambda.call && max_lambda.call)
7
+ param.strip.to_i
8
+ else
9
+ default
10
+ end
11
+ end
12
+
13
+ def self.check_float_param(param, default, min = nil, max = nil)
14
+ min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
15
+ max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
16
+
17
+ if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min_lambda.call && max_lambda.call)
18
+ param.strip.to_f
19
+ else
20
+ default.to_f
21
+ end
22
+ end
23
+
24
+ def self.check_string_param(param, default, allowed)
25
+ if (param && allowed.class == Regexp && param =~ allowed)
26
+ param
27
+ elsif (param && allowed.class == Array && allowed.include?(param))
28
+ param
29
+ elsif (param && allowed.class == String && allowed == param)
30
+ param
31
+ else
32
+ default
33
+ end
34
+ end
35
+
36
+ def self.check_symbol_param(param, default, allowed)
37
+ begin
38
+ if (param && allowed.class == Regexp && param.to_s =~ allowed)
39
+ param.to_sym
40
+ elsif (param && allowed.class == Array && allowed.map { |i| i.to_sym }.include?(param.to_sym))
41
+ param.to_sym
42
+ elsif (param && (allowed.class == String || allowed.class == Symbol) && allowed.to_sym == param.to_sym)
43
+ param.to_sym
44
+ else
45
+ default.to_sym
46
+ end
47
+ rescue
48
+ default.to_sym
49
+ end
50
+ end
51
+
52
+ def self.check_boolean_param(param, default)
53
+ if (param && param == "1" || param == "true")
54
+ true
55
+ elsif (param && param == "0" || param == "false")
56
+ false
57
+ else
58
+ default
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "param_checker/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "param_checker"
7
+ s.version = ParamChecker::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Kai Schlamp"]
10
+ s.email = ["schlamp@gmx.de"]
11
+ s.homepage = "http://www.medihack.org"
12
+ s.summary = %q{Parameter parsing and validation}
13
+ s.description = %q{A library for parameter parsing and validation. A handy way to check user provided params in Rails.}
14
+
15
+ s.rubyforge_project = "param_checker"
16
+
17
+ s.add_development_dependency "rspec", ">= 2.0.0"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe "ParamChecker" do
4
+ it "should check integer" do
5
+ ParamChecker.check_integer_param("5", 99).should == 5
6
+ ParamChecker.check_integer_param("5", 99, 4).should == 5
7
+ ParamChecker.check_integer_param("5", 99, nil, 6).should == 5
8
+ ParamChecker.check_integer_param("5", 99, 4, 6).should == 5
9
+ ParamChecker.check_integer_param("-5", 99).should == -5
10
+ ParamChecker.check_integer_param(" 5 ", 99).should == 5
11
+
12
+ ParamChecker.check_integer_param("", 99).should == 99
13
+ ParamChecker.check_integer_param("5abc", 99).should == 99
14
+ ParamChecker.check_integer_param("5", 99, 6).should == 99
15
+ ParamChecker.check_integer_param("5", 99, nil, 4).should == 99
16
+ ParamChecker.check_integer_param("5", 99, 1, 4).should == 99
17
+ ParamChecker.check_integer_param("5", 99, 10, 1).should == 99
18
+ end
19
+
20
+ it "should check float" do
21
+ ParamChecker.check_float_param("5.1", 99.2).should == 5.1
22
+ ParamChecker.check_float_param("5.1", 99.2, 4.3).should == 5.1
23
+ ParamChecker.check_float_param("5.1", 99.2, nil, 6.4).should == 5.1
24
+ ParamChecker.check_float_param("5.1", 99.2, 4.3, 6.4).should == 5.1
25
+ ParamChecker.check_float_param("-5.1", 99.2).should == -5.1
26
+ ParamChecker.check_float_param(" 5.1 ", 99.2).should == 5.1
27
+
28
+ ParamChecker.check_float_param("", 99.2).should == 99.2
29
+ ParamChecker.check_float_param("5abc", 99.2).should == 99.2
30
+ ParamChecker.check_float_param("5", 99.2, 6.4).should == 99.2
31
+ ParamChecker.check_float_param("5", 99.2, nil, 4.3).should == 99.2
32
+ ParamChecker.check_float_param("5", 99.2, 1, 4.3).should == 99.2
33
+ ParamChecker.check_float_param("5", 99.2, 10.5, 1.6).should == 99.2
34
+ end
35
+
36
+ it "should check string" do
37
+ ParamChecker.check_string_param("lorem", "dolor", /.*ore.*/).should == "lorem"
38
+ ParamChecker.check_string_param("lorem", "dolor", ["lorem", "ipsum"]).should == "lorem"
39
+ ParamChecker.check_string_param("lorem", "dolor", "lorem").should == "lorem"
40
+ ParamChecker.check_string_param("", "dolor", /.*/).should == ""
41
+ ParamChecker.check_string_param("", "dolor", "").should == ""
42
+
43
+ ParamChecker.check_string_param("lorem", "dolor", /.*ips.*/).should == "dolor"
44
+ ParamChecker.check_string_param("lorem", "dolor", ["patre", "ipsum"]).should == "dolor"
45
+ ParamChecker.check_string_param("lorem", "dolor", "ipsum").should == "dolor"
46
+ end
47
+
48
+ it "should check symbol" do
49
+ ParamChecker.check_symbol_param("lorem", :dolor, /.*ore.*/).should == :lorem
50
+ ParamChecker.check_symbol_param("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
51
+ ParamChecker.check_symbol_param("lorem", :dolor, :lorem).should == :lorem
52
+
53
+ ParamChecker.check_symbol_param("lorem", :dolor, /.*ips.*/).should == :dolor
54
+ ParamChecker.check_symbol_param("lorem", :dolor, ["patre", "ipsum"]).should == :dolor
55
+ ParamChecker.check_symbol_param("lorem", :dolor, "ipsum").should == :dolor
56
+ ParamChecker.check_symbol_param("lorem", "dolor", "ipsum").should == :dolor
57
+ ParamChecker.check_symbol_param("", :dolor, /.*/).should == :dolor
58
+ ParamChecker.check_symbol_param("", :dolor, "").should == :dolor
59
+ end
60
+
61
+ it "should check boolean" do
62
+ ParamChecker.check_boolean_param("1", false).should == true
63
+ ParamChecker.check_boolean_param("true", false).should == true
64
+ ParamChecker.check_boolean_param("0", true).should == false
65
+ ParamChecker.check_boolean_param("false", true).should == false
66
+
67
+ ParamChecker.check_boolean_param("3", true).should == true
68
+ ParamChecker.check_boolean_param("", true).should == true
69
+ ParamChecker.check_boolean_param("abc", true).should == true
70
+ end
71
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'param_checker'
3
+
4
+ #load(File.dirname(__FILE__) + '/models.rb')
5
+
6
+ RSpec.configure do |config|
7
+ config.filter_run :focus => true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run_excluding :exclude => true
10
+
11
+ config.mock_with :rspec
12
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: param_checker
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Kai Schlamp
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-09 00:00:00 +01: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: 15
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ version: 2.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: A library for parameter parsing and validation. A handy way to check user provided params in Rails.
38
+ email:
39
+ - schlamp@gmx.de
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - Rakefile
50
+ - lib/param_checker.rb
51
+ - lib/param_checker/version.rb
52
+ - param_checker.gemspec
53
+ - spec/lib/param_checker_spec.rb
54
+ - spec/spec_helper.rb
55
+ has_rdoc: true
56
+ homepage: http://www.medihack.org
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project: param_checker
85
+ rubygems_version: 1.4.2
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Parameter parsing and validation
89
+ test_files: []
90
+