normatron 0.3.3 → 0.3.4

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.
data/README.md CHANGED
@@ -210,8 +210,8 @@ To apply the normalizations without doing validations or persistence, just call
210
210
 
211
211
  Create a new module with the following characteristics:
212
212
 
213
- * Having a module method called evaluate
214
- * The evaluate method must receive at least one argument
213
+ * Having a module method called `call`
214
+ * The `call` method must receive at least one argument
215
215
  * The first argument must be the value to be filtered
216
216
 
217
217
  Here is an example:
@@ -220,7 +220,7 @@ Here is an example:
220
220
  # ./lib/my_filters/emoticon_filter.rb
221
221
  module MyFilters
222
222
  module EmoticonFilter
223
- def self.evaluate(value, type)
223
+ def self.call(value, type)
224
224
  emot = (type == :sad) ? ":(" : ":D"
225
225
  value + emot
226
226
  end
@@ -34,6 +34,8 @@ module Normatron
34
34
  new_filters = Normatron.build_hash(options[:with])
35
35
  end
36
36
 
37
+ @normalize_rules ||= {}
38
+
37
39
  # Append new filters to rules
38
40
  @normalize_rules =
39
41
  args.reduce(@normalize_rules) do |hash, att|
@@ -56,7 +58,7 @@ module Normatron
56
58
  if self.respond_to? filter
57
59
  value = send(filter, value, *args)
58
60
  elsif listed_filters[filter].kind_of? Module
59
- value = listed_filters[filter].evaluate(value, *args)
61
+ value = listed_filters[filter].call(value, *args)
60
62
  elsif listed_filters[filter].kind_of? Proc
61
63
  value = listed_filters[filter].call(value, *args)
62
64
  else
@@ -7,8 +7,8 @@ module Normatron
7
7
  # Converts Unicode(and accented ASCII) characters to their plain-text ASCII equivalents.
8
8
  #
9
9
  # @example Out of box
10
- # AsciiFilter.evaluate("EVOLUÇÃO") #=> "EVOLUCAO"
11
- # AsciiFilter.evaluate("⠋⠗⠁⠝⠉⠑") #=> "france"
10
+ # AsciiFilter.call("EVOLUÇÃO") #=> "EVOLUCAO"
11
+ # AsciiFilter.call("⠋⠗⠁⠝⠉⠑") #=> "france"
12
12
  #
13
13
  # @example Using as model normalizer
14
14
  # normalize :attribute_a, :with => :ascii
@@ -24,7 +24,7 @@ module Normatron
24
24
  #
25
25
  # @param input [String] The String to be filtered
26
26
  # @return [String] A new transliterated String
27
- def self.evaluate(input)
27
+ def self.call(input)
28
28
  input.kind_of?(String) ? Stringex::Unidecoder.decode(input) : input
29
29
  end
30
30
  end
@@ -5,11 +5,11 @@ module Normatron
5
5
  # Returns nil for a blank string or the string itself otherwise.
6
6
  #
7
7
  # @example Out of box
8
- # BlankFilter.evaluate("") #=> nil
9
- # BlankFilter.evaluate(" ") #=> nil
10
- # BlankFilter.evaluate(" \n ") #=> nil
11
- # BlankFilter.evaluate("1") #=> "1"
12
- # BlankFilter.evaluate("It's blank?") #=> "It's blank?"
8
+ # BlankFilter.call("") #=> nil
9
+ # BlankFilter.call(" ") #=> nil
10
+ # BlankFilter.call(" \n ") #=> nil
11
+ # BlankFilter.call("1") #=> "1"
12
+ # BlankFilter.call("It's blank?") #=> "It's blank?"
13
13
  #
14
14
  # @example Using as model normalizer
15
15
  # normalize :attribute_a, :with => :blank
@@ -25,7 +25,7 @@ module Normatron
25
25
  #
26
26
  # @param input [String] The String to be filtered
27
27
  # @return [String, nil] The object itself or nil
28
- def self.evaluate(input)
28
+ def self.call(input)
29
29
  input.kind_of?(String) && input.blank? ? nil : input
