activecleaner 0.3.1 → 0.3.2

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
- SHA1:
3
- metadata.gz: 158e7ce1e853b39bf241a0bfbe3d50b7e583b18e
4
- data.tar.gz: 5d6da62887b63399be53d73c00e5dda6a77f008c
2
+ SHA256:
3
+ metadata.gz: db7af709547b9681d4d402b9e4f7d58afd93285f18699653d205d91bf2de76c7
4
+ data.tar.gz: c0c308d472f8c4a47b9c0cacdb999fad674ff048fc3fe5fabd1bbd3fb381c9d3
5
5
  SHA512:
6
- metadata.gz: 7ae1bd28121eb544095c27f801b6d20b15cef8e3b0f790ac6153728b178acd84b4d35ac182a27c34bce1559f41358e967f0a5dfd0a60d7e3810a715388bb2659
7
- data.tar.gz: 3e04e8552411cd72504bb451fa76c822b48443bac2f299473efe3bf623ba3207ea0115e802be5812321bc6dbe781b6421fd03e32fe08a68ba7459c569d3245c4
6
+ metadata.gz: da651be9a0a08b68d4456fe0a225602474615a6ceeea90fb32a255e5e4b82de5645cccdad40919e175d8d508a782df2c6db265be0f2ac56fd024b08ad820d503
7
+ data.tar.gz: 676b0c47a6e16edea5f862fd3c89b8e35305e386a9bbedd2bdd34340fbc2788330e60ca575e547e566c218efb6aaf6f820ecb792d60a87572ba07f5349ccaaf2
@@ -1,52 +1,69 @@
1
- # encoding: utf-8
2
-
3
- require 'active_support'
4
- #require 'active_support/rails'
5
- require 'active_model'
6
-
7
- require 'active_cleaner/helper_methods'
8
-
9
- require 'active_cleaner/base_cleaner'
10
- require 'active_cleaner/string_cleaner'
11
- require 'active_cleaner/text_cleaner'
12
- require 'active_cleaner/markdown_cleaner'
13
- require 'active_cleaner/utf8mb3_cleaner'
14
-
15
- require 'active_cleaner/version'
16
-
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_model"
5
+
6
+ require "active_cleaner/helper_methods"
7
+
8
+ require "active_cleaner/base_cleaner"
9
+ require "active_cleaner/string_cleaner"
10
+ require "active_cleaner/text_cleaner"
11
+ require "active_cleaner/markdown_cleaner"
12
+ require "active_cleaner/utf8mb3_cleaner"
13
+
14
+ require "active_cleaner/version"
15
+
16
+ # = ActiveCleaner
17
+ #
18
+ # See HelperMethods for the DSL.
19
+ #
20
+ # == Example
21
+ #
22
+ # class Post
23
+ # include Mongoid::Document
24
+ # include ActiveCleaner
25
+ #
26
+ # field :title
27
+ # field :subtitle
28
+ # clean :title, :subtitle, nilify: true
29
+ #
30
+ # field :body
31
+ # clean :body, as: :text, nilify: true
32
+ # end
17
33
  module ActiveCleaner
34
+
18
35
  extend ActiveSupport::Concern
19
36
 
20
37
  included do
21
-
22
38
  include ActiveModel::Validations
23
39
 
24
40
  extend HelperMethods
25
41
  include HelperMethods
26
42
 
27
- define_callbacks :cleaning, :scope => :name
43
+ define_callbacks :cleaning, scope: :name
28
44
 
29
45
  class_attribute :_cleaners
30
- self._cleaners = Hash.new { |h,k| h[k] = [] }
46
+ self._cleaners = Hash.new { |h, k| h[k] = [] }
31
47
 
32
48
  set_callback :validate, :before, :run_cleaners!
49
+ end
33
50
 
34
- end # included
35
-
36
-
37
- module ClassMethods
51
+ module ClassMethods #:nodoc:
38
52
 
39
53
  def inherited(base) #:nodoc:
40
54
  dup = _cleaners.dup
41
- base._cleaners = dup.each { |k, v| dup[k] = v.dup }
55
+ base._cleaners =
56
+ dup.each do |attr_name, cleaner|
57
+ dup[attr_name] = cleaner.dup
58
+ end
42
59
  super
43
60
  end
44
61
 
45
- end # ClassMethods
46
-
62
+ end
47
63
 
64
+ # Do run the cleaners
48
65
  def run_cleaners!
49
- self._cleaners.each do |attr_name, cleaners|
66
+ _cleaners.each do |_attr_name, cleaners|
50
67
  cleaners.each do |cleaner|
51
68
  cleaner.clean(self)
52
69
  end
@@ -55,9 +72,12 @@ module ActiveCleaner
55
72
  true
56
73
  end
57
74
 
75
+ # Method used by the cleaners to read the value of an attribute.
58
76
  def read_attribute_for_cleaning(attr_name)
59
77
  send(attr_name)
60
78
  end
79
+
80
+ # Method used by the cleaners to write the value of an attribute.
61
81
  def write_attribute_after_cleaning(attr_name, value)
62
82
  send(:"#{attr_name}=", value)
63
83
  end
@@ -1,26 +1,44 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveCleaner
4
+ # = The base cleaner.
5
+ #
6
+ # Every cleaner inherit from it.
7
+ #
8
+ # class MyCleaner < ActiveCleaner::BaseCleaner
9
+ #
10
+ # def clean_value(old_value, record = nil)
11
+ # old_value.gsub("foo", "bar")
12
+ # end
13
+ #
14
+ # end
4
15
  class BaseCleaner
5
16
 
6
- attr_reader :attr_name, :options
17
+ # Attribute name
18
+ attr_reader :attr_name
19
+
20
+ # Options given to the cleaner.
21
+ attr_reader :options
7
22
 
8
23
  # Accepts options that will be made available through the +options+ reader.
9
24
  def initialize(attr_name, options = {})
10
25
  @attr_name = attr_name
11
26
  @options = {
12
- :nilify => false,
27
+ nilify: false,
13
28
  }.merge(options).freeze
14
29
  end
15
30
 
31
+ # The kind of the cleaner.
16
32
  def self.kind
17
- @kind ||= name.split('::').last.underscore.sub(/_cleaner$/, '').to_sym
33
+ @kind ||= name.split("::").last.underscore.sub(/_cleaner$/, "").to_sym
18
34
  end
19
35
 
36
+ # The kind of the cleaner.
20
37
  def kind
21
38
  self.class.kind
22
39
  end
23
40
 
41
+ # Cleans the record by extracting the value of the field, cleaning it, and setting it back.
24
42
  def clean(record)
25
43
  value = record.read_attribute_for_cleaning(attr_name)
26
44
 
@@ -31,15 +49,23 @@ module ActiveCleaner
31
49
  record.write_attribute_after_cleaning(attr_name, new_value) unless new_value == value
32
50
  end
33
51
 
34
- def clean_value(value, record=nil)
52
+ # Cleans the value.
53
+ #
54
+ # It is expected that the returned value is the cleaned value.
55
+ #
56
+ # The method needs to be implemented in the subclasses.
57
+ def clean_value(_old_value, _record = nil)
35
58
  raise NotImplementedError, "Subclasses must implement a clean(value, record=nil) method."
36
59
  end
37
60
 
38
- # feel free to subclass for your custom cleaner
39
- def nilify_value?(value, record=nil)
61
+ # Tests whether or not the value should be nilified.
62
+ #
63
+ # This can be changed in the subclasses.
64
+ def nilify_value?(value, _record = nil)
40
65
  value == ""
41
66
  end
42
67
 
68
+ # Test whether or not two cleaners are equal.
43
69
  def ==(other)
44
70
  kind == other.kind && attr_name == other.attr_name && options == other.options
45
71
  end
@@ -1,8 +1,27 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveCleaner
4
+ # Defines the DSL methods available in your model.
4
5
  module HelperMethods
5
6
 
7
+ # Registers a cleaner to a bunch of fields by its name.
8
+ #
9
+ # === Options
10
+ #
11
+ # [:as]
12
+ # the kind of the cleaner. Default to +:string+
13
+ #
14
+ # Extra options are passed to the cleaner.
15
+ #
16
+ # === Example
17
+ #
18
+ # class MyModel
19
+ # include ActiveCleaner
20
+ #
21
+ # clean :name, nilify: false
22
+ # clean :firstname, :lastname, nilify: false
23
+ # clean :resume, as: :markdown
24
+ # end
6
25
  def clean(*attr_names)
7
26
  options = attr_names.extract_options!.symbolize_keys
8
27
  attr_names.flatten!
@@ -16,8 +35,15 @@ module ActiveCleaner
16
35
  end
17
36
  end
18
37
 
38
+ # Registers a cleaner by an instance of it.
39
+ #
40
+ # class MyModel
41
+ # include ActiveCleaner
42
+ #
43
+ # clean_with ActiveCleaner::StringCleaner.new(:name, nilify: false)
44
+ # end
19
45
  def clean_with(cleaner)
20
- self._cleaners[cleaner.attr_name] << cleaner
46
+ _cleaners[cleaner.attr_name] << cleaner
21
47
  end
22
48
 
23
49
  end
@@ -1,9 +1,37 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveCleaner
4
+ # = MarkdownCleaner
5
+ #
6
+ # Cleans a string by squishing all the extra space characters, but preserves new lines
7
+ # (with a max of 2 successive new lines) and spaces in the beginning of lines (the indentation).
8
+ #
9
+ # Useful for Markdown.
10
+ #
11
+ # It turns <tt>" My todo \n * todo 1 \n * todo 2 \t "</tt> into <tt>"My todo\n * todo 1\n * todo 2"</tt>.
12
+ #
13
+ # == Options
14
+ #
15
+ # [:nilify]
16
+ # Whether or not set the field to +nil+ when the field was or is cleaned to <tt>""</tt>.
17
+ # Default to +false+.
18
+ #
19
+ # == Example
20
+ #
21
+ # class Article
22
+ # include ActiveCleaner
23
+ #
24
+ # clean :body, as: :markdown
25
+ # end
26
+ #
27
+ # article = Article.new(body: " My todo \n * todo 1 \n * todo 2 \t ")
28
+ # article.save
29
+ # article.body
30
+ # # => "My todo\n * todo 1\n * todo 2"
4
31
  class MarkdownCleaner < BaseCleaner
5
32
 
6
- def clean_value(old_value, record=nil)
33
+ # Cleans the value.
34
+ def clean_value(old_value, _record = nil)
7
35
  case old_value
8
36
  when String
9
37
  value = old_value.dup
@@ -15,7 +43,7 @@ module ActiveCleaner
15
43
 
16
44
  # protect stuff to keep with a markup
17
45
  value.gsub!(/\n/, "__NEW_LINE__")
18
- value.gsub!(/(?<=__NEW_LINE__)\s+/) {|match| match.gsub(/\s/, "__SPACE__")}
46
+ value.gsub!(/(?<=__NEW_LINE__)\s+/) { |match| match.gsub(/\s/, "__SPACE__") }
19
47
 
20
48
  value.gsub!(/\s+/, " ")
21
49
  value.gsub!(/(__SPACE__|\s)*__NEW_LINE__\s*/, "__NEW_LINE__")
@@ -1,9 +1,34 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveCleaner
4
+ # = StringCleaner
5
+ #
6
+ # Cleans a string by squishing all the extra space characters.
7
+ #
8
+ # It turns <tt>" A \n \t title \t "</tt> into <tt>"A title"</tt>.
9
+ #
10
+ # == Options
11
+ #
12
+ # [:nilify]
13
+ # Whether or not set the field to +nil+ when the field was or is cleaned to <tt>""</tt>.
14
+ # Default to +false+.
15
+ #
16
+ # == Example
17
+ #
18
+ # class Article
19
+ # include ActiveCleaner
20
+ #
21
+ # clean :title, as: :string
22
+ # end
23
+ #
24
+ # article = Article.new(title: " My \n \t Title \t ")
25
+ # article.save
26
+ # article.title
27
+ # # => "My Title"
4
28
  class StringCleaner < BaseCleaner
5
29
 
6
- def clean_value(old_value, record=nil)
30
+ # Cleans the value.
31
+ def clean_value(old_value, _record = nil)
7
32
  case old_value
8
33
  when String
9
34
  value = old_value.dup
@@ -1,9 +1,37 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveCleaner
4
+ # = TextCleaner
5
+ #
6
+ # Cleans a string by squishing all the extra space characters, but preserves new lines
7
+ # (with a max of 2 successive new lines).
8
+ #
9
+ # Useful when the field is rendered with the +simple_format+ Rails helper.
10
+ #
11
+ # It turns <tt>" My first line \n My second line \t "</tt> into <tt>"My first line\nMy second line"</tt>.
12
+ #
13
+ # == Options
14
+ #
15
+ # [:nilify]
16
+ # Whether or not set the field to +nil+ when the field was or is cleaned to <tt>""</tt>.
17
+ # Default to +false+.
18
+ #
19
+ # == Example
20
+ #
21
+ # class Article
22
+ # include ActiveCleaner
23
+ #
24
+ # clean :body, as: :text
25
+ # end
26
+ #
27
+ # article = Article.new(body: " My first paragraph \n \n \t \n My second paragraph, \n longer. \t ")
28
+ # article.save
29
+ # article.body
30
+ # # => "My first paragraph\n\nMy second paragraph,\nlonger."
4
31
  class TextCleaner < BaseCleaner
5
32
 
6
- def clean_value(old_value, record=nil)
33
+ # Cleans the value.
34
+ def clean_value(old_value, _record = nil)
7
35
  case old_value
8
36
  when String
9
37
  value = old_value.dup
@@ -1,12 +1,41 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveCleaner
4
+ # = Utf8mb3Cleaner
5
+ #
6
+ # Cleans a string by removes all 4-bytes encoded chars in UTF8 strings that mess with
7
+ # the +utf8mb3+ (also simply known as +utf8+) encoding in MySQL.
8
+ #
9
+ # Useful for user input that may contain iOS6 emojis for example (as they are only compatible
10
+ # with +utf8mb4+ and cause string truncation, at best).
11
+ #
12
+ # It turns <tt>"String with emoticon 😀"</tt> into <tt>"String with emoticon"</tt>.
13
+ #
14
+ # == Options
15
+ #
16
+ # [:nilify]
17
+ # Whether or not set the field to +nil+ when the field was or is cleaned to <tt>""</tt>.
18
+ # Default to +false+.
19
+ #
20
+ # == Example
21
+ #
22
+ # class Comment
23
+ # include ActiveCleaner
24
+ #
25
+ # clean :body, as: :utf8mb3
26
+ # end
27
+ #
28
+ # comment = Comment.new(body: "Nice! 😀")
29
+ # comment.save
30
+ # comment.body
31
+ # # => "Nice!"
4
32
  class Utf8mb3Cleaner < BaseCleaner
5
33
 
6
- def clean_value(old_value, record=nil)
34
+ # Cleans the value.
35
+ def clean_value(old_value, _record = nil)
7
36
  case old_value
8
37
  when String
9
- old_value.each_char.select { |char| char.bytesize < 4 }.join('')
38
+ old_value.each_char.select { |char| char.bytesize < 4 }.join
10
39
  else
11
40
  old_value
12
41
  end
@@ -1,8 +1,11 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module ActiveCleaner
4
+ # Contains the version of the gem.
3
5
  module Version
4
6
 
5
- STRING = '0.3.1'
7
+ # The version as string.
8
+ STRING = "0.3.2".freeze
6
9
 
7
10
  end
8
11
  end
@@ -1,3 +1,3 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'active_cleaner'
3
+ require "active_cleaner"
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require "spec_helper"
3
4
 
4
5
  #
@@ -8,28 +9,30 @@ require "spec_helper"
8
9
  # in a common STI scenario.
9
10
  #
10
11
  class Ad
12
+
11
13
  include ActiveCleaner
12
14
 
13
15
  attr_accessor :title, :name
14
16
 
15
17
  clean :title
16
18
  clean :name, as: :string
19
+
17
20
  end
18
21
 
19
22
  class CarAd < Ad
23
+
20
24
  attr_accessor :body, :user_generated_content
