sequel_sanitize 0.0.1
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/CHANGELOG +2 -0
- data/LICENSE +20 -0
- data/README.rdoc +37 -0
- data/Rakefile +0 -0
- data/lib/sequel_sanitize/version.rb +7 -0
- data/lib/sequel_sanitize.rb +74 -0
- metadata +89 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Kevin Tom - http://github.com/kevintom
|
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,37 @@
|
|
1
|
+
= sequel_sanitize
|
2
|
+
|
3
|
+
== Install
|
4
|
+
|
5
|
+
Add gemcutter.org source if you don't have it:
|
6
|
+
gem source http://gemcutter.org
|
7
|
+
|
8
|
+
Install:
|
9
|
+
[sudo] gem install sequel_sanitize
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
This plug-in provide functionality to allow Sequel::Model to have fields sanitized
|
14
|
+
with a defined or default method.
|
15
|
+
|
16
|
+
To use plug-in you add plug-in to your model:
|
17
|
+
|
18
|
+
class MyModel < Sequel::Model
|
19
|
+
plugin :sanitize, :fields => [:first_name, :last_name], :sanitizer => :name_sanitizer
|
20
|
+
plugin :sanitize, :fields => [:email], :downcase => true
|
21
|
+
end
|
22
|
+
|
23
|
+
You can use following options:
|
24
|
+
*fields*:: an array of the fields that the sanitizer method should be applied to
|
25
|
+
*sanitizer*:: Proc or Symbol to call to sanitize the value of the field.
|
26
|
+
*downcase*:: Set to true to downcase the value returned from the sanitizer
|
27
|
+
|
28
|
+
Options *sanitizer* and *downcase* are optional.
|
29
|
+
|
30
|
+
<b>Options are inherited when you use inheritance for your models</b>. However
|
31
|
+
you can only set options via plugin method.
|
32
|
+
|
33
|
+
If you don't provide a :sanitizer, the default sanitizer method will strip whitespace
|
34
|
+
|
35
|
+
== Copyright
|
36
|
+
|
37
|
+
Copyright (c) 2011 Kevin Tom. See LICENSE for details.
|
data/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Plugins
|
3
|
+
# The Sanitize plugin basically does a 'before_filter' on
|
4
|
+
# specified fields, if no sanitizer is specified the default
|
5
|
+
# one is used
|
6
|
+
#
|
7
|
+
module Sanitize
|
8
|
+
# Plugin configuration
|
9
|
+
def self.configure(model, opts={})
|
10
|
+
model.sanitize_options = opts
|
11
|
+
model.sanitize_options.freeze
|
12
|
+
model.class_eval do
|
13
|
+
sanitize_options[:fields].each do |f|
|
14
|
+
define_method("#{f}=") do |value|
|
15
|
+
sanitizer = self.class.sanitize_options[:field_sanitize][f]
|
16
|
+
do_downcase = self.class.sanitize_options[:field_downcase][f]
|
17
|
+
sanitized = sanitizer.call(value) if sanitizer.respond_to?(:call)
|
18
|
+
sanitized ||= self.send(sanitizer, value) if sanitizer
|
19
|
+
sanitized = sanitized.downcase if do_downcase
|
20
|
+
super(sanitized)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
attr_reader :sanitize_options
|
28
|
+
# Propagate settings to the child classes
|
29
|
+
# @param [Class] Child class
|
30
|
+
def inherited(klass)
|
31
|
+
super
|
32
|
+
klass.sanitize_options = self.sanitize_options.dup
|
33
|
+
end
|
34
|
+
|
35
|
+
def sanitize_options=(options)
|
36
|
+
fields = options[:fields]
|
37
|
+
if fields.nil? || !fields.is_a?(Array) || fields.empty?
|
38
|
+
raise ArgumentError, ":fields must be a non-empty array"
|
39
|
+
end
|
40
|
+
options[:fields] = fields.uniq.compact
|
41
|
+
sanitizer = options[:sanitizer]
|
42
|
+
if sanitizer && !sanitizer.is_a?(Symbol) && !sanitizer.respond_to?(:call)
|
43
|
+
raise ArgumentError, "If you provide :sanitizer it must be Symbol or callable."
|
44
|
+
end
|
45
|
+
sanitizer_to_be_called = sanitizer.nil? ? :sanitize_field : sanitizer
|
46
|
+
do_downcase = options[:downcase].class == TrueClass
|
47
|
+
field_sanitize_map = {}
|
48
|
+
field_downcase_map = {}
|
49
|
+
options[:fields].each do |f|
|
50
|
+
field_sanitize_map[f.to_sym] = sanitizer_to_be_called
|
51
|
+
field_downcase_map[f.to_sym] = do_downcase
|
52
|
+
end
|
53
|
+
@sanitize_options ||= {}
|
54
|
+
@sanitize_options[:fields] ||= []
|
55
|
+
@sanitize_options[:field_sanitize] ||= {}
|
56
|
+
@sanitize_options[:field_downcase] ||= {}
|
57
|
+
@sanitize_options[:fields].concat(options[:fields])
|
58
|
+
@sanitize_options[:fields].uniq!
|
59
|
+
@sanitize_options[:fields].compact!
|
60
|
+
@sanitize_options[:field_sanitize].merge!(field_sanitize_map)
|
61
|
+
@sanitize_options[:field_downcase].merge!(field_downcase_map)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
module InstanceMethods
|
66
|
+
private
|
67
|
+
def sanitize_field(value)
|
68
|
+
sanitized_value = value.strip if value.respond_to?(:strip)
|
69
|
+
sanitized_value
|
70
|
+
end
|
71
|
+
end # InstanceMethods
|
72
|
+
end # Sanitize
|
73
|
+
end # Plugins
|
74
|
+
end # Sequel
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel_sanitize
|
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
|
+
- Kevin Tom
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-12 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: sequel
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Sequel plugin which lets you set a sanitization method on specific fields
|
38
|
+
email: kevin@kevintom.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- README.rdoc
|
45
|
+
- LICENSE
|
46
|
+
- CHANGELOG
|
47
|
+
files:
|
48
|
+
- Rakefile
|
49
|
+
- lib/sequel_sanitize/version.rb
|
50
|
+
- lib/sequel_sanitize.rb
|
51
|
+
- README.rdoc
|
52
|
+
- LICENSE
|
53
|
+
- CHANGELOG
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: http://github.com/kevintom/sequel_sanitize
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Sequel plugin which lets you set a sanitization method on specific fields
|
88
|
+
test_files: []
|
89
|
+
|