30
30
  end
31
31
  end
@@ -18,9 +18,9 @@ module Normatron
18
18
  # but it affects UTF-8 characters too.
19
19
  #
20
20
  # @example Out of box
21
- # CamelizeFilter.evaluate("active_record/errors") #=> "ActiveRecord::Errors"
22
- # CamelizeFilter.evaluate("active_record/errors", :upper) #=> "ActiveRecord::Errors"
23
- # CamelizeFilter.evaluate("active_record/errors", :lower) #=> "activeRecord::Errors"
21
+ # CamelizeFilter.call("active_record/errors") #=> "ActiveRecord::Errors"
22
+ # CamelizeFilter.call("active_record/errors", :upper) #=> "ActiveRecord::Errors"
23
+ # CamelizeFilter.call("active_record/errors", :lower) #=> "activeRecord::Errors"
24
24
  #
25
25
  # @example Using as model normalizer
26
26
  # normalize :attribute_a, :with => :camelize
@@ -43,7 +43,7 @@ module Normatron
43
43
  # @param input [String] The String to be filtered
44
44
  # @param camel [Symbol] @:lower@ for lowerCamelCase or @:upper@ for UpperCamelCase
45
45
  # @return [String] A new camelized String
46
- def self.evaluate(input, camel = :upper)
46
+ def self.call(input, camel = :upper)
47
47
  return input unless input.kind_of?(String)
48
48
 
49
49
  string = mb_send(:downcase, input)
@@ -7,9 +7,9 @@ module Normatron
7
7
  # Makes the first character uppercase and all remaining characters lowercase.
8
8
  #
9
9
  # @example Out of box
10
- # CapitalizeFilter.evaluate("KEEP IT SIMPLE") #=> "Keep it simple"
11
- # CapitalizeFilter.evaluate("keep it simple") #=> "Keep it simple"
12
- # CapitalizeFilter.evaluate(" KEEP IT SIMPLE") #=> " keep it simple"
10
+ # CapitalizeFilter.call("KEEP IT SIMPLE") #=> "Keep it simple"
11
+ # CapitalizeFilter.call("keep it simple") #=> "Keep it simple"
12
+ # CapitalizeFilter.call(" KEEP IT SIMPLE") #=> " keep it simple"
13
13
  #
14
14
  # @example Using as model normalizer
15
15
  # normalize :attribute_a, :with => :capitalize
@@ -27,7 +27,7 @@ module Normatron
27
27
  #
28
28
  # @param input [String] The String to be filtered
29
29
  # @return [String] A new capitalized String
30
- def self.evaluate(input)
30
+ def self.call(input)
31
31
  input.kind_of?(String) ? mb_send(:capitalize, input) : input
32
32
  end
33
33
  end
@@ -7,11 +7,11 @@ module Normatron
7
7
  # characters (that is it will remove @\n@, @\r@, and @\r\n@).
8
8
  #
9
9
  # @example Out of box
10
- # ChompFilter.evaluate("Bon Scott\n") #=> "Bon Scott"
11
- # ChompFilter.evaluate("Bon Scott\r") #=> "Bon Scott"
12
- # ChompFilter.evaluate("Bon Scott\r\n") #=> "Bon Scott"
13
- # ChompFilter.evaluate("Bon Scott\n\r") #=> "Bon Scott\n"
14
- # ChompFilter.evaluate("Bon Scott", " Scott") #=> "Bon"
10
+ # ChompFilter.call("Bon Scott\n") #=> "Bon Scott"
11
+ # ChompFilter.call("Bon Scott\r") #=> "Bon Scott"
12
+ # ChompFilter.call("Bon Scott\r\n") #=> "Bon Scott"
13
+ # ChompFilter.call("Bon Scott\n\r") #=> "Bon Scott\n"
14
+ # ChompFilter.call("Bon Scott", " Scott") #=> "Bon"
15
15
  #
16
16
  # @example Using as model normalizer
17
17
  # normalize :attribute_a, :with => :chomp
@@ -32,7 +32,7 @@ module Normatron
32
32
  # @param input [String] The String to be filtered
33
33
  # @param separator [String] The separator used to chomp input
34
34
  # @return [String] A new chopped String
35
- def self.evaluate(input, separator=$/)
35
+ def self.call(input, separator=$/)
36
36
  input.kind_of?(String) ? input.chomp(separator) : input
37
37
  end
38
38
  end
@@ -7,7 +7,7 @@ module Normatron
7
7
  # Replaces all underscores with dashes.
8
8
  #
9
9
  # @example Out of box
10
- # DasherizeFilter.evaluate("monty_python") #=> "monty-python"
10
+ # DasherizeFilter.call("monty_python") #=> "monty-python"
11
11
  #
12
12
  # @example Using as model normalizer
13
13
  # normalize :attribute_a, :with => :dasherize
@@ -23,7 +23,7 @@ module Normatron
23
23
  #
24
24
  # @param input [String] The String to be filtered
25
25
  # @return [String] A new dasherized String
26
- def self.evaluate(input)
26
+ def self.call(input)
27
27
  input.kind_of?(String) ? input.dasherize : input
28
28
  end
29
29
  end
@@ -7,7 +7,7 @@ module Normatron
7
7
  # Lowercase all characters.
8
8
  #
9
9
  # @example Out of box
10
- # DowncaseFilter.evaluate("NOTHING ELSE MATTERS") #=> "nothing else matters"
10
+ # DowncaseFilter.call("NOTHING ELSE MATTERS") #=> "nothing else matters"
11
11
  #
12
12
  # @example Using as ActiveRecord::Base normalizer
13
13
  # normalize :attribute_a, :with => :downcase
@@ -27,7 +27,7 @@ module Normatron
27
27
  #
28
28
  # @param input [String] The String to be filtered
29
29
  # @return [String] A new lowercased String
30
- def self.evaluate(input)
30
+ def self.call(input)
31
31
  input.kind_of?(String) ? mb_send(:downcase, input) : input
32
32
  end
33
33
  end
@@ -6,10 +6,10 @@ module Normatron
6
6
  # special characters escaped.
7
7
  #
8
8
  # @example Out of box
9
- # DumpFilter.evaluate("I'm not\na \"clubber\"...") #=> "\"I'm not\\na \\\"clubber\\\"...\""
10
- # DumpFilter.evaluate("I'm not\na \"clubber\"...") #== '"I\'m not\na \"clubber\"..."'
11
- # DumpFilter.evaluate('I\'m not\na "clubber"...') #=> "\"I'm not\\\\na \\\"clubber\\\"...\""
12
- # DumpFilter.evaluate('I\'m not\na "clubber"...') #== '"I\'m not\\\na \"clubber\"..."'
9
+ # DumpFilter.call("I'm not\na \"clubber\"...") #=> "\"I'm not\\na \\\"clubber\\\"...\""
10
+ # DumpFilter.call("I'm not\na \"clubber\"...") #== '"I\'m not\na \"clubber\"..."'
11
+ # DumpFilter.call('I\'m not\na "clubber"...') #=> "\"I'm not\\\\na \\\"clubber\\\"...\""
12
+ # DumpFilter.call('I\'m not\na "clubber"...') #== '"I\'m not\\\na \"clubber\"..."'
13
13
  #
14
14
  # @example Using as ActiveRecord::Base normalizer
15
15
  # normalize :attribute_a, :with => :dump
@@ -25,7 +25,7 @@ module Normatron
25
25
  #
26
26
  # @param input [String] The String to be filtered
27
27
  # @return [String] A new dumpped String
28
- def self.evaluate(input)
28
+ def self.call(input)
29
29
  input.kind_of?(String) ? input.dump : input
30
30
  end
31
31
  end
@@ -84,11 +84,11 @@ module Normatron
84
84
  # Thai, Tibetan, Tifinagh, Ugaritic, Vai, and Yi.
85
85
  #
86
86
  # @example Out of box
87
- # KeepFilter.evaluate("Doom 3", :L) #=> "Doom" keep only letters
88
- # KeepFilter.evaluate("Doom 3", :N) #=> "3" keep only numbers
89
- # KeepFilter.evaluate("Doom 3", :L, :N) #=> "Doom3" keep only letters and numbers
90
- # KeepFilter.evaluate("Doom 3", :Lu, :N) #=> "D3" keep only uppercased letters or numbers
91
- # KeepFilter.evaluate("Doom ˩", :Latin) #=> "Doom" keep only latin characters
87
+ # KeepFilter.call("Doom 3", :L) #=> "Doom" keep only letters
88
+ # KeepFilter.call("Doom 3", :N) #=> "3" keep only numbers
89
+ # KeepFilter.call("Doom 3", :L, :N) #=> "Doom3" keep only letters and numbers
90
+ # KeepFilter.call("Doom 3", :Lu, :N) #=> "D3" keep only uppercased letters or numbers
91
+ # KeepFilter.call("Doom ˩", :Latin) #=> "Doom" keep only latin characters
92
92
  #
93
93
  # @example Using as ActiveRecord::Base normalizer
94
94
  # normalize :attribute_a, :with => [[:keep, :Lu]]
@@ -109,7 +109,7 @@ module Normatron
109
109
  # @param input [String] The String to be filtered
110
110
  # @param properties [[Symbol]*] Symbols equivalent to Regexp property for @\\p{}@ construct
111
111
  # @return [String] A new clean String
112
- def self.evaluate(input, *properties)
112
+ def self.call(input, *properties)
113
113
  input.kind_of?(String) ? evaluate_regexp(input, :keep, properties) : input
114
114
  end
115
115
  end
@@ -9,11 +9,11 @@ module Normatron
9
9
  # For additional informations see Normatron::Filter::ClassMethods#keep documentation.
10
10
  #
11
11
  # @example Out of box
12
- # RemoveFilter.evaluate("Quake 3", :L) #=> " 3" remove only letters
13
- # RemoveFilter.evaluate("Quake 3", :N) #=> "Quake " remove only numbers
14
- # RemoveFilter.evaluate("Quake 3", :L, :N) #=> " " remove only letters or numbers
15
- # RemoveFilter.evaluate("Quake 3", :Lu, :N) #=> "uake " remove only uppercased letters or numbers
16
- # RemoveFilter.evaluate("Quake ˩", :Latin) #=> " ˩" remove only latin characters
12
+ # RemoveFilter.call("Quake 3", :L) #=> " 3" remove only letters
13
+ # RemoveFilter.call("Quake 3", :N) #=> "Quake " remove only numbers
14
+ # RemoveFilter.call("Quake 3", :L, :N) #=> " " remove only letters or numbers
15
+ # RemoveFilter.call("Quake 3", :Lu, :N) #=> "uake " remove only uppercased letters or numbers
16
+ # RemoveFilter.call("Quake ˩", :Latin) #=> " ˩" remove only latin characters
17
17
  #
18
18
  # @example Using as ActiveRecord::Base normalizer
19
19
  # normalize :attribute_a, :with => [[:remove, :Lu]]
@@ -34,7 +34,7 @@ module Normatron
34
34
  # @param input [String] The String to be filtered
35
35
  # @param properties [[Symbol]*] Symbols equivalent to Regexp property for @\\p{}@ construct
36
36
  # @return [String] A new clean String
37
- def self.evaluate(input, *properties)
37
+ def self.call(input, *properties)
38
38
  input.kind_of?(String) ? evaluate_regexp(input, :remove, properties) : input
39
39
  end
40
40
  end
@@ -7,9 +7,9 @@ module Normatron
7
7
  # If no option are given, all runs of identical characters are replaced by a single character.
8
8
  #
9
9
  # @example Out of box
10
- # SqueezeFilter.evaluate("yellow moon") #=> "yelow mon"
11
- # SqueezeFilter.evaluate(" now is the", " ") #=> " now is the"
12
- # SqueezeFilter.evaluate("putters shoot balls", "m-z") #=> "puters shot balls"
10
+ # SqueezeFilter.call("yellow moon") #=> "yelow mon"
11
+ # SqueezeFilter.call(" now is the", " ") #=> " now is the"
12
+ # SqueezeFilter.call("putters shoot balls", "m-z") #=> "puters shot balls"
13
13
  #
14
14
  # @example Using as ActiveRecord::Base normalizer
15
15
  # normalize :attribute_a, :with => [:custom_filter, :squeeze]
@@ -29,7 +29,7 @@ module Normatron
29
29
  # @param input [String] The String to be filtered
30
30
  # @param targets [[String]*] Characters to be affected
31
31
  # @return [String] A new squeezed String
32
- def self.evaluate(input, *targets)
32
+ def self.call(input, *targets)
33
33
  return input unless input.kind_of?(String)
34
34
  targets.any? ? input.squeeze(targets.last) : input.squeeze
35
35
  end
@@ -7,10 +7,10 @@ module Normatron
7
7
  # Strip input, remove line-breaks and multiple spaces.
8
8
  #
9
9
  # @example Out of box
10
- # SquishFilter.evaluate(" the simpsons ") #=> "the simpsons"
11
- # SquishFilter.evaluate("family guy") #=> "family guy"
12
- # SquishFilter.evaluate("the \n simpsons") #=> "the simpsons"
13
- # SquishFilter.evaluate("the\nsimpsons") #=> "the simpsons"
10
+ # SquishFilter.call(" the simpsons ") #=> "the simpsons"
11
+ # SquishFilter.call("family guy") #=> "family guy"
12
+ # SquishFilter.call("the \n simpsons") #=> "the simpsons"
13
+ # SquishFilter.call("the\nsimpsons") #=> "the simpsons"
14
14
  #
15
15
  # @example Using as ActiveRecord::Base normalizer
16
16
  # normalize :attribute, :with => [:custom_filter, :squish]
@@ -27,7 +27,7 @@ module Normatron
27
27
  #
28
28
  # @param input [String] The String to be filtered
29
29
  # @return [String] A new squished String
30
- def self.evaluate(input)
30
+ def self.call(input)
31
31
  input.kind_of?(String) ? input.squish : input
32
32
  end
33
33
  end
@@ -7,10 +7,10 @@ module Normatron
7
7
  # Removes traling and leading spaces.
8
8
  #
9
9
  # @example Out of box
10
- # StripFilter.evaluate(" copy ") #=> "copy"
11
- # StripFilter.evaluate(" copy ", :L) #=> "copy "
12
- # StripFilter.evaluate(" copy ", :R) #=> " copy"
13
- # StripFilter.evaluate(" copy ", :LR) #=> "copy"
10
+ # StripFilter.call(" copy ") #=> "copy"
11
+ # StripFilter.call(" copy ", :L) #=> "copy "
12
+ # StripFilter.call(" copy ", :R) #=> " copy"
13
+ # StripFilter.call(" copy ", :LR) #=> "copy"
14
14
  #
15
15
  # @example Using as ActiveRecord::Base normalizer
16
16
  # normalize :attribute_a, :with => :strip
@@ -33,7 +33,7 @@ module Normatron
33
33
  # @param input [String] The String to be filtered
34
34
  # @param edges [Symbol] @:L@ to strip trailing spaces, @:R@ for leading spaces or @:LR@ for both
35
35
  # @return [String] A new stripped String
36
- def self.evaluate(input, edges=:LR)
36
+ def self.call(input, edges=:LR)
37
37
  return input unless input.kind_of?(String)
38
38
 
39
39
  regex_string =
@@ -7,7 +7,7 @@ module Normatron
7
7
  # Replaces uppercased characters by lowercased and vice versa.
8
8
  #
9
9
  # @example Out of box
10
- # SwapcaseFilter.evaluate("As you Wish!") #=> "aS YOU wISH!"
10
+ # SwapcaseFilter.call("As you Wish!") #=> "aS YOU wISH!"
11
11
  #
12
12
  # @example Using as ActiveRecord::Base normalizer
13
13
  # normalize :attribute_a, :with => :swapcase
@@ -27,7 +27,7 @@ module Normatron
27
27
  #
28
28
  # @param input [String] The String to be filtered
29
29
  # @return [String] A new swapcased String
30
- def self.evaluate(input)
30
+ def self.call(input)
31
31
  return input unless input.kind_of?(String)
32
32
  input.gsub(/([\p{Ll}])|(\p{Lu})|([^\p{Ll}\p{Lu}])/u) { $3 || ($2 ? mb_send(:downcase, $2) : mb_send(:upcase, $1)) }
33
33
  end
@@ -7,7 +7,7 @@ module Normatron
7
7
  # Capitalizes the first character of each word.
8
8
  #
9
9
  # @example Out of box
10
- # TitleizeFilter.evaluate("at your will!") #=> "At Your Will!"
10
+ # TitleizeFilter.call("at your will!") #=> "At Your Will!"
11
11
  #
12
12
  # @example Using as ActiveRecord::Base normalizer
13
13
  # normalize :attribute_a, :with => :titleize
@@ -27,7 +27,7 @@ module Normatron
27
27
  #
28
28
  # @param input [String] The String to be filtered
29
29
  # @return [String] A new titleized String
30
- def self.evaluate(input)
30
+ def self.call(input)
31
31
  input.kind_of?(String) ? mb_send(:titleize, input) : input
32
32
  end
33
33
  end
@@ -14,7 +14,7 @@ module Normatron
14
14
  # "SSLError".underscore.camelize # => "SslError"
15
15
  #
16
16
  # @example Out of box
17
- # UnderscoreFilter.evaluate("ActiveRecord::Errors") #=> "active_record/errors"
17
+ # UnderscoreFilter.call("ActiveRecord::Errors") #=> "active_record/errors"
18
18
  #
19
19
  # @example Using as ActiveRecord::Base normalizer
20
20
  # normalize :attribute_a, :with => :underscore
@@ -36,7 +36,7 @@ module Normatron
36
36
  #
37
37
  # @param input [String] The String to be filtered
38
38
  # @return [String] A new underscored String
39
- def self.evaluate(input)
39
+ def self.call(input)
40
40
  return input unless input.kind_of?(String)
41
41
 
42
42
  string = input.gsub(/::/, '/')
@@ -7,7 +7,7 @@ module Normatron
7
7
  # Uppercase all characters.
8
8
  #
9
9
  # @example Out of box
10
- # UpcaseFilter.evaluate("borderlands") #=> "BORDERLANDS"
10
+ # UpcaseFilter.call("borderlands") #=> "BORDERLANDS"
11
11
  #
12
12
  # @example Using as ActiveRecord::Base normalizer
13
13
  # normalize :attribute_a, :with => :upcase
@@ -27,7 +27,7 @@ module Normatron
27
27
  #
28
28
  # @param input [String] The String to be filtered
29
29
  # @return [String] A new uppercased String
30
- def self.evaluate(input)
30
+ def self.call(input)
31
31
  input.kind_of?(String) ? mb_send(:upcase, input) : input
32
32
  end
33
33
  end
@@ -1,3 +1,3 @@
1
1
  module Normatron
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -23,7 +23,7 @@ module FilterMatchers
23
23
  def matches?(subject)
24
24
  @subject = subject
25
25
  @expected = @options[:input].gsub(regexp, '')
26
- @got = @subject.evaluate(@options[:input], @options[:properties])
26
+ @got = @subject.call(@options[:input], @options[:properties])
27
27
  @failure_reason = failure_reason
28
28
  @failure_reason.nil?
29
29
  end
@@ -62,9 +62,9 @@ module FilterMatchers
62
62
 
63
63
  def get_evaluation
64
64
  if @args.nil?
65
- @filter.evaluate(@input)
65
+ @filter.call(@input)
66
66
  else
67
- @filter.evaluate(@input, *@args)
67
+ @filter.call(@input, *@args)
68
68
  end
69
69
  end
70
70
 
@@ -1,6 +1,6 @@
1
1
  module MyFilters
2
2
  module SmileFilter
3
- def self.evaluate(value)
3
+ def self.call(value)
4
4
  value + " =]"
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: normatron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: