Class: Swearjar
- Inherits:
-
Object
- Object
- Swearjar
- Defined in:
- lib/swearjar.rb,
lib/swearjar/tester.rb,
lib/swearjar/version.rb
Defined Under Namespace
Classes: Tester
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#tester ⇒ Object
readonly
Returns the value of attribute tester.
Class Method Summary collapse
Instance Method Summary collapse
- #censor(string) ⇒ Object
-
#initialize(file = nil) ⇒ Swearjar
constructor
A new instance of Swearjar.
- #load_file(file) ⇒ Object
- #profane?(string) ⇒ Boolean
- #scan(string, &block) ⇒ Object
- #scorecard(string) ⇒ Object
Constructor Details
#initialize(file = nil) ⇒ Swearjar
Returns a new instance of Swearjar.
16 17 18 19 20 |
# File 'lib/swearjar.rb', line 16 def initialize(file = nil) @tester = FuzzyHash.new @hash = {} load_file(file) if file end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
14 15 16 |
# File 'lib/swearjar.rb', line 14 def hash @hash end |
#tester ⇒ Object (readonly)
Returns the value of attribute tester.
14 15 16 |
# File 'lib/swearjar.rb', line 14 def tester @tester end |
Class Method Details
.default ⇒ Object
6 7 8 |
# File 'lib/swearjar.rb', line 6 def self.default from_language end |
.from_language(language = 'en') ⇒ Object
10 11 12 |
# File 'lib/swearjar.rb', line 10 def self.from_language(language = 'en') new(File.join(File.dirname(__FILE__), 'config', "#{language}.yml")) end |
Instance Method Details
#censor(string) ⇒ Object
57 58 59 60 61 |
# File 'lib/swearjar.rb', line 57 def censor(string) censored_string = string.to_s.dup scan(string) {|word, test| censored_string.gsub!(word, block_given? ? yield(word) : word.gsub(/\S/, '*')) if test} censored_string end |
#load_file(file) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/swearjar.rb', line 22 def load_file(file) data = YAML.load_file(file) data['regex'].each do |pattern, type| @tester[Regexp.new(pattern)] = type end if data['regex'] data['simple'].each do |test, type| @hash[test] = type end if data['simple'] end |
#profane?(string) ⇒ Boolean
44 45 46 47 48 |
# File 'lib/swearjar.rb', line 44 def profane?(string) string = string.to_s scan(string) {|word, test| return true if !test.nil?} return false end |
#scan(string, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/swearjar.rb', line 34 def scan(string, &block) string = string.to_s string.scan(/\b[a-zA-Z-]+\b/) do |word| block.call(word, hash[word.downcase] || hash[word.downcase.gsub(/e?s$/,'')] ) end if match = tester.match_with_result(string) block.call(match.last, match.first) end end |
#scorecard(string) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/swearjar.rb', line 50 def scorecard(string) string = string.to_s scorecard = {} scan(string) {|word, test| test.each { |type| scorecard.key?(type) ? scorecard[type] += 1 : scorecard[type] = 1} if test} scorecard end |