blacklist_validator 0.0.2 → 0.0.3

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.
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "blacklist_validator"
6
- s.version = "0.0.2"
6
+ s.version = "0.0.3"
7
7
  s.authors = ["Axel Vergult"]
8
8
  s.email = ["axel.vergult@gmail.com"]
9
9
  s.homepage = "https://github.com/episko/blacklist_validator"
@@ -1,17 +1,16 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  class BlacklistValidator < ActiveModel::EachValidator
4
- attr_accessor :blacklist
5
-
6
- def initialize(options)
7
- load_blacklist!
8
- super(options)
9
- end
10
4
 
11
5
  def validate_each(record, attribute, value)
12
6
  record.errors.add(attribute, options[:message] || invalid_message(record, attribute)) if blacklisted?(value)
13
7
  end
14
8
 
9
+ # Lazy load and transform directly words to patterns instead of creating regexps at each matching tests
10
+ def blacklist
11
+ @blacklist ||= YAML.load_file(blacklist_file).map { |word| /(#{word})+/i }
12
+ end
13
+
15
14
  #######################
16
15
  ### Private methods ###
17
16
  #######################
@@ -25,20 +24,14 @@ class BlacklistValidator < ActiveModel::EachValidator
25
24
  end
26
25
 
27
26
  def blacklisted?(str)
28
- @blacklist.each { |word| return true if greedy_match?(str, word) }
29
- false
30
- end
31
-
32
- def greedy_match?(str, word)
33
- str =~ /(#{word})+/i
27
+ blacklist.any? { |pattern| str =~ pattern }
34
28
  end
35
29
 
36
- def load_blacklist!
30
+ def blacklist_file
37
31
  if defined?(Rails.root) && (blacklist_file_path = Rails.root.join("config", "blacklist.yml")).exist?
38
- blacklist_path = blacklist_file_path
32
+ return blacklist_file_path
39
33
  end
40
- blacklist_path ||= File.join(File.dirname(__FILE__), "../config/blacklist.yml")
41
- @blacklist = YAML.load_file(blacklist_path)
34
+ File.join(File.dirname(__FILE__), "../config/blacklist.yml")
42
35
  end
43
36
 
44
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blacklist_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &70349443486320 !ruby/object:Gem::Requirement
16
+ requirement: &70295538182100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70349443486320
24
+ version_requirements: *70295538182100
25
25
  description: Blacklist validator for Rails 3
26
26
  email:
27
27
  - axel.vergult@gmail.com