ostruct-sanitizer 0.2.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb69d7d79fdbe87d7170925729d2322892251f0d
4
- data.tar.gz: e7916d591ac8d1f636aaf594109ef95c0e7234c8
3
+ metadata.gz: 73b62ce9ad20aad02e6d4b8c844920156e38abef
4
+ data.tar.gz: ea109f7926cbaa9075ef332299a36a3321b2c2d9
5
5
  SHA512:
6
- metadata.gz: f6d3a53a6bf8f18592ff87a1b284e61008418bfe0951afd59db0b434882427fbc14d8d9199518f76c77265cdaab4c2bf2055374d62f2343fa686e21d73d03b7b
7
- data.tar.gz: 4dd3c31e4f72e2af30218071fc24b37847e4d9a6156adebdbb133250e28a87ee0a32bbda852a4ec636041a0dbea8461cef991e0b2a2c0b3f30fa10c5905ac52e
6
+ metadata.gz: f594d6d4e082d7737c536770102b5b818c2828a59cd23050535c8a3bf7848c47c64f7f014615af7c5dda0f9fcafa066d3a549933d0b016a9e5e91871b5148cd8
7
+ data.tar.gz: 07ecf9f6b57b7df70d9041fe9011a441bdd65267c754d15bd9184296d13e91bce3801846cc681504d093b88d104c745d162b3822b1eb9e1764055a330ce816a9
@@ -1,5 +1,5 @@
1
1
  module OStruct
2
2
  module Sanitizer
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -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 "OpenStructSanitizer can only be used within OpenStruct classes."
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] field the field to be sanitized
75
- # @param [#call] block sanitization block that takes the current value and returns the new sanitized value
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(field, &block)
77
+ def sanitize(*fields, &block)
78
78
  @sanitizers ||= {}
79
- field_sanitizers = @sanitizers[field] ||= []
80
- field_sanitizers << block
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.each do |field|
90
- sanitize(field) { |value| value[0...length] }
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.each do |field|
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.each do |field|
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.each do |field|
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.2.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-19 00:00:00.000000000 Z
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler