sanitization 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c204e5917662144318c424b310a9ad6c8bdd64d59a421086fadd8bc94a48e239
4
- data.tar.gz: 5cbe00e349e72aa1e12371cc8155b1e2c80288b3da6ff0286665cdddfd7a0c1e
3
+ metadata.gz: 7df5efc0b65948fd54d88ac7c8f90751d86fe451fe13e89708fac01acc0eead6
4
+ data.tar.gz: da8dbd58e672ea0919a8d2f5952801866c14edb054cc25d643af05b0c87cbfb9
5
5
  SHA512:
6
- metadata.gz: eaafc40ec8fdddd782b913830f83a488974dafedab9c10004e61bc40209ee40e61112bff9d872c38deefede278f200f1e258dced084f720f4c730e7f99e2f4a8
7
- data.tar.gz: 1f1e095ce608ac8da583760efb546364dfe707c0c884c91541ceb364015b632cfd6695117153519e42221673fc54db7852ab9c06a59f26234b1ef44393a39193
6
+ metadata.gz: ff7a7568225361dc64f8e18ea00adeefc6e6ba468ae9233a9518e6c8dc83f880c51b5b6c16c54a6d5f695493e3393f0fd54dd34a0a3ea666c91fdb84d4207c1f
7
+ data.tar.gz: d89eb17b76a4c2dbe2cfe90457728bc795a46fb56a47c07b2ce2bbfa5c2245f1f0f56ec9bb3b8050d468e4e2c598ee6335efdaa6d26eb6477b949c948c5be83b
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # 1.1.0
2
+ * **BREAKING CHANGE:** By default, Sanitization now does nothing. A configuration block should be used to set your desired defaults. Add `Sanitization.simple_defaults!` to `config/initializers/sanitization.rb` for version 1.0.x defaults.
3
+ * Added support for configuration block.
4
+
5
+
6
+ # 1.0.0
7
+ * Initial Release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sanitization (1.0.2)
4
+ sanitization (1.1.0)
5
5
  activerecord
6
6
  activesupport
7
7
 
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Sanitization makes it easy to store slightly cleaner strings to your database.
4
4
 
5
+
5
6
  ### Features (all optional):
6
7
 
7
8
  - White space stripping
@@ -9,14 +10,37 @@ Sanitization makes it easy to store slightly cleaner strings to your database.
9
10
  - Empty string to nil (if database column supports it)
10
11
  - Change casing (ie. upcase, downcase, titlecase, etc)
11
12
 
13
+
12
14
  ### Defaults
13
15
 
14
- - Leading & training white spaces are stripped (`strip: true`)
15
- - All spaces are collapsed (`collapse: true`)
16
- - All empty strings are stored as `null` if the database column allows it (`nullify: true`)
16
+ By default, Sanitization has all options disabled. It is recommended you use a configuration block to set
17
+ sensitive defaults for your projects.
18
+
19
+ For example, I use:
20
+
21
+ ```ruby
22
+ # config/initializers/sanitization.rb
23
+
24
+ Sanitization.configure do |config|
25
+ config.strip = true
26
+ config.collapse = true
27
+ config.nullify = true
28
+ end
29
+
30
+ # or you can use the following shortcut instead:
31
+
32
+ Sanitization.simple_defaults!
33
+ ```
34
+
35
+
36
+ ### Configuration Options
37
+
38
+ - Strip leading & training white spaces (`strip: true|false`)
39
+ - Collapse consecutive spaces (`collapse: true|false`)
40
+ - Store empty strings as `null` if the database column allows it (`nullify: true|false`)
17
41
  - All String columns are sanitized (`only: nil, except: nil`)
18
- - Columns of type `text` are not sanitized (`include_text_type: false`)
19
- - Casing remains unchanged (`case: nil`)
42
+ - Also sanitize strings of type `text` (`include_text_type: true|false`)
43
+ - Change casing: (`case: :none|:up|:down|:custom`)
20
44
 
21
45
 
22
46
  ## Installation
@@ -29,6 +53,14 @@ bundle add sanitization
29
53
  ## Usage
30
54
 
31
55
  ```ruby
56
+
57
+ # Assuming the following configuration block:
58
+ Sanitization.configure do |config|
59
+ config.strip = true
60
+ config.collapse = true
61
+ config.nullify = true
62
+ end
63
+
32
64
  # Default settings for all strings
33
65
  class Person < ApplicationModel
34
66
  sanitization
@@ -70,10 +102,12 @@ end
70
102
 
71
103
  ```
72
104
 
105
+
73
106
  ## Development
74
107
 
75
108
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
109
 
110
+
77
111
  ## License
78
112
 
79
113
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/lib/sanitization.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "sanitization/version"
2
+ require "sanitization/configuration"
2
3
  require "sanitization/active_record_extension"
3
4
  require "active_record" unless defined?(ActiveRecord)
4
5
 
@@ -15,11 +15,8 @@ module Sanitization
15
15
 
16
16
  self.sanitization__store ||= {}
17
17
 
18
- options[:only] = Array.wrap(options[:only])
19
- options[:except] = Array.wrap(options[:except])
20
- options[:strip] = !!(options[:strip].nil? ? true : options[:strip])
21
- options[:collapse] = !!(options[:collapse].nil? ? true : options[:collapse])
22
- options[:nullify] = !!(options[:nullify].nil? ? true : options[:nullify])
18
+ options[:only] = Array.wrap(options[:only])
19
+ options[:except] = Array.wrap(options[:except])
23
20
 
24
21
  unless options[:case].nil?
25
22
  raise ArgumentError.new("Invalid type for `case`: #{options[:case].class}") \
@@ -41,8 +38,9 @@ module Sanitization
41
38
  end
42
39
 
43
40
  if options[:case]
44
- raise ArgumentError.new("Method not found: `:#{options[:case]}`. Valid methods are: :#{valid_case_methods.join(', :')}") \
45
- unless valid_case_methods.include?(options[:case])
41
+ raise ArgumentError.new("Method not found: `:#{options[:case]}`. " +
42
+ "Valid methods are: :#{valid_case_methods.join(', :')}") \
43
+ unless valid_case_methods.include?(options[:case]) || options[:case] == :none
46
44
  end
47
45
 
48
46
  columns_to_format.each do |col|
@@ -95,9 +93,9 @@ module Sanitization
95
93
  def sanitization__format_column(col_name, col_formatting)
96
94
  return unless self[col_name].is_a?(String)
97
95
 
98
- self[col_name].strip! if col_formatting[:strip]
96
+ self[col_name].strip! if value_or_default(col_formatting, :strip)
99
97
 
100
- if col_formatting[:collapse]
98
+ if value_or_default(col_formatting, :collapse)
101
99
  if MULTIBYTE_SUPPORTED && Encoding.compatible?(self[col_name], MULTIBYTE_BLANK)
102
100
  self[col_name].gsub!(/#{MULTIBYTE_BLANK}+/, " ")
103
101
  else
@@ -105,15 +103,27 @@ module Sanitization
105
103
  end
106
104
  end
107
105
 
108
- if col_formatting[:nullify] && !self[col_name].nil? && self[col_name].to_s.empty? && \
106
+ if value_or_default(col_formatting, :nullify) && !self[col_name].nil? && self[col_name].to_s.empty? && \
109
107
  self.class.columns.select { |c| c.name == col_name }.first.null
110
108
  return self[col_name] = nil
111
109
  end
112
110
 
113
- self[col_name] = self[col_name].send(col_formatting[:case]) if col_formatting[:case]
111
+ case_formatting_method = value_or_default(col_formatting, :case)
112
+ if !case_formatting_method.nil? && case_formatting_method != :none
113
+ self[col_name] = self[col_name].send(case_formatting_method)
114
+ end
115
+
114
116
  self[col_name]
115
117
  end
116
118
 
119
+ def value_or_default(col_formatting, transform)
120
+ if col_formatting[transform].nil?
121
+ Sanitization.configuration[transform]
122
+ else
123
+ col_formatting[transform]
124
+ end
125
+ end
126
+
117
127
 
118
128
  end # module InstanceMethods
119
129
  end # module ActiveRecordExt
@@ -0,0 +1,64 @@
1
+ module Sanitization
2
+ class << self
3
+ def configuration
4
+ @configuration ||= Configuration.new
5
+ end
6
+
7
+ def configuration=(c)
8
+ @configuration = c
9
+ end
10
+ end
11
+
12
+ def self.configure
13
+ yield(configuration)
14
+ end
15
+
16
+ def self.simple_defaults!
17
+ self.configuration.simple!
18
+ end
19
+
20
+ class Configuration
21
+ DEFAULTS = {
22
+ strip: false,
23
+ collapse: false,
24
+ case: :none,
25
+ nullify: false,
26
+ include_text_type: false
27
+ }
28
+
29
+ SIMPLE_DEFAULTS = {
30
+ strip: true,
31
+ collapse: true,
32
+ case: :none,
33
+ nullify: true,
34
+ include_text_type: false
35
+ }
36
+
37
+ attr_accessor *DEFAULTS.keys
38
+
39
+ def initialize(opts = {})
40
+ opts = DEFAULTS.merge((opts || {}).slice(DEFAULTS.keys))
41
+
42
+ opts.each_pair do |k,v|
43
+ self.instance_variable_set("@#{k}", v)
44
+ end
45
+ end
46
+
47
+ def [](k)
48
+ raise ArgumentError.new("Invalid parameter: #{k}") unless DEFAULTS.keys.include?(k)
49
+ self.instance_variable_get("@#{k}")
50
+ end
51
+
52
+ def clear!
53
+ DEFAULTS.each_pair do |k,v|
54
+ self.instance_variable_set("@#{k}", v)
55
+ end; self
56
+ end
57
+
58
+ def simple!
59
+ SIMPLE_DEFAULTS.each_pair do |k,v|
60
+ self.instance_variable_set("@#{k}", v)
61
+ end; self
62
+ end
63
+ end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module Sanitization
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanitization
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Mercier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-04 00:00:00.000000000 Z
11
+ date: 2021-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
49
  - ".rspec"
50
+ - CHANGELOG.md
50
51
  - Gemfile
51
52
  - Gemfile.lock
52
53
  - LICENSE.txt
@@ -56,6 +57,7 @@ files:
56
57
  - bin/setup
57
58
  - lib/sanitization.rb
58
59
  - lib/sanitization/active_record_extension.rb
60
+ - lib/sanitization/configuration.rb
59
61
  - lib/sanitization/version.rb
60
62
  - sanitization.gemspec
61
63
  homepage: https://github.com/cmer/sanitization
@@ -81,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
83
  - !ruby/object:Gem::Version
82
84
  version: '0'
83
85
  requirements: []
84
- rubygems_version: 3.1.2
86
+ rubygems_version: 3.1.4
85
87
  signing_key:
86
88
  specification_version: 4
87
89
  summary: ''