attributes_sanitizer 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: 98fa0192ea9522563237224ab465c986be336c2a68056bc41ba5f276feeb0fd1
4
- data.tar.gz: 3176dd989b679ed4c9a54b0815cea118c62862ea2bd21dd55e0c945c4d5f6b2a
3
+ metadata.gz: 57e17ee2d4e35426d5648e6fe5eead7ebe103efafaf1400be039882245a335c6
4
+ data.tar.gz: 33979410ba0b283121cc31dadae6bb8fe353c890882ccef6a12320fefa093e17
5
5
  SHA512:
6
- metadata.gz: 11ed0f80297d724c017f8df73b2d675f2a8d9f999764aaa51818e60fbd95889dd8d51fa327d44dc6fa18c535933dd5a3a15ef4f69efc18e971ce150d5d3e2e44
7
- data.tar.gz: cf469c09997dd4356f7a14d47be3fb4c740408663e4c7f363d9de9179c7a6fd4b7086d7f7c5e37c53976ba4bc2251ecf2cfffa380344d602152b8143d2bc125a
6
+ metadata.gz: 7e52b0d8717f93d78be0fd3fd33c7ea7ecd03200bfc573f7cfe45dfdd65891cec59452b6491af2bc04ba01b172de9de500e20a309a34dad5b483421e9fea064a
7
+ data.tar.gz: b0eef6ee51606052915c3306215c24eb2249c743b9b521a677df99e4ba55c1789b82cb10dc245cb5cb424c28d305c32562f6300a143cdfd805acf8b67008c59e
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Maintainability](http://api.codeclimate.com/v1/badges/29a55c3bd2dd9e5ed117/maintainability)](http://codeclimate.com/github/andersondias/attributes_sanitizer/maintainability) [![Test Coverage](http://api.codeclimate.com/v1/badges/29a55c3bd2dd9e5ed117/test_coverage)](http://codeclimate.com/github/andersondias/attributes_sanitizer/test_coverage)
1
+ [![Maintainability](http://api.codeclimate.com/v1/badges/29a55c3bd2dd9e5ed117/maintainability)](http://codeclimate.com/github/andersondias/attributes_sanitizer/maintainability) [![Test Coverage](http://api.codeclimate.com/v1/badges/29a55c3bd2dd9e5ed117/test_coverage)](http://codeclimate.com/github/andersondias/attributes_sanitizer/test_coverage) [![Build](http://travis-ci.org/andersondias/attributes_sanitizer.svg?branch=master)](http://travis-ci.org/andersondias/attributes_sanitizer.svg?branch=master)
2
2
 
3
3
  # AttributesSanitizer
4
4
 
@@ -35,6 +35,24 @@ AttributesSanitizer.define_sanitizer :reverse do |value|
35
35
  end
36
36
  ```
37
37
 
38
+ It also comes with predefined bundles:
39
+ ```ruby
40
+ class Product < ApplicationRecord
41
+ sanitize_attribute :title, with: -> (value) {
42
+ value.gsub(/[1-9]/, 'X')
43
+ }
44
+
45
+ sanitize_attributes :title, :description, with: :no_tags_emojis_or_extra_spaces
46
+ # same as: `with: %i(stringify strip_tags strip_emojis strip_spaces)
47
+ end
48
+ ```
49
+
50
+ And, finally, you are able to define your own bundles:
51
+ ```ruby
52
+ # config/initializers/attribute_sanitizers.rb
53
+ AttributesSanitizer.define_bundle(:my_bundle, %i(downcase strip_spaces))
54
+ ```
55
+
38
56
  ## Installation
39
57
  Add this line to your application's Gemfile:
40
58
 
@@ -3,20 +3,12 @@ require "attributes_sanitizer/railtie"
3
3
  require "attributes_sanitizer/sanitizer_proc"
4
4
  require "attributes_sanitizer/concern"
5
5
  require "attributes_sanitizer/overrider"
6
+ require "attributes_sanitizer/bundle"
7
+ require "attributes_sanitizer/sanitizers"
6
8
  require "attributes_sanitizer/predefined"
7
9
 
8
10
  module AttributesSanitizer
9
- def self.define_sanitizer(sanitizer_name, &block)
10
- @sanitizers ||= {}
11
- raise ArgumentError, 'sanitizer needs a block' unless block_given?
12
- @sanitizers[sanitizer_name.to_sym] = block
13
- end
14
-
15
- def self.find(sanitizer_name)
16
- sanitizer = @sanitizers && @sanitizers[sanitizer_name.to_sym]
17
- raise ArgumentError, "No sanitizer defined for #{sanitizer}" if sanitizer.nil?
18
- sanitizer
19
- end
20
-
21
- include Predefined
11
+ extend Sanitizers, Bundle
12
+ # must to be called here in order to ensure extension of upper modules
13
+ extend Predefined
22
14
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+ module AttributesSanitizer
3
+ module Bundle
4
+ def define_bundle(bundle_name, keys)
5
+ raise ArgumentError, 'empty bundle name' if bundle_name.blank?
6
+
7
+ keys = Array(keys)
8
+ raise ArgumentError, 'empty keys' if keys.blank?
9
+
10
+ @bundles ||= {}
11
+ @bundles[bundle_name.to_sym] = keys
12
+ end
13
+
14
+ def bundle(bundle_name)
15
+ bundle = @bundles[bundle_name.to_sym]
16
+ bundle&.map do |sanitizer_name|
17
+ find(sanitizer_name)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,11 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
  module AttributesSanitizer
3
3
  module Predefined
4
- extend ActiveSupport::Concern
5
-
6
4
  EMOJI_REGEX = /[^\u0000-\u00FF]/
7
5
 
8
- included do
6
+ def setup_predefined_bundles
7
+ define_bundle(:predefined, @sanitizers.keys)
8
+ define_bundle(:no_tags_emojis_or_extra_spaces, %i(stringify strip_tags strip_emojis strip_spaces))
9
+ @predefined_bundles = @bundles.keys
10
+ end
11
+
12
+ def self.extended(_)
9
13
  AttributesSanitizer.define_sanitizer :stringify do |value|
10
14
  value.to_s
11
15
  end
@@ -29,6 +33,8 @@ module AttributesSanitizer
29
33
  AttributesSanitizer.define_sanitizer :strip_spaces do |value|
30
34
  value.strip
31
35
  end
36
+
37
+ AttributesSanitizer.setup_predefined_bundles
32
38
  end
33
39
  end
34
40
  end
@@ -9,11 +9,9 @@ module AttributesSanitizer
9
9
  raise ArgumentError, "No sanitizer given" if sanitizer.nil?
10
10
 
11
11
  if sanitizer.is_a?(Proc)
12
- @proc = sanitizer
13
- @id = sanitizer.object_id
12
+ setup_lambda_proc(sanitizer)
14
13
  else
15
- @proc = AttributesSanitizer.find(sanitizer)
16
- @id = sanitizer
14
+ setup_defined_proc(sanitizer)
17
15
  end
18
16
  end
19
17
 
@@ -22,7 +20,21 @@ module AttributesSanitizer
22
20
  end
23
21
 
24
22
  def call(value)
25
- @proc.call(value)
23
+ @proc.inject(value) do |value, proc|
24
+ proc.call(value)
25
+ end
26
26
  end
27
+
28
+ private
29
+
30
+ def setup_lambda_proc(sanitizer)
31
+ @proc = Array(sanitizer)
32
+ @id = sanitizer.object_id
33
+ end
34
+
35
+ def setup_defined_proc(sanitizer)
36
+ @id = sanitizer
37
+ @proc = AttributesSanitizer.bundle(sanitizer) || Array(AttributesSanitizer.find(sanitizer))
38
+ end
27
39
  end
28
40
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module AttributesSanitizer
3
+ module Sanitizers
4
+ def define_sanitizer(sanitizer_name, &block)
5
+ @sanitizers ||= {}
6
+ raise ArgumentError, 'sanitizer needs a block' unless block_given?
7
+ @sanitizers[sanitizer_name.to_sym] = block
8
+ end
9
+
10
+ def find(sanitizer_name)
11
+ sanitizer = @sanitizers && @sanitizers[sanitizer_name.to_sym]
12
+ raise ArgumentError, "No sanitizer defined for #{sanitizer_name}" if sanitizer.nil?
13
+ sanitizer
14
+ end
15
+ end
16
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module AttributesSanitizer
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attributes_sanitizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anderson Dias
@@ -49,11 +49,13 @@ files:
49
49
  - README.md
50
50
  - Rakefile
51
51
  - lib/attributes_sanitizer.rb
52
+ - lib/attributes_sanitizer/bundle.rb
52
53
  - lib/attributes_sanitizer/concern.rb
53
54
  - lib/attributes_sanitizer/overrider.rb
54
55
  - lib/attributes_sanitizer/predefined.rb
55
56
  - lib/attributes_sanitizer/railtie.rb
56
57
  - lib/attributes_sanitizer/sanitizer_proc.rb
58
+ - lib/attributes_sanitizer/sanitizers.rb
57
59
  - lib/attributes_sanitizer/version.rb
58
60
  - lib/tasks/attributes_sanitizer_tasks.rake
59
61
  homepage: http://github.com/andersondias/attributes_sanitizer