ostruct-sanitizer 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ostruct/sanitizer/version.rb +1 -1
- data/lib/ostruct/sanitizer.rb +15 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73b62ce9ad20aad02e6d4b8c844920156e38abef
|
4
|
+
data.tar.gz: ea109f7926cbaa9075ef332299a36a3321b2c2d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f594d6d4e082d7737c536770102b5b818c2828a59cd23050535c8a3bf7848c47c64f7f014615af7c5dda0f9fcafa066d3a549933d0b016a9e5e91871b5148cd8
|
7
|
+
data.tar.gz: 07ecf9f6b57b7df70d9041fe9011a441bdd65267c754d15bd9184296d13e91bce3801846cc681504d093b88d104c745d162b3822b1eb9e1764055a330ce816a9
|
data/lib/ostruct/sanitizer.rb
CHANGED
@@ -19,7 +19,7 @@ module OStruct
|
|
19
19
|
module Sanitizer
|
20
20
|
def self.included(base)
|
21
21
|
unless base.ancestors.include? OpenStruct
|
22
|
-
raise "
|
22
|
+
raise "#{self.name} can only be used within OpenStruct classes"
|
23
23
|
end
|
24
24
|
|
25
25
|
base.extend ClassMethods
|
@@ -71,24 +71,26 @@ module OStruct
|
|
71
71
|
|
72
72
|
# Registers a sanitization block for a given field
|
73
73
|
#
|
74
|
-
# @param [Symbol]
|
75
|
-
# @param [#call] block sanitization block
|
74
|
+
# @param [Array<Symbol>] a list of field names to be sanitized
|
75
|
+
# @param [#call] block sanitization block to be applied to the current value of each field and returns the new sanitized value
|
76
76
|
#
|
77
|
-
def sanitize(
|
77
|
+
def sanitize(*fields, &block)
|
78
78
|
@sanitizers ||= {}
|
79
|
-
|
80
|
-
|
79
|
+
fields.each do |field|
|
80
|
+
field_sanitizers = @sanitizers[field] ||= []
|
81
|
+
field_sanitizers << block
|
82
|
+
end
|
81
83
|
end
|
82
84
|
|
83
85
|
# Truncates fields to a given length value
|
84
86
|
#
|
85
87
|
# @param [Array<Symbol>] a list of field names to be sanitized
|
86
88
|
# @param [Integer] length the amount to truncate the field's value to
|
89
|
+
# @param [Boolean] strip_whitespaces whether or not to strip whitespaces
|
87
90
|
#
|
88
|
-
def truncate(*fields, length:)
|
89
|
-
fields
|
90
|
-
|
91
|
-
end
|
91
|
+
def truncate(*fields, length:, strip_whitespaces: true)
|
92
|
+
sanitize(*fields) { |value| value[0...length] }
|
93
|
+
strip(*fields) if strip_whitespaces
|
92
94
|
end
|
93
95
|
|
94
96
|
# Drops any punctuation character from the field's value
|
@@ -96,9 +98,7 @@ module OStruct
|
|
96
98
|
# @param [Array<Symbol>] a list of field names to be sanitized
|
97
99
|
#
|
98
100
|
def drop_punctuation(*fields)
|
99
|
-
fields
|
100
|
-
sanitize(field) { |value| value.gsub(/[^\w\s]/, '') }
|
101
|
-
end
|
101
|
+
sanitize(*fields) { |value| value.gsub(/[^\w\s]/, '') }
|
102
102
|
end
|
103
103
|
|
104
104
|
# Strips out leading and trailing spaces from the values of the given fields
|
@@ -106,9 +106,7 @@ module OStruct
|
|
106
106
|
# @param [Array<Symbol>] fields list of fields to be sanitized
|
107
107
|
#
|
108
108
|
def strip(*fields)
|
109
|
-
fields
|
110
|
-
sanitize(field) { |value| value.strip }
|
111
|
-
end
|
109
|
+
sanitize(*fields) { |value| value.strip }
|
112
110
|
end
|
113
111
|
|
114
112
|
# Removes any non-digit character from the values of the given fields
|
@@ -116,9 +114,7 @@ module OStruct
|
|
116
114
|
# @param [Array<Symbol>] fields list of fields to be sanitized
|
117
115
|
#
|
118
116
|
def digits(*fields)
|
119
|
-
fields
|
120
|
-
sanitize(field) { |value| value.to_s.gsub(/[^0-9]/, '') }
|
121
|
-
end
|
117
|
+
sanitize(*fields) { |value| value.to_s.gsub(/[^0-9]/, '') }
|
122
118
|
end
|
123
119
|
end
|
124
120
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ostruct-sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego Borges
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|