param_checker 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -16,44 +16,44 @@ and afterwards (when using the Gemfile) execute
16
16
 
17
17
  == Usage
18
18
 
19
- Include the ParamChecker module where ever you like. I usually put it into my Rails +ApplicationController.rb+
19
+ Include the ParamChecker module where ever you like. I usually put it into my Rails <tt>ApplicationController.rb</tt>
20
20
 
21
21
  class ApplicationController < ActionController::Base
22
22
  include ParamChecker
23
23
  end
24
24
 
25
- You can then simply call +check_string_param(param_to_check, "my default", ["foo", "bar"])+ in every controller.
25
+ You can then simply call <tt>check_string(param_to_check, "my default", ["foo", "bar"])</tt> in every controller.
26
26
 
27
27
  Instead of including the module you could also call all functions of the module directly.
28
28
 
29
- ParamChecker.check_string_param(param_to_check, "my default", ["foo", "bar"])
29
+ ParamChecker.check_string(param_to_check, "my default", ["foo", "bar"])
30
30
 
31
31
  There are currently 5 supported functions:
32
32
 
33
- * +check_integer_param(param, default, min, max)+
34
- * +check_float_param(param, default, min, max)+
35
- * +check_string_param(param, default, allowed)+
36
- * +check_symbol_param(param, default, allowed)+
37
- * +check_boolean_param(param, default)+
33
+ check_integer(param, default, min, max)
34
+ check_float(param, default, min, max)
35
+ check_string(param, default, allowed)
36
+ check_symbol(param, default, allowed)
37
+ check_boolean(param, default)
38
38
 
39
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_param+ and +check_float_param+ 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_param+ and +check_symbol_param+ represent the allowed values of +param+. They can be either a string (resp. a symbol for +check_symbol_param+), a regular expression, or an array of strings (resp. symbols for +check_symbol_param+).
42
- +check_boolean_param+ 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_param returns an integer, check_symbol_param returns a symbol, and so on).
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).
44
44
 
45
45
  == Examples
46
46
 
47
47
  Below are some simple examples how I use those function in my controllers.
48
48
 
49
49
  # +max+ is not provided in this case. I just want to ensure that page is bigger than 1 and otherwise return 1.
50
- page = check_integer_param(params[:page], 1, 1)
50
+ page = check_integer(params[:page], 1, 1)
51
51
 
52
52
  # Returns "name" if params[:field] is not "name" or "address"
53
- field = check_string_param(params[:field], "name", ["name", "address"])
53
+ field = check_string(params[:field], "name", ["name", "address"])
54
54
 
55
55
  # Return the boolean if params[:accepted] is a valid boolean representation, and the default false otherwise.
56
- accepted = check_boolean_param(params[:accepted], false)
56
+ accepted = check_boolean(params[:accepted], false)
57
57
 
58
58
  == Testing
59
59
 
@@ -1,3 +1,3 @@
1
1
  module ParamChecker
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/param_checker.rb CHANGED
@@ -2,7 +2,12 @@ module ParamChecker
2
2
 
3
3
  module_function
4
4
 
5
- def check_integer_param(param, default, min = nil, max = nil)
5
+ # Check a parameter string if it is a valid integer and return its integer value.
6
+ # +param+: the string parameter to check
7
+ # +default+: the default integer to return if the check fails
8
+ # +min+: the minimum value allowed (optional)
9
+ # +max+: the maximum value allowed (optional)
10
+ def check_integer(param, default, min = nil, max = nil)
6
11
  min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
7
12
  max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
8
13
 
@@ -13,30 +18,45 @@ module ParamChecker
13
18
  end
14
19
  end
15
20
 
16
- def check_float_param(param, default, min = nil, max = nil)
21
+ # Check a parameter string if it is a valid float and return its float value.
22
+ # +param+: the string parameter to check
23
+ # +default+: the default float to return if the check fails
24
+ # +min+: the minimum value allowed (optional)
25
+ # +max+: the maximum value allowed (optional)
26
+ def check_float(param, default, min = nil, max = nil)
17
27
  min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
18
28
  max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
19
29
 
20
30
  if (param && param.strip =~ /^-?[0-9]+(\.[0-9]+)?$/ && min_lambda.call && max_lambda.call)
21
31
  param.strip.to_f
22
32
  else
23
- default.to_f
33
+ default
24
34
  end
25
35
  end
26
36
 
27
- def check_string_param(param, default, allowed)
37
+ # Check a parameter string if it is a valid sting and return its string value.
38
+ # +param+: the string parameter to check
39
+ # +default+: the default string to return if the check fails
40
+ # +allowed+: the allowed string value to check +param+ against; could be
41
+ # a regular expression, a string or an array of strings
42
+ def check_string(param, default, allowed)
28
43
  if (param && allowed.class == Regexp && param =~ allowed)
29
- param
44
+ param.to_s
30
45
  elsif (param && allowed.class == Array && allowed.include?(param))
31
- param
46
+ param.to_s
32
47
  elsif (param && allowed.class == String && allowed == param)
33
- param
48
+ param.to_s
34
49
  else
35
50
  default
36
51
  end
37
52
  end
38
53
 
39
- def check_symbol_param(param, default, allowed)
54
+ # Check a parameter string if it is a valid :symbol and return its symbol value.
55
+ # +param+: the string parameter to check
56
+ # +default+: the default symbol to return if the check fails
57
+ # +allowed+: the allowed symbol value to check +param+ against; could be
58
+ # a regular expression, a string, a symbol, an array of strings or an array of symbols.
59
+ def check_symbol(param, default, allowed)
40
60
  begin
41
61
  if (param && allowed.class == Regexp && param.to_s =~ allowed)
42
62
  param.to_sym
@@ -45,14 +65,18 @@ module ParamChecker
45
65
  elsif (param && (allowed.class == String || allowed.class == Symbol) && allowed.to_sym == param.to_sym)
46
66
  param.to_sym
47
67
  else
48
- default.to_sym
68
+ default
49
69
  end
50
70
  rescue
51
- default.to_sym
71
+ default
52
72
  end
53
73
  end
54
74
 
55
- def check_boolean_param(param, default)
75
+ # Check a parameter string if it represents a valid boolean and return its boolean value.
76
+ # Allowed string parameters are "1" or "true" for +true+, and "0" or "false" for +false+.
77
+ # +param+: the string parameter to check
78
+ # +default+: the default boolean to return if the check fails
79
+ def check_boolean(param, default)
56
80
  if (param && param == "1" || param == "true")
57
81
  true
58
82
  elsif (param && param == "0" || param == "false")
@@ -62,9 +86,9 @@ module ParamChecker
62
86
  end
63
87
  end
64
88
 
65
- public :check_integer_param,
66
- :check_float_param,
67
- :check_string_param,
68
- :check_symbol_param,
69
- :check_boolean_param
89
+ public :check_integer,
90
+ :check_float,
91
+ :check_string,
92
+ :check_symbol,
93
+ :check_boolean
70
94
  end
@@ -6,78 +6,83 @@ describe "ParamChecker" do
6
6
  end
7
7
 
8
8
  it "should check integer" do
9
- @model.check_integer_param("5", 99).should == 5
10
- @model.check_integer_param("5", 99, 4).should == 5
11
- @model.check_integer_param("5", 99, nil, 6).should == 5
12
- @model.check_integer_param("5", 99, 4, 6).should == 5
13
- @model.check_integer_param("-5", 99).should == -5
14
- @model.check_integer_param(" 5 ", 99).should == 5
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
15
15
 
16
- @model.check_integer_param("", 99).should == 99
17
- @model.check_integer_param("5abc", 99).should == 99
18
- @model.check_integer_param("5", 99, 6).should == 99
19
- @model.check_integer_param("5", 99, nil, 4).should == 99
20
- @model.check_integer_param("5", 99, 1, 4).should == 99
21
- @model.check_integer_param("5", 99, 10, 1).should == 99
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
22
23
  end
23
24
 
24
25
  it "should check float" do
25
- @model.check_float_param("5.1", 99.2).should == 5.1
26
- @model.check_float_param("5.1", 99.2, 4.3).should == 5.1
27
- @model.check_float_param("5.1", 99.2, nil, 6.4).should == 5.1
28
- @model.check_float_param("5.1", 99.2, 4.3, 6.4).should == 5.1
29
- @model.check_float_param("-5.1", 99.2).should == -5.1
30
- @model.check_float_param(" 5.1 ", 99.2).should == 5.1
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
31
32
 
32
- @model.check_float_param("", 99.2).should == 99.2
33
- @model.check_float_param("5abc", 99.2).should == 99.2
34
- @model.check_float_param("5", 99.2, 6.4).should == 99.2
35
- @model.check_float_param("5", 99.2, nil, 4.3).should == 99.2
36
- @model.check_float_param("5", 99.2, 1, 4.3).should == 99.2
37
- @model.check_float_param("5", 99.2, 10.5, 1.6).should == 99.2
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
38
40
  end
39
41
 
40
42
  it "should check string" do
41
- @model.check_string_param("lorem", "dolor", /.*ore.*/).should == "lorem"
42
- @model.check_string_param("lorem", "dolor", ["lorem", "ipsum"]).should == "lorem"
43
- @model.check_string_param("lorem", "dolor", "lorem").should == "lorem"
44
- @model.check_string_param("", "dolor", /.*/).should == ""
45
- @model.check_string_param("", "dolor", "").should == ""
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 == ""
46
48
 
47
- @model.check_string_param("lorem", "dolor", /.*ips.*/).should == "dolor"
48
- @model.check_string_param("lorem", "dolor", ["patre", "ipsum"]).should == "dolor"
49
- @model.check_string_param("lorem", "dolor", "ipsum").should == "dolor"
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"
50
53
  end
51
54
 
52
55
  it "should check symbol" do
53
- @model.check_symbol_param("lorem", :dolor, /.*ore.*/).should == :lorem
54
- @model.check_symbol_param("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
55
- @model.check_symbol_param("lorem", :dolor, :lorem).should == :lorem
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
56
59
 
57
- @model.check_symbol_param("lorem", :dolor, /.*ips.*/).should == :dolor
58
- @model.check_symbol_param("lorem", :dolor, ["patre", "ipsum"]).should == :dolor
59
- @model.check_symbol_param("lorem", :dolor, "ipsum").should == :dolor
60
- @model.check_symbol_param("lorem", "dolor", "ipsum").should == :dolor
61
- @model.check_symbol_param("", :dolor, /.*/).should == :dolor
62
- @model.check_symbol_param("", :dolor, "").should == :dolor
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
63
67
  end
64
68
 
65
69
  it "should check boolean" do
66
- @model.check_boolean_param("1", false).should == true
67
- @model.check_boolean_param("true", false).should == true
68
- @model.check_boolean_param("0", true).should == false
69
- @model.check_boolean_param("false", true).should == false
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
70
74
 
71
- @model.check_boolean_param("3", true).should == true
72
- @model.check_boolean_param("", true).should == true
73
- @model.check_boolean_param("abc", true).should == true
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
74
79
  end
75
80
 
76
81
  it "can be called as module functions" do
77
- ParamChecker.check_integer_param("5", 99).should == 5
78
- ParamChecker.check_float_param("5.1", 99.2).should == 5.1
79
- ParamChecker.check_string_param("lorem", "dolor", /.*ore.*/).should == "lorem"
80
- ParamChecker.check_symbol_param("lorem", :dolor, /.*ore.*/).should == :lorem
81
- ParamChecker.check_boolean_param("1", false).should == true
82
+ ParamChecker.check_integer("5", 99).should == 5
83
+ ParamChecker.check_float("5.1", 99.2).should == 5.1
84
+ ParamChecker.check_string("lorem", "dolor", /.*ore.*/).should == "lorem"
85
+ ParamChecker.check_symbol("lorem", :dolor, /.*ore.*/).should == :lorem
86
+ ParamChecker.check_boolean("1", false).should == true
82
87
  end
83
88
  end
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: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 4
10
+ version: 0.0.4
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-09 00:00:00 +01:00
18
+ date: 2011-03-20 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency