polite_text 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
  SHA256:
3
- metadata.gz: b61709fd69469a4a019e0a041b18c95700d72132992293b2e2f49eebe9df6662
4
- data.tar.gz: d243724fa7ef4eb931e4b6e295b6b1d8afec2a81c82d399e5759f764f808c1e4
3
+ metadata.gz: 6441b50cc047fa72c872b0b2f4e1bcaff686461d28738426251f3562e7507494
4
+ data.tar.gz: 5cec91bda68345fdc0af048107edaf971b32848f823e9f2e4f88f46524d608f5
5
5
  SHA512:
6
- metadata.gz: 1a4fbc965f3c668bedbe5e0bf1df64f72077199c743938ec009c82a71e6c889f11492c92d18c58776303e31d34acbacdd01ea6dc47666731a41ade9abb044255
7
- data.tar.gz: c232a79a3dd3c5bd8ea7abfbfbe99f807de817143d54a57db31e1feb85a88ec5e6c75c3f040a3c05f14f89401b265b00f1e3c1b47a7daaad0dbf9e1a72c64613
6
+ metadata.gz: e6d59fba6f064483e5ffe3494d02694790d1d15cc2710463c49a64f25f487886518e68e33f6f23e6ed4ebc9bf87047f23a9fee49f8fb7b06802aea202d0ae134
7
+ data.tar.gz: d76c2d3b3eb20b90b03ff2f05fa5eb277e5d7b36100aa8ae5cae1650a55582da9a2146f6b06b14b208b397b3f752cf92abb9ae154c2199fc8b2dbdc2bf20b4a4
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- polite_text (0.1.22)
4
+ polite_text (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -79,7 +79,7 @@ end
79
79
  ### Custom swear words list 🔞
80
80
  Create a custom list in a **YAML** file following this format :
81
81
  ```
82
- # my_custom_swear_words.yml
82
+ # custom_swear_words.yml
83
83
 
84
84
  swear_words:
85
85
  - gem
@@ -89,25 +89,29 @@ swear_words:
89
89
 
90
90
  Place it where you want in your app, we recommend here : `./lib/polite_text/my_custom_swear_words.yml`
91
91
 
92
- 👉 You just need to pass the path to this file as a second argument.
92
+ ## Confirguration
93
93
 
94
- ### Example for an Article model
94
+ 📄 Create an initializer named `polite_text.rb`
95
+
96
+ ✌️ Add the path to your custom swear words list like this :
95
97
 
96
- ### Remove or detect swear words 🤬 🙅‍♂️ 🙅‍♀️
97
98
  ```
98
- # Inside a random_model.rb
99
+ # ./config/initializers/polite_text.rb
99
100
 
100
- include PoliteText
101
+ PoliteText.configure do |config|
102
+ config.custom_swear_words_path = "#{__dir__}/../../lib/polite_text/custom_swear_words.yml"
103
+ end
104
+ ```
101
105
 
106
+ PoliteText is now configured with your custom list ✨💫
107
+ ```
102
108
  str = "This gem is a fucking big shit but let's try it"
103
109
 
104
- current_dir = __dir__
105
- custom_path = "#{current_dir}/../lib/polite_text/fr.yml"
106
-
107
- PoliteText.be_polite!(str, custom_path)
110
+ <<<<<<< HEAD
111
+ PoliteText.be_polite!(str)
108
112
  => "This *** is a fucking *** shit *** let's try it"
109
113
 
110
- PoliteText.is_polite?(str, custom_path)
114
+ PoliteText.is_polite?(str)
111
115
  => false
112
116
  ```
113
117
 
@@ -4,12 +4,27 @@ require 'polite_text/text_cleaner'
4
4
 
5
5
  module PoliteText
6
6
  class << self
7
- def be_polite!(text, custom_swear_words_path = nil)
8
- PoliteText::TextCleaner.new(text, custom_swear_words_path).clean!
7
+ attr_accessor :custom_swear_words_path
8
+
9
+ def configure
10
+ yield self if block_given?
11
+ check_attrs
12
+ end
13
+
14
+ def be_polite!(text)
15
+ PoliteText::TextCleaner.new(text).clean!
9
16
  end
10
17
 
11
- def is_polite?(text, custom_swear_words_path = nil)
12
- PoliteText::TextScanner.new(text, custom_swear_words_path).match_swear_word?
18
+ def is_polite?(text)
19
+ PoliteText::TextScanner.new(text).match_swear_word?
20
+ end
21
+
22
+ private
23
+
24
+ def check_attrs
25
+ unless custom_swear_words_path.is_a?(::String)
26
+ raise(ArgumentError, 'Invalid path to your custom swear words list.')
27
+ end
13
28
  end
14
29
  end
15
30
  end
@@ -2,17 +2,21 @@ require 'yaml'
2
2
 
3
3
  module PoliteText
4
4
  class Base
5
- def swear_words(custom_swear_words_path)
6
- @swear_words ||= /\b(#{Regexp.union(swear_words_list(custom_swear_words_path)).source})\b/
5
+ def swear_words
6
+ @swear_words ||= /\b(#{Regexp.union(swear_words_list).source})\b/
7
7
  end
8
8
 
9
- def swear_words_list(custom_swear_words_path = nil)
9
+ def swear_words_list()
10
10
  path = custom_swear_words_path ? custom_swear_words_path.to_s : swear_words_load_path
11
11
  YAML.load_file(path)['swear_words']
12
12
  end
13
13
 
14
14
  private
15
15
 
16
+ def custom_swear_words_path
17
+ @custom_swear_words_path ||= PoliteText.custom_swear_words_path
18
+ end
19
+
16
20
  # TODO: Implement dynamic locale with I18N
17
21
  def locale
18
22
  @locale ||= 'en'
@@ -2,15 +2,14 @@ module PoliteText
2
2
  class TextCleaner < Base
3
3
  attr_reader :text
4
4
 
5
- def initialize(text, custom_swear_words_path = nil)
5
+ def initialize(text)
6
6
  raise ArgumentError.new('The text can not be nil') if text.nil?
7
7
 
8
8
  @text = text.to_s
9
- @custom_swear_words_path = custom_swear_words_path
10
9
  end
11
10
 
12
11
  def clean!
13
- text.gsub!(swear_words(@custom_swear_words_path), '***') || text
12
+ text.gsub!(swear_words, '***') || text
14
13
  end
15
14
  end
16
15
  end
@@ -2,15 +2,14 @@ module PoliteText
2
2
  class TextScanner < Base
3
3
  attr_reader :text
4
4
 
5
- def initialize(text, custom_swear_words_path = nil)
5
+ def initialize(text)
6
6
  raise ArgumentError.new('The text can not be nil') if text.nil?
7
7
 
8
8
  @text = text.to_s
9
- @custom_swear_words_path = custom_swear_words_path
10
9
  end
11
10
 
12
11
  def match_swear_word?
13
- !@text.match?(swear_words(@custom_swear_words_path))
12
+ !@text.match?(swear_words)
14
13
  end
15
14
  end
16
15
  end
@@ -1,3 +1,3 @@
1
1
  module PoliteText
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polite_text
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
  - DumasOlivier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-05 00:00:00.000000000 Z
11
+ date: 2020-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec