normatron 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/lib/normatron/extensions/active_record.rb +3 -1
- data/lib/normatron/filters/ascii_filter.rb +3 -3
- data/lib/normatron/filters/blank_filter.rb +6 -6
- data/lib/normatron/filters/camelize_filter.rb +4 -4
- data/lib/normatron/filters/capitalize_filter.rb +4 -4
- data/lib/normatron/filters/chomp_filter.rb +6 -6
- data/lib/normatron/filters/dasherize_filter.rb +2 -2
- data/lib/normatron/filters/downcase_filter.rb +2 -2
- data/lib/normatron/filters/dump_filter.rb +5 -5
- data/lib/normatron/filters/keep_filter.rb +6 -6
- data/lib/normatron/filters/remove_filter.rb +6 -6
- data/lib/normatron/filters/squeeze_filter.rb +4 -4
- data/lib/normatron/filters/squish_filter.rb +5 -5
- data/lib/normatron/filters/strip_filter.rb +5 -5
- data/lib/normatron/filters/swapcase_filter.rb +2 -2
- data/lib/normatron/filters/titleize_filter.rb +2 -2
- data/lib/normatron/filters/underscore_filter.rb +2 -2
- data/lib/normatron/filters/upcase_filter.rb +2 -2
- data/lib/normatron/version.rb +1 -1
- data/spec/support/matchers/character_cleaner_matcher.rb +1 -1
- data/spec/support/matchers/evaluate_matcher.rb +2 -2
- data/spec/support/my_filters.rb +1 -1
- metadata +1 -1
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
|
214
|
-
* The
|
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.
|
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].
|
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.
|
11
|
-
# AsciiFilter.
|
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.
|
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.
|
9
|
-
# BlankFilter.
|
10
|
-
# BlankFilter.
|
11
|
-
# BlankFilter.
|
12
|
-
# BlankFilter.
|
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.
|
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.
|
22
|
-
# CamelizeFilter.
|
23
|
-
# CamelizeFilter.
|
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.
|
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.
|
11
|
-
# CapitalizeFilter.
|
12
|
-
# CapitalizeFilter.
|
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.
|
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.
|
11
|
-
# ChompFilter.
|
12
|
-
# ChompFilter.
|
13
|
-
# ChompFilter.
|
14
|
-
# ChompFilter.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
10
|
-
# DumpFilter.
|
11
|
-
# DumpFilter.
|
12
|
-
# DumpFilter.
|
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.
|
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.
|
88
|
-
# KeepFilter.
|
89
|
-
# KeepFilter.
|
90
|
-
# KeepFilter.
|
91
|
-
# KeepFilter.
|
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.
|
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.
|
13
|
-
# RemoveFilter.
|
14
|
-
# RemoveFilter.
|
15
|
-
# RemoveFilter.
|
16
|
-
# RemoveFilter.
|
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.
|
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.
|
11
|
-
# SqueezeFilter.
|
12
|
-
# SqueezeFilter.
|
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.
|
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.
|
11
|
-
# SquishFilter.
|
12
|
-
# SquishFilter.
|
13
|
-
# SquishFilter.
|
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.
|
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.
|
11
|
-
# StripFilter.
|
12
|
-
# StripFilter.
|
13
|
-
# StripFilter.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
30
|
+
def self.call(input)
|
31
31
|
input.kind_of?(String) ? mb_send(:upcase, input) : input
|
32
32
|
end
|
33
33
|
end
|
data/lib/normatron/version.rb
CHANGED
@@ -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.
|
26
|
+
@got = @subject.call(@options[:input], @options[:properties])
|
27
27
|
@failure_reason = failure_reason
|
28
28
|
@failure_reason.nil?
|
29
29
|
end
|
data/spec/support/my_filters.rb
CHANGED