attributes_sanitizer 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7293d638b6f0162ea95543f432fcae6150687b1a87aad503f3f57950c278b92e
4
+ data.tar.gz: 7c8046d3f6435c229607b336031164f63761c8b58ac7eaafa99f45bcabf3d18e
5
+ SHA512:
6
+ metadata.gz: 81920cb935016e60767ae442105fe540451d700ba3567dd6c611d81b7ab319860ff89ca4f08dd7275bcf65985f164e162220d10214ca2d894205c41daff33779
7
+ data.tar.gz: 051514ccb49100577fcd88ea6bd53d83f1581e2c5c63fc99da1195d8afe8007f080d86915fc519c66b17365b28ac895fb3b35c84a287337164ba9a592ebddd04
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Anderson Dias
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.md ADDED
@@ -0,0 +1,57 @@
1
+ [![Maintainability](https://api.codeclimate.com/v1/badges/29a55c3bd2dd9e5ed117/maintainability)](https://codeclimate.com/github/andersondias/attributes_sanitizer/maintainability)
2
+
3
+ # AttributesSanitizer
4
+
5
+ A simple way to append sanitizers to attributes on Rails.
6
+
7
+
8
+ ## Usage
9
+
10
+ ```ruby
11
+ class Product < ApplicationRecord
12
+ extend AttributesSanitizer::Concern
13
+
14
+ sanitize_attribute :title, with: -> (value) {
15
+ value.gsub(/[1-9]/, 'X')
16
+ }
17
+
18
+ sanitize_attributes :title, :description, with: [:downcase, :strip_tags]
19
+ end
20
+ ```
21
+
22
+ It comes with pre-defined sanitizers:
23
+ - `:downcase` which downcases a given attribute string
24
+ - `:upcase` which upcases a given attribute string
25
+ - `:strip_tags` which removes any tags from the given string based on Rails sanitize helper.
26
+ - `:strip_emojis` which removes any emoji from the given string
27
+ - `:strip_spaces` which removes any white spaces from the beginning and end of given attribute
28
+
29
+ You might define your own sanitizers:
30
+
31
+ ```ruby
32
+ # config/initializers/attribute_sanitizers.rb
33
+
34
+ AttributesSanitizer.define_sanitizer :reverse do |value|
35
+ value.to_s.reverse
36
+ end
37
+ ```
38
+
39
+ ## Installation
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem 'attributes_sanitizer', git: 'https://github.com/andersondias/attributes_sanitizer.git'
44
+ ```
45
+
46
+ And then execute:
47
+ ```bash
48
+ $ bundle
49
+ ```
50
+
51
+ Or install it yourself as:
52
+ ```bash
53
+ $ gem install attributes_sanitizer
54
+ ```
55
+
56
+ ## License
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AttributesSanitizer'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,50 @@
1
+ module AttributesSanitizer::Concern
2
+ def self.extended(klass)
3
+ klass.cattr_accessor :attributes_sanitize_map
4
+ klass.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def sanitize_attributes(*attributes)
9
+ self.attributes_sanitize_map ||= {}
10
+
11
+ fetch_sanitizers_from_options(attributes).each do |sanitizer|
12
+ sanitizer = AttributesSanitizer::SanitizerProc.new(sanitizer)
13
+
14
+ attributes.each do |attribute|
15
+ add_sanitizer_to_attribute(attribute, sanitizer)
16
+ end
17
+ end
18
+
19
+ AttributesSanitizer::Overrider.new(self).override_getters_and_setters
20
+ end
21
+ alias_method :sanitize_attribute, :sanitize_attributes
22
+
23
+ def execute_sanitizers_for(attribute, value)
24
+ return value if self.attributes_sanitize_map.blank? || value.nil?
25
+
26
+ self.attributes_sanitize_map[attribute].reduce(value) do |value, sanitizer|
27
+ sanitizer.call(value)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def fetch_sanitizers_from_options(attributes)
34
+ defaults = attributes.extract_options!.dup
35
+ sanitizers = Array(defaults[:with])
36
+
37
+ raise ArgumentError, "You need to supply at least one attribute" if attributes.empty?
38
+ raise ArgumentError, "You need to supply at least one sanitize method" if sanitizers.empty?
39
+
40
+ sanitizers
41
+ end
42
+
43
+ def add_sanitizer_to_attribute(attribute, sanitizer)
44
+ self.attributes_sanitize_map[attribute] ||= []
45
+ unless self.attributes_sanitize_map[attribute].include?(sanitizer)
46
+ self.attributes_sanitize_map[attribute] << sanitizer
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,49 @@
1
+ module AttributesSanitizer
2
+ class Overrider
3
+ attr_reader :klass
4
+
5
+ def initialize(klass)
6
+ @klass = klass
7
+ end
8
+
9
+ def override_getters_and_setters
10
+ return if klass.attributes_sanitize_map.blank?
11
+
12
+ attributes_to_override.each do |attribute|
13
+ override_getter(attribute)
14
+ override_setter(attribute)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def attributes_to_override
21
+ klass.attributes_sanitize_map.keys
22
+ end
23
+
24
+ def override_method(method_name, &block)
25
+ return if klass.method_defined?(method_name)
26
+ klass.define_method(method_name, &block)
27
+ end
28
+
29
+ def override_getter(attribute)
30
+ getter = attribute.to_sym
31
+ override_method(getter) do
32
+ attribute_value = self[getter.to_s]
33
+ return if attribute_value.nil?
34
+
35
+ self.class.execute_sanitizers_for(attribute, attribute_value)
36
+ end
37
+ end
38
+
39
+ def override_setter(attribute)
40
+ override_method(:"#{attribute}=") do |new_value|
41
+ if new_value.present?
42
+ new_value = self.class.execute_sanitizers_for(attribute, new_value)
43
+ end
44
+
45
+ super(new_value)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,28 @@
1
+ module AttributesSanitizer
2
+ module Predefined
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ AttributesSanitizer.define_sanitizer :downcase do |value|
7
+ value.downcase
8
+ end
9
+
10
+ AttributesSanitizer.define_sanitizer :upcase do |value|
11
+ value.upcase
12
+ end
13
+
14
+ AttributesSanitizer.define_sanitizer :strip_tags do |value|
15
+ ActionController::Base.helpers.sanitize(value, tags: [])
16
+ end
17
+
18
+ AttributesSanitizer.define_sanitizer :strip_emojis do |value|
19
+ value.gsub(AttributesSanitizer::EMOJI_REGEX, '')
20
+ end
21
+
22
+ AttributesSanitizer.define_sanitizer :strip_spaces do |value|
23
+ value.strip
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,4 @@
1
+ module AttributesSanitizer
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,29 @@
1
+ module AttributesSanitizer
2
+ class SanitizerProc
3
+ include Comparable
4
+
5
+ attr_reader :id
6
+
7
+ def initialize(sanitizer)
8
+ raise ArgumentError, "No sanitizer given" if sanitizer.nil?
9
+
10
+ if sanitizer.is_a?(Proc)
11
+ @proc = sanitizer
12
+ @id = sanitizer.object_id
13
+ else
14
+ @proc = AttributesSanitizer.sanitizers[sanitizer]
15
+ raise ArgumentError, "No attribute sanitizer defined for #{sanitizer}" if @proc.nil?
16
+
17
+ @id = sanitizer
18
+ end
19
+ end
20
+
21
+ def <=>(another_proc)
22
+ self.id <=> another_proc.id
23
+ end
24
+
25
+ def call(value)
26
+ @proc.call(value)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module AttributesSanitizer
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,51 @@
1
+ require "attributes_sanitizer/railtie"
2
+ require "attributes_sanitizer/sanitizer_proc"
3
+ require "attributes_sanitizer/concern"
4
+ require "attributes_sanitizer/overrider"
5
+ require "attributes_sanitizer/predefined"
6
+
7
+ #
8
+ # Attributes sanitizer for Rails
9
+ #
10
+ # A simple way to append sanitizers to attributes on Rails.
11
+ #
12
+ # ```ruby
13
+ # class Product < ApplicationRecord
14
+ # extend AttributesSanitizer::Concern
15
+ #
16
+ # sanitize_attribute :title, with: -> (value) {
17
+ # value.gsub(/[1-9]/, 'X')
18
+ # }
19
+ #
20
+ # sanitize_attributes :title, :description, with: [:downcase, :strip_tags]
21
+ # end
22
+ # ```
23
+ #
24
+ # It comes with pre-defined sanitizers:
25
+ # - `:downcase` which downcases a given attribute string
26
+ # - `:upcase` which upcases a given attribute string
27
+ # - `:strip_tags` which removes any tags from the given string based on Rails sanitize helper.
28
+ # - `:strip_emojis` which removes any emoji from the given string
29
+ # - `:strip_spaces` which removes any white spaces from the beginning and end of given attribute
30
+ #
31
+ # You might define your own sanitizers:
32
+ #
33
+ # ```ruby
34
+ # # config/initializers/attribute_sanitizers.rb
35
+ #
36
+ # AttributesSanitizer.define_sanitizer :reverse do |value|
37
+ # value.to_s.reverse
38
+ # end
39
+ # ```
40
+ module AttributesSanitizer
41
+ EMOJI_REGEX = /[^\u0000-\u00FF]/
42
+
43
+ cattr_accessor :sanitizers
44
+ self.sanitizers = {}
45
+
46
+ def self.define_sanitizer(sanitizer_name, &block)
47
+ self.sanitizers[sanitizer_name.to_sym] = block
48
+ end
49
+
50
+ include Predefined
51
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :attributes_sanitizer do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attributes_sanitizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anderson Dias
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A simple way to append sanitizers to attributes on Rails.
42
+ email:
43
+ - andersondaraujo@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/attributes_sanitizer.rb
52
+ - lib/attributes_sanitizer/concern.rb
53
+ - lib/attributes_sanitizer/overrider.rb
54
+ - lib/attributes_sanitizer/predefined.rb
55
+ - lib/attributes_sanitizer/railtie.rb
56
+ - lib/attributes_sanitizer/sanitizer_proc.rb
57
+ - lib/attributes_sanitizer/version.rb
58
+ - lib/tasks/attributes_sanitizer_tasks.rake
59
+ homepage: https://github.com/andersondias/attributes_sanitizer
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ changelog_uri: https://github.com/andersondias/attributes_sanitizer/blob/master/CHANGELOG.md
64
+ homepage_uri: https://github.com/andersondias/attributes_sanitizer
65
+ source_code_uri: https://github.com/andersondias/attributes_sanitizer
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.7.7
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Attributes sanitizer for Rails 5+.
86
+ test_files: []