21
25
 
22
26
  clean :body, as: :text
23
27
  clean :user_generated_content, as: :utf8mb3
28
+
24
29
  end
25
30
 
26
31
  #
27
32
  # The specs
28
33
  #
29
34
  describe "Case: an ad and his inherited car ad" do
30
-
31
35
  describe Ad, "._cleaners" do
32
-
33
36
  subject { Ad._cleaners }
34
37
 
35
38
  it "has 2 cleaners" do
@@ -43,11 +46,9 @@ describe "Case: an ad and his inherited car ad" do
43
46
  it "includes a StringCleaner for #name" do
44
47
  expect(subject[:name].first).to eq(ActiveCleaner::StringCleaner.new(:name))
45
48
  end
46
-
47
- end # describe
49
+ end
48
50
 
49
51
  describe CarAd, "._cleaners" do
50
-
51
52
  subject { CarAd._cleaners }
52
53
 
53
54
  it "has 4 cleaners" do
@@ -67,17 +68,15 @@ describe "Case: an ad and his inherited car ad" do
67
68
  end
68
69
 
69
70
  it "includes a Utf8mb3Cleaner for #user_generated_content" do
70
- expect(subject[:user_generated_content].first).to eq(ActiveCleaner::Utf8mb3Cleaner.new(:user_generated_content))
71
+ expect(subject[:user_generated_content].first)
72
+ .to eq(ActiveCleaner::Utf8mb3Cleaner.new(:user_generated_content))
71
73
  end
72
-
73
- end # describe
74
+ end
74
75
 
75
76
  context "considering a car ad" do
76
-
77
77
  let(:subject) { CarAd.new }
78
78
 
79
79
  describe "#title, marked as to clean with no type" do
80
-
81
80
  it "is untouched when legit" do
82
81
  subject.title = "A good title!"
83
82
  subject.valid?
@@ -89,11 +88,9 @@ describe "Case: an ad and his inherited car ad" do
89
88
  subject.valid?
90
89
  expect(subject.title).to eq("A good title!")
91
90
  end
92
-
93
- end # describe
91
+ end
94
92
 
95
93
  describe "#name, marked as to clean as a string" do
96
-
97
94
  it "is untouched when legit" do
98
95
  subject.name = "John Doe"
99
96
  subject.valid?
@@ -105,11 +102,9 @@ describe "Case: an ad and his inherited car ad" do
105
102
  subject.valid?
106
103
  expect(subject.name).to eq("John Doe")
107
104
  end
108
-
109
- end # describe
105
+ end
110
106
 
111
107
  describe "#body, marked as to clean as a text" do
112
-
113
108
  it "is untouched when legit" do
114
109
  subject.body = "Lorem ipsum\ndolor sit amet.\n\nLorem."
115
110
  subject.valid?
@@ -121,11 +116,9 @@ describe "Case: an ad and his inherited car ad" do
121
116
  subject.valid?
122
117
  expect(subject.body).to eq("Lorem ipsum\ndolor sit amet.\n\nLorem.")
123
118
  end
124
-
125
- end # describe
119
+ end
126
120
 
127
121
  describe "#user_generated_content, marked as to clean as utf8mb3" do
128
-
129
122
  it "is untouched when legit" do
130
123
  subject.user_generated_content = "A good user generated content!"
131
124
  subject.valid?
@@ -137,9 +130,6 @@ describe "Case: an ad and his inherited car ad" do
137
130
  subject.valid?
138
131
  expect(subject.user_generated_content).to eq("A good user generated content!")
139
132
  end
140
-
141
- end # describe
142
-
143
- end # context
144
-
145
- end # describe
133
+ end
134
+ end
135
+ end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require "spec_helper"
3
4
 
4
5
  #
@@ -7,6 +8,7 @@ require "spec_helper"
7
8
  # This is to demonstrate when we want to clean some simple fields and nulify them
8
9
  #
9
10
  class OptimizedPost
11
+
10
12
  include ActiveCleaner
11
13
 
12
14
  attr_accessor :title, :name, :body, :user_generated_content
@@ -15,16 +17,14 @@ class OptimizedPost
15
17
  clean :name, as: :string, nilify: true
16
18
  clean :body, as: :text, nilify: true
17
19
  clean :user_generated_content, as: :utf8mb3, nilify: true
18
- end
19
20
 
21
+ end
20
22
 
21
23
  #
22
24
  # The specs
23
25
  #
24
26
  describe "Case: a simple post with nulify" do
25
-
26
27
  describe OptimizedPost, "._cleaners" do
27
-
28
28
  subject { OptimizedPost._cleaners }
29
29
 
30
30
  it "has 4 cleaners" do
@@ -44,17 +44,15 @@ describe "Case: a simple post with nulify" do
44
44
  end
45
45
 
46
46
  it "includes a Utf8mb3Cleaner for #user_generated_content" do
47
- expect(subject[:user_generated_content].first).to eq(ActiveCleaner::Utf8mb3Cleaner.new(:user_generated_content, nilify: true))
47
+ expect(subject[:user_generated_content].first)
48
+ .to eq(ActiveCleaner::Utf8mb3Cleaner.new(:user_generated_content, nilify: true))
48
49
  end
49
-
50
- end # describe
50
+ end
51
51
 
52
52
  context "considering a post" do
53
-
54
53
  let(:subject) { OptimizedPost.new }
55
54
 
56
55
  describe "#title, marked as to clean with no type" do
57
-
58
56
  it "is untouched when legit" do
59
57
  subject.title = "A good title!"
60
58
  subject.valid?
@@ -66,11 +64,9 @@ describe "Case: a simple post with nulify" do
66
64
  subject.valid?
67
65
  expect(subject.title).to be_nil
68
66
  end
69
-
70
- end # describe
67
+ end
71
68
 
72
69
  describe "#name, marked as to clean as a string" do
73
-
74
70
  it "is untouched when legit" do
75
71
  subject.name = "John Doe"
76
72
  subject.valid?
@@ -82,11 +78,9 @@ describe "Case: a simple post with nulify" do
82
78
  subject.valid?
83
79
  expect(subject.title).to be_nil
84
80
  end
85
-
86
- end # describe
81
+ end
87
82
 
88
83
  describe "#body, marked as to clean as a text" do
89
-
90
84
  it "is untouched when legit" do
91
85
  subject.body = "Lorem ipsum\ndolor sit amet.\n\nLorem."
92
86
  subject.valid?
@@ -98,11 +92,9 @@ describe "Case: a simple post with nulify" do
98
92
  subject.valid?
99
93
  expect(subject.title).to be_nil
100
94
  end
101
-
102
- end # describe
95
+ end
103
96
 
104
97
  describe "#user_generated_content, marked as to clean as utf8mb3" do
105
-
106
98
  it "is untouched when legit" do
107
99
  subject.user_generated_content = "A good user generated content!"
108
100
  subject.valid?
@@ -114,9 +106,6 @@ describe "Case: a simple post with nulify" do
114
106
  subject.valid?
115
107
  expect(subject.user_generated_content).to be_nil
116
108
  end
117
-
118
- end # describe
119
-
120
- end # context
121
-
122
- end # describe
109
+ end
110
+ end
111
+ end
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require "spec_helper"
3
4
 
4
5
  #
@@ -7,6 +8,7 @@ require "spec_helper"
7
8
  # This is to demonstrate when we want to clean some simple fields
8
9
  #
9
10
  class Post
11
+
10
12
  include ActiveCleaner
11
13
 
12
14
  attr_accessor :title, :name, :body, :user_generated_content
@@ -15,16 +17,14 @@ class Post
15
17
  clean :name, as: :string
16
18
  clean :body, as: :text
17
19
  clean :user_generated_content, as: :utf8mb3
18
- end
19
20
 
21
+ end
20
22
 
21
23
  #
22
24
  # The specs
23
25
  #
24
26
  describe "Case: a simple post" do
25
-
26
27
  describe Post, "._cleaners" do
27
-
28
28
  subject { Post._cleaners }
29
29
 
30
30
  it "has 4 cleaners" do
@@ -44,17 +44,15 @@ describe "Case: a simple post" do
44
44
  end
45
45
 
46
46
  it "includes a Utf8mb3Cleaner for #user_generated_content" do
47
- expect(subject[:user_generated_content].first).to eq(ActiveCleaner::Utf8mb3Cleaner.new(:user_generated_content))
47
+ expect(subject[:user_generated_content].first)
48
+ .to eq(ActiveCleaner::Utf8mb3Cleaner.new(:user_generated_content))
48
49
  end
49
-
50
- end # describe
50
+ end
51
51
 
52
52
  context "considering a post" do
53
-
54
53
  let(:subject) { Post.new }
55
54
 
56
55
  describe "#title, marked as to clean with no type" do
57
-
58
56
  it "is untouched when legit" do
59
57
  subject.title = "A good title!"
60
58
  subject.valid?
@@ -66,11 +64,9 @@ describe "Case: a simple post" do
66
64
  subject.valid?
67
65
  expect(subject.title).to eq("A good title!")
68
66
  end
69
-
70
- end # describe
67
+ end
71
68
 
72
69
  describe "#name, marked as to clean as a string" do
73
-
74
70
  it "is untouched when legit" do
75
71
  subject.name = "John Doe"
76
72
  subject.valid?
@@ -82,11 +78,9 @@ describe "Case: a simple post" do
82
78
  subject.valid?
83
79
  expect(subject.name).to eq("John Doe")
84
80
  end
85
-
86
- end # describe
81
+ end
87
82
 
88
83
  describe "#body, marked as to clean as a text" do
89
-
90
84
  it "is untouched when legit" do
91
85
  subject.body = "Lorem ipsum\ndolor sit amet.\n\nLorem."
92
86
  subject.valid?
@@ -98,11 +92,9 @@ describe "Case: a simple post" do
98
92
  subject.valid?
99
93
  expect(subject.body).to eq("Lorem ipsum\ndolor sit amet.\n\nLorem.")
100
94
  end
101
-
102
- end # describe
95
+ end
103
96
 
104
97
  describe "#user_generated_content, marked as to clean as utf8mb3" do
105
-
106
98
  it "is untouched when legit" do
107
99
  subject.user_generated_content = "A good user generated content!"
108
100
  subject.valid?
@@ -114,9 +106,6 @@ describe "Case: a simple post" do
114
106
  subject.valid?
115
107
  expect(subject.user_generated_content).to eq("A good user generated content!")
116
108
  end
117
-
118
- end # describe
119
-
120
- end # context
121
-
122
- end # describe
109
+ end
110
+ end
111
+ end
@@ -1,12 +1,11 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
1
+ # frozen_string_literal: true
3
2
 
4
- describe ActiveCleaner::MarkdownCleaner do
3
+ require "spec_helper"
5
4
 
5
+ describe ActiveCleaner::MarkdownCleaner do
6
6
  let(:cleaner) { ActiveCleaner::MarkdownCleaner.new(:body) }
7
7
 
8
8
  describe "#clean_value" do
9
-
10
9
  it "doesn't touch non string value" do
11
10
  expect(cleaner.clean_value(nil)).to eq(nil)
12
11
  expect(cleaner.clean_value(true)).to eq(true)
@@ -14,8 +13,15 @@ describe ActiveCleaner::MarkdownCleaner do
14
13
  expect(cleaner.clean_value(10)).to eq(10)
15
14
  end
16
15
 
16
+ it "doesn't modify input string" do
17
+ input = "Lorem ipsum"
18
+ expect {
19
+ cleaner.clean_value(input)
20
+ }.not_to change { input }
21
+ end
22
+
17
23
  it "doesn't touch legit value" do
18
- body = ""
24
+ body = String.new
19
25
  body << "= Title =\n"
20
26
  body << "\n"
21
27
  body << "A first paragraph.\n"
@@ -48,25 +54,37 @@ describe ActiveCleaner::MarkdownCleaner do
48
54
  end
49
55
 
50
56
  it "cleans repeted spaces" do
51
- expect(cleaner.clean_value("Lorem ipsum \ndolor sit amet.")).to eq("Lorem ipsum\ndolor sit amet.")
52
- expect(cleaner.clean_value("Lorem \t ipsum \t \ndolor \t sit \t amet.")).to eq("Lorem ipsum\ndolor sit amet.")
57
+ expect(cleaner.clean_value("Lorem ipsum \ndolor sit amet."))
58
+ .to eq("Lorem ipsum\ndolor sit amet.")
59
+ expect(cleaner.clean_value("Lorem \t ipsum \t \ndolor \t sit \t amet."))
60
+ .to eq("Lorem ipsum\ndolor sit amet.")
61
+ end
62
+
63
+ it "cleans \\r" do
64
+ expect(cleaner.clean_value("Lorem ipsum\rdolor sit amet."))
65
+ .to eq("Lorem ipsum\ndolor sit amet.")
66
+ expect(cleaner.clean_value("Lorem ipsum\r\ndolor sit amet."))
67
+ .to eq("Lorem ipsum\ndolor sit amet.")
53
68
  end
54
69
 
55
70
  context "considering the spaces in the beggining of lines" do
56
71
  it "preserves them" do
57
- expect(cleaner.clean_value("Lorem ipsum\n dolor sit amet.")).to eq("Lorem ipsum\n dolor sit amet.")
72
+ expect(cleaner.clean_value("Lorem ipsum\n dolor sit amet."))
73
+ .to eq("Lorem ipsum\n dolor sit amet.")
58
74
  end
59
75
  it "clears line full of spaces" do
60
- expect(cleaner.clean_value("Lorem ipsum \n \n dolor sit amet.")).to eq("Lorem ipsum\n\n dolor sit amet.")
76
+ expect(cleaner.clean_value("Lorem ipsum \n \n dolor sit amet."))
77
+ .to eq("Lorem ipsum\n\n dolor sit amet.")
61
78
  end
62
79
  end
63
80
 
64
81
  it "keeps two max succeeding new line" do
65
- expect(cleaner.clean_value("Lorem ipsum\n\n\ndolor sit amet.")).to eq("Lorem ipsum\n\ndolor sit amet.")
66
- expect(cleaner.clean_value("Lorem ipsum\n\n\n\ndolor sit amet.")).to eq("Lorem ipsum\n\ndolor sit amet.")
67
- expect(cleaner.clean_value("Lorem ipsum\n \n \n \ndolor sit amet.")).to eq("Lorem ipsum\n\ndolor sit amet.")
82
+ expect(cleaner.clean_value("Lorem ipsum\n\n\ndolor sit amet."))
83
+ .to eq("Lorem ipsum\n\ndolor sit amet.")
84
+ expect(cleaner.clean_value("Lorem ipsum\n\n\n\ndolor sit amet."))
85
+ .to eq("Lorem ipsum\n\ndolor sit amet.")
86
+ expect(cleaner.clean_value("Lorem ipsum\n \n \n \ndolor sit amet."))
87
+ .to eq("Lorem ipsum\n\ndolor sit amet.")
68
88
  end
69
-
70
89
  end
71
-
72
90
  end
@@ -1,12 +1,11 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
1
+ # frozen_string_literal: true
3
2
 
4
- describe ActiveCleaner::StringCleaner do
3
+ require "spec_helper"
5
4
 
5
+ describe ActiveCleaner::StringCleaner do
6
6
  let(:cleaner) { ActiveCleaner::StringCleaner.new(:title) }
7
7
 
8
8
  describe "#clean_value" do
9
-
10
9
  it "doesn't touch non string value" do
11
10
  expect(cleaner.clean_value(nil)).to eq(nil)
12
11
  expect(cleaner.clean_value(true)).to eq(true)
@@ -14,6 +13,13 @@ describe ActiveCleaner::StringCleaner do
14
13
  expect(cleaner.clean_value(10)).to eq(10)
15
14
  end
16
15
 
16
+ it "doesn't modify input string" do
17
+ input = "Lorem ipsum"
18
+ expect {
19
+ cleaner.clean_value(input)
20
+ }.not_to change { input }
21
+ end
22
+
17
23
  it "doesn't touch legit value" do
18
24
  expect(cleaner.clean_value("A good title!")).to eq("A good title!")
19
25
  end
@@ -33,9 +39,11 @@ describe ActiveCleaner::StringCleaner do
33
39
  it "cleans leading and trailing spaces" do
34
40
  expect(cleaner.clean_value(" A good title! ")).to eq("A good title!")
35
41
  end
42
+
36
43
  it "cleans leading and trailing tabs" do
37
44
  expect(cleaner.clean_value("\tA good title!\t")).to eq("A good title!")
38
45
  end
46
+
39
47
  it "cleans leading and trailing lines" do
40
48
  expect(cleaner.clean_value("\nA good title!\n")).to eq("A good title!")
41
49
  end
@@ -44,7 +52,5 @@ describe ActiveCleaner::StringCleaner do
44
52
  expect(cleaner.clean_value("A good title!")).to eq("A good title!")
45
53
  expect(cleaner.clean_value("A \n good \t title!")).to eq("A good title!")
46
54
  end
47
-
48
55
  end
49
-
50
56
  end
@@ -1,12 +1,11 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
1
+ # frozen_string_literal: true
3
2
 
4
- describe ActiveCleaner::TextCleaner do
3
+ require "spec_helper"
5
4
 
5
+ describe ActiveCleaner::TextCleaner do
6
6
  let(:cleaner) { ActiveCleaner::TextCleaner.new(:text) }
7
7
 
8
8
  describe "#clean_value" do
9
-
10
9
  it "doesn't touch non string value" do
11
10
  expect(cleaner.clean_value(nil)).to eq(nil)
12
11
  expect(cleaner.clean_value(true)).to eq(true)
@@ -14,6 +13,13 @@ describe ActiveCleaner::TextCleaner do
14
13
  expect(cleaner.clean_value(10)).to eq(10)
15
14
  end
16
15
 
16
+ it "doesn't modify input string" do
17
+ input = "Lorem ipsum"
18
+ expect {
19
+ cleaner.clean_value(input)
20
+ }.not_to change { input }
21
+ end
22
+
17
23
  it "doesn't touch legit value" do
18
24
  [
19
25
  "Lorem ipsum dolor sit amet.",
@@ -37,25 +43,39 @@ describe ActiveCleaner::TextCleaner do
37
43
  end
38
44
 
39
45
  it "cleans leading and trailing spaces" do
40
- expect(cleaner.clean_value(" Lorem ipsum\ndolor sit amet. ")).to eq("Lorem ipsum\ndolor sit amet.")
46
+ expect(cleaner.clean_value(" Lorem ipsum\ndolor sit amet. "))
47
+ .to eq("Lorem ipsum\ndolor sit amet.")
41
48
  end
49
+
42
50
  it "cleans leading and trailing tabs" do
43
- expect(cleaner.clean_value("\tLorem ipsum\ndolor sit amet.\t")).to eq("Lorem ipsum\ndolor sit amet.")
51
+ expect(cleaner.clean_value("\tLorem ipsum\ndolor sit amet.\t"))
52
+ .to eq("Lorem ipsum\ndolor sit amet.")
44
53
  end
54
+
45
55
  it "cleans leading and trailing lines" do
46
- expect(cleaner.clean_value("\nLorem ipsum\ndolor sit amet.\n")).to eq("Lorem ipsum\ndolor sit amet.")
56
+ expect(cleaner.clean_value("\nLorem ipsum\ndolor sit amet.\n"))
57
+ .to eq("Lorem ipsum\ndolor sit amet.")
47
58
  end
48
59
 
49
60
  it "cleans repeted spaces" do
50
- expect(cleaner.clean_value("Lorem ipsum \n dolor sit amet.")).to eq("Lorem ipsum\ndolor sit amet.")
51
- expect(cleaner.clean_value("Lorem \t ipsum \t \n dolor \t sit \t amet.")).to eq("Lorem ipsum\ndolor sit amet.")
61
+ expect(cleaner.clean_value("Lorem ipsum \n dolor sit amet."))
62
+ .to eq("Lorem ipsum\ndolor sit amet.")
63
+ expect(cleaner.clean_value("Lorem \t ipsum \t \n dolor \t sit \t amet."))
64
+ .to eq("Lorem ipsum\ndolor sit amet.")
52
65
  end
53
66
 
54
- it "keeps two max succeeding new line" do
55
- expect(cleaner.clean_value("Lorem ipsum\n\n\ndolor sit amet.")).to eq("Lorem ipsum\n\ndolor sit amet.")
56
- expect(cleaner.clean_value("Lorem ipsum\n\n\n\ndolor sit amet.")).to eq("Lorem ipsum\n\ndolor sit amet.")
67
+ it "cleans \\r" do
68
+ expect(cleaner.clean_value("Lorem ipsum\rdolor sit amet."))
69
+ .to eq("Lorem ipsum\ndolor sit amet.")
70
+ expect(cleaner.clean_value("Lorem ipsum\r\ndolor sit amet."))
71
+ .to eq("Lorem ipsum\ndolor sit amet.")
57
72
  end
58
73
 
74
+ it "keeps two max succeeding new line" do
75
+ expect(cleaner.clean_value("Lorem ipsum\n\n\ndolor sit amet."))
76
+ .to eq("Lorem ipsum\n\ndolor sit amet.")
77
+ expect(cleaner.clean_value("Lorem ipsum\n\n\n\ndolor sit amet."))
78
+ .to eq("Lorem ipsum\n\ndolor sit amet.")
79
+ end
59
80
  end
60
-
61
81
  end
@@ -1,12 +1,11 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
1
+ # frozen_string_literal: true
3
2
 
4
- describe ActiveCleaner::Utf8mb3Cleaner do
3
+ require "spec_helper"
5
4
 
5
+ describe ActiveCleaner::Utf8mb3Cleaner do
6
6
  let(:cleaner) { ActiveCleaner::Utf8mb3Cleaner.new(:title) }
7
7
 
8
8
  describe "#clean_value" do
9
-
10
9
  it "doesn't touch non string value" do
11
10
  expect(cleaner.clean_value(nil)).to eq(nil)
12
11
  expect(cleaner.clean_value(true)).to eq(true)
@@ -14,6 +13,13 @@ describe ActiveCleaner::Utf8mb3Cleaner do
14
13
  expect(cleaner.clean_value(10)).to eq(10)
15
14
  end
16
15
 
16
+ it "doesn't modify input string" do
17
+ input = "Lorem 😁 ipsum"
18
+ expect {
19
+ cleaner.clean_value(input)
20
+ }.not_to change { input }
21
+ end
22
+
17
23
  it "doesn't touch legit value" do
18
24
  expect(cleaner.clean_value("A good title!")).to eq("A good title!")
19
25
  end
@@ -30,7 +36,5 @@ describe ActiveCleaner::Utf8mb3Cleaner do
30
36
  it "kepts japanese chars" do
31
37
  expect(cleaner.clean_value("ginkō is written as 銀行")).to eq("ginkō is written as 銀行")
32
38
  end
33
-
34
39
  end
35
-
36
40
  end
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
1
+ # frozen_string_literal: true
4
2
 
3
+ require "spec_helper"
@@ -1,9 +1,8 @@
1
- # encoding: utf-8
2
- require 'rubygems'
3
- require 'rspec'
4
- require 'active_cleaner'
1
+ # frozen_string_literal: true
5
2
 
3
+ require "rubygems"
4
+ require "rspec"
5
+ require "active_cleaner"
6
6
 
7
- RSpec.configure do |config|
8
-
7
+ RSpec.configure do |_config|
9
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activecleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxime Garcia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-22 00:00:00.000000000 Z
11
+ date: 2017-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -16,64 +16,72 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.1'
19
+ version: '4.2'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5'
22
+ version: '5.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '3.1'
29
+ version: '4.2'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5'
32
+ version: '5.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: '3.1'
39
+ version: '4.2'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '5'
42
+ version: '5.2'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: '3.1'
49
+ version: '4.2'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '5'
52
+ version: '5.2'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: rspec
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
57
  - - "~>"
58
58
  - !ruby/object:Gem::Version
59
- version: '3.0'
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 3.0.0
59
+ version: '3.4'
63
60
  type: :development
64
61
  prerelease: false
65
62
  version_requirements: !ruby/object:Gem::Requirement
66
63
  requirements:
67
64
  - - "~>"
68
65
  - !ruby/object:Gem::Version
69
- version: '3.0'
70
- - - ">="
66
+ version: '3.4'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rubocop
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '='
71
72
  - !ruby/object:Gem::Version
72
- version: 3.0.0
73
- description: |2
74
- ActiveCleaner is a set of helpers that helps you in cleaning user-typed content in your ActiveModel depending models (ActiveRecord, Mongoid...)
73
+ version: 0.52.0
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '='
79
+ - !ruby/object:Gem::Version
80
+ version: 0.52.0
81
+ description: " ActiveCleaner is a set of helpers that helps you in cleaning user-typed
82
+ content in your ActiveModel depending models (ActiveRecord, Mongoid...)\n"
75
83
  email:
76
- - maxime.garcia@free.fr
84
+ - maxime.garcia@gmail.com
77
85
  executables: []
78
86
  extensions: []
79
87
  extra_rdoc_files: []
@@ -110,25 +118,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
118
  requirements:
111
119
  - - ">="
112
120
  - !ruby/object:Gem::Version
113
- version: '1.9'
121
+ version: 2.2.0
114
122
  required_rubygems_version: !ruby/object:Gem::Requirement
115
123
  requirements:
116
124
  - - ">="
117
125
  - !ruby/object:Gem::Version
118
- version: 1.3.6
126
+ version: '0'
119
127
  requirements: []
120
128
  rubyforge_project:
121
- rubygems_version: 2.4.1
129
+ rubygems_version: 2.7.4
122
130
  signing_key:
123
131
  specification_version: 4
124
132
  summary: Clean the fields in your models
125
133
  test_files:
126
- - spec/lib/active_cleaner/utf8mb4_cleaner_spec.rb
127
- - spec/lib/active_cleaner/string_cleaner_spec.rb
128
- - spec/lib/active_cleaner/text_cleaner_spec.rb
129
- - spec/lib/active_cleaner/markdown_cleaner_spec.rb
130
- - spec/lib/active_cleaner_spec.rb
131
134
  - spec/spec_helper.rb
132
- - spec/cases/inherit_spec.rb
133
135
  - spec/cases/simple_spec.rb
134
136
  - spec/cases/nilify_spec.rb
137
+ - spec/cases/inherit_spec.rb
138
+ - spec/lib/active_cleaner_spec.rb
139
+ - spec/lib/active_cleaner/string_cleaner_spec.rb
140
+ - spec/lib/active_cleaner/markdown_cleaner_spec.rb
141
+ - spec/lib/active_cleaner/text_cleaner_spec.rb
142
+ - spec/lib/active_cleaner/utf8mb4_cleaner_spec.rb