param_checker 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  = ParamChecker
2
2
 
3
- ParamChecker is a small library for validation and parsing of user provided parameters. It is a handy way to check GET/POST params in Ruby webframeworks (like Rails or Sinatra).
3
+ ParamChecker is a small Ruby library for validating and casting string parameters. It is for example a handy way to check GET/POST +params+ in Ruby On Rails.
4
4
 
5
5
  == Installation
6
6
 
@@ -22,9 +22,9 @@ Include the ParamChecker module where ever you like. I usually put it into my Ra
22
22
  include ParamChecker
23
23
  end
24
24
 
25
- You can then simply call <tt>check_string(param_to_check, "my default", ["foo", "bar"])</tt> in every controller.
25
+ You can then simply call for example <tt>check_string(param_to_check, "my default", ["foo", "bar"])</tt> in every controller.
26
26
 
27
- Instead of including the module you could also call all functions of the module directly.
27
+ Instead of including the module you could also call all functions of the module directly, like
28
28
 
29
29
  ParamChecker.check_string(param_to_check, "my default", ["foo", "bar"])
30
30
 
@@ -36,11 +36,13 @@ There are currently 5 supported functions:
36
36
  check_symbol(param, default, allowed)
37
37
  check_boolean(param, default)
38
38
 
39
- +param+ is always the string parameter to check. +default+ is a value that is returned when +param+ does not succeed the check.
40
- +min+ and +max+ in +check_integer+ and +check_float+ are the minimum and maximum allowed values of param. Both +min+ and +max+ options are optional. If they are not provided then no range is checked at all.
41
- +allowed+ in +check_string+ and +check_symbol+ represent the allowed values of +param+. They can be either a string (resp. a symbol for +check_symbol+), a regular expression, or an array of strings (resp. symbols for +check_symbol+).
42
- +check_boolean+ evaluates "1" or "true" string as true and "0" or "false" string to false.
43
- All functions return the parsed and type cast value (check_integer returns an integer, check_symbol returns a symbol, and so on).
39
+ * +param+ (_required_) is the string parameter to check.
40
+ * +default+ (_required_) is the value that is returned when +param+ does not pass the check.
41
+ * +min+ (_optional_), +max+ (_optional_) in +check_integer+ and +check_float+ are the minimum and maximum allowed values of param. (If not provided then no range is checked at all.)
42
+ * +allowed+ (_optional_) in +check_string+ and +check_symbol+ represent the allowed values of +param+. They can be either a string (resp. a symbol for +check_symbol+), a regular expression, or an array of strings (resp. symbols for +check_symbol+).
43
+ * +check_boolean+ evaluates "1" or "true" string as true and "0" or "false" string to false.
44
+
45
+ All functions return the the casted value (check_integer returns an integer, check_symbol returns a symbol, and so on).
44
46
 
45
47
  == Examples
46
48
 
@@ -8,11 +8,11 @@ module ParamChecker
8
8
  # +min+: the minimum value allowed (optional)
9
9
  # +max+: the maximum value allowed (optional)
10
10
  def check_integer(param, default, min = nil, max = nil)
11
- min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
12
- max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
11
+ min_lambda = (min.nil? ? lambda { true } : lambda { param.to_i >= min })
12
+ max_lambda = (max.nil? ? lambda { true } : lambda { param.to_i <= max })
13
13
 
14
14
  if (param && param.strip =~ /^-?[0-9]+$/ && min_lambda.call && max_lambda.call)
15
- param.strip.to_i
15
+ param.to_i
16
16
  else
17
17
  default
18
18
  end
@@ -24,11 +24,11 @@ module ParamChecker
24
24
  # +min+: the minimum value allowed (optional)
25
25
  # +max+: the maximum value allowed (optional)
26
26
  def check_float(param, default, min = nil, max = nil)
27
- min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
28
- max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
27
+ min_lambda = (min.nil? ? lambda { true } : lambda { param.to_i >= min })
28
+ max_lambda = (max.nil? ? lambda { true } : lambda { param.to_i <= max })
29
29
 
30
30
  if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min_lambda.call && max_lambda.call)
31
- param.strip.to_f
31
+ param.to_f
32
32
  else
33
33
  default
34
34
  end
@@ -41,11 +41,11 @@ module ParamChecker
41
41
  # a regular expression, a string or an array of strings
42
42
  def check_string(param, default, allowed)
43
43
  if (param && allowed.class == Regexp && param =~ allowed)
44
- param.to_s
44
+ param
45
45
  elsif (param && allowed.class == Array && allowed.include?(param))
46
- param.to_s
46
+ param
47
47
  elsif (param && allowed.class == String && allowed == param)
48
- param.to_s
48
+ param
49
49
  else
50
50
  default
51
51
  end
@@ -57,17 +57,13 @@ module ParamChecker
57
57
  # +allowed+: the allowed symbol value to check +param+ against; could be
58
58
  # a regular expression, a string, a symbol, an array of strings or an array of symbols.
59
59
  def check_symbol(param, default, allowed)
60
- begin
61
- if (param && allowed.class == Regexp && param.to_s =~ allowed)
62
- param.to_sym
63
- elsif (param && allowed.class == Array && allowed.map { |i| i.to_sym }.include?(param.to_sym))
64
- param.to_sym
65
- elsif (param && (allowed.class == String || allowed.class == Symbol) && allowed.to_sym == param.to_sym)
66
- param.to_sym
67
- else
68
- default
69
- end
70
- rescue
60
+ if (param && !param.empty? && allowed.class == Regexp && param =~ allowed)
61
+ param.to_sym
62
+ elsif (param && !param.empty? && allowed.class == Array && allowed.map { |a| a.to_sym }.include?(param.to_sym))
63
+ param.to_sym
64
+ elsif (param && !param.empty? && (allowed.class == String || allowed.class == Symbol) && allowed.to_sym == param.to_sym)
65
+ param.to_sym
66
+ else
71
67
  default
72
68
  end
73
69
  end
@@ -1,3 +1,3 @@
1
1
  module ParamChecker
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -5,77 +5,102 @@ describe "ParamChecker" do
5
5
  @model = ParamCheckerModel.new
6
6
  end
7
7
 
8
- it "should check integer" do
9
- @model.check_integer("5", 99).should == 5
10
- @model.check_integer("5", 99, 4).should == 5
11
- @model.check_integer("5", 99, nil, 6).should == 5
12
- @model.check_integer("5", 99, 4, 6).should == 5
13
- @model.check_integer("-5", 99).should == -5
14
- @model.check_integer(" 5 ", 99).should == 5
8
+ describe "check integer" do
9
+ it "should pass" do
10
+ @model.check_integer("5", 99).should == 5
11
+ @model.check_integer("5", 99, 4).should == 5
12
+ @model.check_integer("5", 99, nil, 6).should == 5
13
+ @model.check_integer("5", 99, 4, 6).should == 5
14
+ @model.check_integer("-5", 99).should == -5
15
+ @model.check_integer(" 5 ", 99).should == 5
16
+ end
15
17
 
16
- @model.check_integer(nil, 99).should == 99
17
- @model.check_integer("", 99).should == 99
18
- @model.check_integer("5abc", 99).should == 99
19
- @model.check_integer("5", 99, 6).should == 99
20
- @model.check_integer("5", 99, nil, 4).should == 99
21
- @model.check_integer("5", 99, 1, 4).should == 99
22
- @model.check_integer("5", 99, 10, 1).should == 99
18
+ it "should fail" do
19
+ @model.check_integer(nil, 99).should == 99
20
+ @model.check_integer("", 99).should == 99
21
+ @model.check_integer("5abc", 99).should == 99
22
+ @model.check_integer("5", 99, 6).should == 99
23
+ @model.check_integer("5", 99, nil, 4).should == 99
24
+ @model.check_integer("5", 99, 1, 4).should == 99
25
+ @model.check_integer("5", 99, 10, 1).should == 99
26
+ @model.check_integer("", nil).should == nil
27
+ end
23
28
  end
24
29
 
25
- it "should check float" do
26
- @model.check_float("5.1", 99.2).should == 5.1
27
- @model.check_float("5.1", 99.2, 4.3).should == 5.1
28
- @model.check_float("5.1", 99.2, nil, 6.4).should == 5.1
29
- @model.check_float("5.1", 99.2, 4.3, 6.4).should == 5.1
30
- @model.check_float("-5.1", 99.2).should == -5.1
31
- @model.check_float(" 5.1 ", 99.2).should == 5.1
30
+ describe "check float" do
31
+ it "should pass" do
32
+ @model.check_float("5.1", 99.2).should == 5.1
33
+ @model.check_float("5.1", 99.2, 4.3).should == 5.1
34
+ @model.check_float("5.1", 99.2, nil, 6.4).should == 5.1
35
+ @model.check_float("5.1", 99.2, 4.3, 6.4).should == 5.1
36
+ @model.check_float("-5.1", 99.2).should == -5.1
37
+ @model.check_float(" 5.1 ", 99.2).should == 5.1
38
+ end
32
39
 
33
- @model.check_float(nil, 99.2).should == 99.2
34
- @model.check_float("", 99.2).should == 99.2
35
- @model.check_float("5abc", 99.2).should == 99.2
36
- @model.check_float("5", 99.2, 6.4).should == 99.2
37
- @model.check_float("5", 99.2, nil, 4.3).should == 99.2
38
- @model.check_float("5", 99.2, 1, 4.3).should == 99.2
39
- @model.check_float("5", 99.2, 10.5, 1.6).should == 99.2
40
+ it "should fail" do
41
+ @model.check_float(nil, 99.2).should == 99.2
42
+ @model.check_float("", 99.2).should == 99.2
43
+ @model.check_float("5abc", 99.2).should == 99.2
44
+ @model.check_float("5", 99.2, 6.4).should == 99.2
45
+ @model.check_float("5", 99.2, nil, 4.3).should == 99.2
46
+ @model.check_float("5", 99.2, 1, 4.3).should == 99.2
47
+ @model.check_float("5", 99.2, 10.5, 1.6).should == 99.2
48
+ @model.check_float("", nil).should == nil
49
+ end
40
50
  end
41
51
 
42
- it "should check string" do
43
- @model.check_string("lorem", "dolor", /.*ore.*/).should == "lorem"
44
- @model.check_string("lorem", "dolor", ["lorem", "ipsum"]).should == "lorem"
45
- @model.check_string("lorem", "dolor", "lorem").should == "lorem"
46
- @model.check_string("", "dolor", /.*/).should == ""
47
- @model.check_string("", "dolor", "").should == ""
52
+ describe "check string" do
53
+ it "should pass" do
54
+ @model.check_string("lorem", "dolor", /.*ore.*/).should == "lorem"
55
+ @model.check_string("lorem", "dolor", ["lorem", "ipsum"]).should == "lorem"
56
+ @model.check_string("lorem", "dolor", "lorem").should == "lorem"
57
+ @model.check_string("", "dolor", /.*/).should == ""
58
+ @model.check_string("", "dolor", "").should == ""
59
+ end
48
60
 
49
- @model.check_string(nil, "dolor", /.*ore.*/).should == "dolor"
50
- @model.check_string("lorem", "dolor", /.*ips.*/).should == "dolor"
51
- @model.check_string("lorem", "dolor", ["patre", "ipsum"]).should == "dolor"
52
- @model.check_string("lorem", "dolor", "ipsum").should == "dolor"
61
+ it "should fail" do
62
+ @model.check_string(nil, "dolor", /.*ore.*/).should == "dolor"
63
+ @model.check_string("lorem", "dolor", /.*ips.*/).should == "dolor"
64
+ @model.check_string("lorem", "dolor", ["patre", "ipsum"]).should == "dolor"
65
+ @model.check_string("lorem", "dolor", "ipsum").should == "dolor"
66
+ @model.check_string("lorem", nil, /.*ips.*/).should == nil
67
+ end
53
68
  end
54
69
 
55
- it "should check symbol" do
56
- @model.check_symbol("lorem", :dolor, /.*ore.*/).should == :lorem
57
- @model.check_symbol("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
58
- @model.check_symbol("lorem", :dolor, :lorem).should == :lorem
70
+ describe "check symbol" do
71
+ it "should pass" do
72
+ @model.check_symbol("lorem", :dolor, /.*ore.*/).should == :lorem
73
+ @model.check_symbol("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
74
+ @model.check_symbol("lorem", :dolor, :lorem).should == :lorem
75
+ end
59
76
 
60
- @model.check_symbol(nil, :dolor, /.*ore.*/).should == :dolor
61
- @model.check_symbol("lorem", :dolor, /.*ips.*/).should == :dolor
62
- @model.check_symbol("lorem", :dolor, ["patre", "ipsum"]).should == :dolor
63
- @model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
64
- @model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
65
- @model.check_symbol("", :dolor, /.*/).should == :dolor
66
- @model.check_symbol("", :dolor, "").should == :dolor
77
+ it "should fail" do
78
+ @model.check_symbol(nil, :dolor, /.*ore.*/).should == :dolor
79
+ @model.check_symbol("lorem", :dolor, /.*ips.*/).should == :dolor
80
+ @model.check_symbol("lorem", :dolor, ["patre", "ipsum"]).should == :dolor
81
+ @model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
82
+ @model.check_symbol("lorem", :dolor, "ipsum").should == :dolor
83
+ @model.check_symbol("", :dolor, /.*/).should == :dolor
84
+ @model.check_symbol("", :dolor, "").should == :dolor
85
+ @model.check_symbol("lorem", nil, /.*ips.*/).should == nil
86
+ end
67
87
  end
68
88
 
69
- it "should check boolean" do
70
- @model.check_boolean("1", false).should == true
71
- @model.check_boolean("true", false).should == true
72
- @model.check_boolean("0", true).should == false
73
- @model.check_boolean("false", true).should == false
89
+ describe "check boolean" do
90
+ it "should pass" do
91
+ @model.check_boolean("1", false).should == true
92
+ @model.check_boolean("true", false).should == true
93
+ @model.check_boolean("0", true).should == false
94
+ @model.check_boolean("false", true).should == false
95
+ end
74
96
 
75
- @model.check_boolean(nil, true).should == true
76
- @model.check_boolean("3", true).should == true
77
- @model.check_boolean("", true).should == true
78
- @model.check_boolean("abc", true).should == true
97
+ it "should fail" do
98
+ @model.check_boolean(nil, true).should == true
99
+ @model.check_boolean("3", true).should == true
100
+ @model.check_boolean("", true).should == true
101
+ @model.check_boolean("abc", true).should == true
102
+ @model.check_boolean("abc", nil).should == nil
103
+ end
79
104
  end
80
105
 
81
106
  it "can be called as module functions" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: param_checker
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 0.0.4
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kai Schlamp
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-20 00:00:00 +01:00
18
+ date: 2011-03-21 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency