normatron 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +2 -0
- data/lib/normatron/filters.rb +42 -24
- data/lib/normatron/filters/ascii_filter.rb +18 -12
- data/lib/normatron/filters/blank_filter.rb +19 -15
- data/lib/normatron/filters/camelize_filter.rb +39 -34
- data/lib/normatron/filters/capitalize_filter.rb +19 -13
- data/lib/normatron/filters/chomp_filter.rb +27 -21
- data/lib/normatron/filters/dasherize_filter.rb +16 -10
- data/lib/normatron/filters/downcase_filter.rb +19 -13
- data/lib/normatron/filters/dump_filter.rb +20 -14
- data/lib/normatron/filters/keep_filter.rb +101 -84
- data/lib/normatron/filters/remove_filter.rb +26 -21
- data/lib/normatron/filters/squeeze_filter.rb +24 -16
- data/lib/normatron/filters/squish_filter.rb +20 -13
- data/lib/normatron/filters/strip_filter.rb +25 -17
- data/lib/normatron/filters/swapcase_filter.rb +19 -13
- data/lib/normatron/filters/titleize_filter.rb +19 -13
- data/lib/normatron/filters/underscore_filter.rb +28 -22
- data/lib/normatron/filters/upcase_filter.rb +19 -13
- data/lib/normatron/version.rb +1 -1
- data/spec/normatron/configuration_spec.rb +0 -1
- data/spec/normatron/extensions/active_record_spec.rb +1 -0
- data/spec/normatron/filters/ascii_filter_spec.rb +15 -9
- data/spec/normatron/filters/blank_filter_spec.rb +10 -11
- data/spec/normatron/filters/camelize_filter_spec.rb +94 -33
- data/spec/normatron/filters/capitalize_filter_spec.rb +8 -7
- data/spec/normatron/filters/squeeze_filter_spec.rb +2 -2
- data/spec/spec_helper.rb +6 -1
- data/spec/support/matchers/evaluate_matcher.rb +75 -0
- metadata +5 -2
@@ -2,96 +2,113 @@ require 'normatron/filters/helpers'
|
|
2
2
|
|
3
3
|
module Normatron
|
4
4
|
module Filters
|
5
|
+
|
6
|
+
##
|
7
|
+
# Remove the characters that doesn't match the given properties.
|
8
|
+
#
|
9
|
+
# The character properties follow the rule of @\p{}@ construct described in Regexp class.
|
10
|
+
# The @\p{}@ construct matches characters with the named property, much like POSIX bracket classes.
|
11
|
+
#
|
12
|
+
# To pass named properties to this filter, use them as Symbols:
|
13
|
+
#
|
14
|
+
# |_<.Property |_<.Description |
|
15
|
+
# | @:Alnum@ | Alphabetic and numeric character |
|
16
|
+
# | @:Alpha@ | Alphabetic character |
|
17
|
+
# | @:Blank@ | Space or tab |
|
18
|
+
# | @:Cntrl@ | Control character |
|
19
|
+
# | @:Digit@ | Digit |
|
20
|
+
# | @:Graph@ | Non-blank character (excludes spaces, control characters, and similar) |
|
21
|
+
# | @:Lower@ | Lowercase alphabetical character |
|
22
|
+
# | @:Print@ | Like :Graph, but includes the space character |
|
23
|
+
# | @:Punct@ | Punctuation character |
|
24
|
+
# | @:Space@ | Whitespace character (@[:blank:]@, newline, carriage return, etc.) |
|
25
|
+
# | @:Upper@ | Uppercase alphabetical |
|
26
|
+
# | @:XDigit@ | Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F) |
|
27
|
+
# | @:Word@ | A member of one of the following Unicode general category Letter, Mark, Number, Connector_Punctuation |
|
28
|
+
# | @:ASCII@ | A character in the ASCII character set |
|
29
|
+
# | @:Any@ | Any Unicode character (including unassigned characters) |
|
30
|
+
# | @:Assigned@ | An assigned character |
|
31
|
+
#
|
32
|
+
# A Unicode character's General Category value can also be matched with @:Ab@ where @Ab@ is the category's
|
33
|
+
# abbreviation as described below:
|
34
|
+
#
|
35
|
+
# |_<.Property |_<.Description |
|
36
|
+
# | @:L@ | Letter |
|
37
|
+
# | @:Ll@ | Letter: Lowercase |
|
38
|
+
# | @:Lm@ | Letter: Mark |
|
39
|
+
# | @:Lo@ | Letter: Other |
|
40
|
+
# | @:Lt@ | Letter: Titlecase |
|
41
|
+
# | @:Lu@ | Letter: Uppercas |
|
42
|
+
# | @:Lo@ | Letter: Other |
|
43
|
+
# | @:M@ | Mark |
|
44
|
+
# | @:Mn@ | Mark: Nonspacing |
|
45
|
+
# | @:Mc@ | Mark: Spacing Combining |
|
46
|
+
# | @:Me@ | Mark: Enclosing |
|
47
|
+
# | @:N@ | Number |
|
48
|
+
# | @:Nd@ | Number: Decimal Digit |
|
49
|
+
# | @:Nl@ | Number: Letter |
|
50
|
+
# | @:No@ | Number: Other |
|
51
|
+
# | @:P@ | Punctuation |
|
52
|
+
# | @:Pc@ | Punctuation: Connector |
|
53
|
+
# | @:Pd@ | Punctuation: Dash |
|
54
|
+
# | @:Ps@ | Punctuation: Open |
|
55
|
+
# | @:Pe@ | Punctuation: Close |
|
56
|
+
# | @:Pi@ | Punctuation: Initial Quote |
|
57
|
+
# | @:Pf@ | Punctuation: Final Quote |
|
58
|
+
# | @:Po@ | Punctuation: Other |
|
59
|
+
# | @:S@ | Symbol |
|
60
|
+
# | @:Sm@ | Symbol: Math |
|
61
|
+
# | @:Sc@ | Symbol: Currency |
|
62
|
+
# | @:Sc@ | Symbol: Currency |
|
63
|
+
# | @:Sk@ | Symbol: Modifier |
|
64
|
+
# | @:So@ | Symbol: Other |
|
65
|
+
# | @:Z@ | Separator |
|
66
|
+
# | @:Zs@ | Separator: Space |
|
67
|
+
# | @:Zl@ | Separator: Line |
|
68
|
+
# | @:Zp@ | Separator: Paragraph |
|
69
|
+
# | @:C@ | Other |
|
70
|
+
# | @:Cc@ | Other: Control |
|
71
|
+
# | @:Cf@ | Other: Format |
|
72
|
+
# | @:Cn@ | Other: Not Assigned |
|
73
|
+
# | @:Co@ | Other: Private Use |
|
74
|
+
# | @:Cs@ | Other: Surrogate |
|
75
|
+
#
|
76
|
+
# Lastly, this method matches a character's Unicode script. The following scripts are supported:
|
77
|
+
#
|
78
|
+
# Arabic, Armenian, Balinese, Bengali, Bopomofo, Braille, Buginese, Buhid, Canadian_Aboriginal, Carian, Cham,
|
79
|
+
# Cherokee, Common, Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, Devanagari, Ethiopic, Georgian, Glagolitic,
|
80
|
+
# Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hiragana, Inherited, Kannada, Katakana, Kayah_Li,
|
81
|
+
# Kharoshthi, Khmer, Lao, Latin, Lepcha, Limbu, Linear_B, Lycian, Lydian, Malayalam, Mongolian, Myanmar,
|
82
|
+
# New_Tai_Lue, Nko, Ogham, Ol_Chiki, Old_Italic, Old_Persian, Oriya, Osmanya, Phags_Pa, Phoenician, Rejang, Runic,
|
83
|
+
# Saurashtra, Shavian, Sinhala, Sundanese, Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, Tamil, Telugu, Thaana,
|
84
|
+
# Thai, Tibetan, Tifinagh, Ugaritic, Vai, and Yi.
|
85
|
+
#
|
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
|
92
|
+
#
|
93
|
+
# @example Using as ActiveRecord::Base normalizer
|
94
|
+
# normalize :attribute_a, :with => [[:keep, :Lu]]
|
95
|
+
# normalize :attribute_b, :with => [{:keep =>[:Lu]}]
|
96
|
+
# normalize :attribute_c, :with => [:custom_filter, [:keep, :Ll, :Space]]
|
97
|
+
# normalize :attribute_d, :with => [:custom_filter, {:keep => [:Ll, :Space]}]
|
98
|
+
#
|
99
|
+
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
100
|
+
# @see RemoveFilter Normatron::Filters::RemoveFilter
|
5
101
|
module KeepFilter
|
6
102
|
extend Helpers
|
7
103
|
|
8
104
|
##
|
9
|
-
#
|
10
|
-
# The character properties follow the rule of \\p{} construct described in Regexp class.
|
11
|
-
# The \\p{} construct matches characters with the named property, much like POSIX bracket classes.
|
105
|
+
# Performs input conversion according to filter requirements.
|
12
106
|
#
|
13
|
-
#
|
14
|
-
# * <tt>:Alnum</tt> - Alphabetic and numeric character
|
15
|
-
# * <tt>:Alpha</tt> - Alphabetic character
|
16
|
-
# * <tt>:Blank</tt> - Space or tab
|
17
|
-
# * <tt>:Cntrl</tt> - Control character
|
18
|
-
# * <tt>:Digit</tt> - Digit
|
19
|
-
# * <tt>:Graph</tt> - Non-blank character (excludes spaces, control characters, and similar)
|
20
|
-
# * <tt>:Lower</tt> - Lowercase alphabetical character
|
21
|
-
# * <tt>:Print</tt> - Like :Graph, but includes the space character
|
22
|
-
# * <tt>:Punct</tt> - Punctuation character
|
23
|
-
# * <tt>:Space</tt> - Whitespace character ([:blank:], newline, carriage return, etc.)
|
24
|
-
# * <tt>:Upper</tt> - Uppercase alphabetical
|
25
|
-
# * <tt>:XDigit</tt> - Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F)
|
26
|
-
# * <tt>:Word</tt> - A member of one of the following Unicode general category Letter, Mark, Number, Connector_Punctuation
|
27
|
-
# * <tt>:ASCII</tt> - A character in the ASCII character set
|
28
|
-
# * <tt>:Any</tt> - Any Unicode character (including unassigned characters)
|
29
|
-
# * <tt>:Assigned</tt> - An assigned character
|
30
|
-
#
|
31
|
-
# A Unicode character's General Category value can also be matched with :Ab where Ab is the category’s abbreviation as described below:
|
32
|
-
# * <tt>:L</tt> - 'Letter'
|
33
|
-
# * <tt>:Ll</tt> - 'Letter: Lowercase'
|
34
|
-
# * <tt>:Lm</tt> - 'Letter: Mark'
|
35
|
-
# * <tt>:Lo</tt> - 'Letter: Other'
|
36
|
-
# * <tt>:Lt</tt> - 'Letter: Titlecase'
|
37
|
-
# * <tt>:Lu</tt> - 'Letter: Uppercase
|
38
|
-
# * <tt>:Lo</tt> - 'Letter: Other'
|
39
|
-
# * <tt>:M</tt> - 'Mark'
|
40
|
-
# * <tt>:Mn</tt> - 'Mark: Nonspacing'
|
41
|
-
# * <tt>:Mc</tt> - 'Mark: Spacing Combining'
|
42
|
-
# * <tt>:Me</tt> - 'Mark: Enclosing'
|
43
|
-
# * <tt>:N</tt> - 'Number'
|
44
|
-
# * <tt>:Nd</tt> - 'Number: Decimal Digit'
|
45
|
-
# * <tt>:Nl</tt> - 'Number: Letter'
|
46
|
-
# * <tt>:No</tt> - 'Number: Other'
|
47
|
-
# * <tt>:P</tt> - 'Punctuation'
|
48
|
-
# * <tt>:Pc</tt> - 'Punctuation: Connector'
|
49
|
-
# * <tt>:Pd</tt> - 'Punctuation: Dash'
|
50
|
-
# * <tt>:Ps</tt> - 'Punctuation: Open'
|
51
|
-
# * <tt>:Pe</tt> - 'Punctuation: Close'
|
52
|
-
# * <tt>:Pi</tt> - 'Punctuation: Initial Quote'
|
53
|
-
# * <tt>:Pf</tt> - 'Punctuation: Final Quote'
|
54
|
-
# * <tt>:Po</tt> - 'Punctuation: Other'
|
55
|
-
# * <tt>:S</tt> - 'Symbol'
|
56
|
-
# * <tt>:Sm</tt> - 'Symbol: Math'
|
57
|
-
# * <tt>:Sc</tt> - 'Symbol: Currency'
|
58
|
-
# * <tt>:Sc</tt> - 'Symbol: Currency'
|
59
|
-
# * <tt>:Sk</tt> - 'Symbol: Modifier'
|
60
|
-
# * <tt>:So</tt> - 'Symbol: Other'
|
61
|
-
# * <tt>:Z</tt> - 'Separator'
|
62
|
-
# * <tt>:Zs</tt> - 'Separator: Space'
|
63
|
-
# * <tt>:Zl</tt> - 'Separator: Line'
|
64
|
-
# * <tt>:Zp</tt> - 'Separator: Paragraph'
|
65
|
-
# * <tt>:C</tt> - 'Other'
|
66
|
-
# * <tt>:Cc</tt> - 'Other: Control'
|
67
|
-
# * <tt>:Cf</tt> - 'Other: Format'
|
68
|
-
# * <tt>:Cn</tt> - 'Other: Not Assigned'
|
69
|
-
# * <tt>:Co</tt> - 'Other: Private Use'
|
70
|
-
# * <tt>:Cs</tt> - 'Other: Surrogate'
|
107
|
+
# This method returns the object itself when the first argument is not a String.
|
71
108
|
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
# @example
|
77
|
-
# KeepFilter.evaluate("Doom 3", :L) #=> "Doom" keep only letters
|
78
|
-
# KeepFilter.evaluate("Doom 3", :N) #=> "3" keep only numbers
|
79
|
-
# KeepFilter.evaluate("Doom 3", :L, :N) #=> "Doom3" keep only letters and numbers
|
80
|
-
# KeepFilter.evaluate("Doom 3", :Lu, :N) #=> "D3" keep only uppercased letters or numbers
|
81
|
-
# KeepFilter.evaluate("Doom ˩", :Latin) #=> "Doom" keep only latin characters
|
82
|
-
#
|
83
|
-
# @example Using as ActiveRecord::Base normalizer
|
84
|
-
# normalize :attribute_a, :with => [[:keep, :Lu]]
|
85
|
-
# normalize :attribute_b, :with => [{:keep =>[:Lu]}]
|
86
|
-
# normalize :attribute_c, :with => [:custom_filter, [:keep, :Ll, :Space]]
|
87
|
-
# normalize :attribute_d, :with => [:custom_filter, {:keep => [:Ll, :Space]}]
|
88
|
-
#
|
89
|
-
# @param [String] input A character sequence
|
90
|
-
# @param [[Symbol]*] properties Array of Symbols equivalent to Regexp property for \\p{} construct.
|
91
|
-
# @return [String] The clean character sequence or the object itself
|
92
|
-
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
93
|
-
# @see RemoveFilter Normatron::Filters::RemoveFilter
|
94
|
-
# @todo Raise exception for empty properties
|
109
|
+
# @param input [String] The String to be filtered
|
110
|
+
# @param properties [[Symbol]*] Symbols equivalent to Regexp property for @\\p{}@ construct
|
111
|
+
# @return [String] A new clean String
|
95
112
|
def self.evaluate(input, *properties)
|
96
113
|
input.kind_of?(String) ? evaluate_regexp(input, :keep, properties) : input
|
97
114
|
end
|
@@ -2,33 +2,38 @@ require 'normatron/filters/helpers'
|
|
2
2
|
|
3
3
|
module Normatron
|
4
4
|
module Filters
|
5
|
+
|
6
|
+
##
|
7
|
+
# Remove the characters that match the given properties.
|
8
|
+
#
|
9
|
+
# For additional informations see Normatron::Filter::ClassMethods#keep documentation.
|
10
|
+
#
|
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
|
17
|
+
#
|
18
|
+
# @example Using as ActiveRecord::Base normalizer
|
19
|
+
# normalize :attribute_a, :with => [[:remove, :Lu]]
|
20
|
+
# normalize :attribute_b, :with => [{:remove =>[:Lu]}]
|
21
|
+
# normalize :attribute_c, :with => [:custom_filter, [:remove, :Ll, :Space]]
|
22
|
+
# normalize :attribute_d, :with => [:custom_filter, {:remove => [:Ll, :Space]}]
|
23
|
+
#
|
24
|
+
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
25
|
+
# @see KeepFilter Normatron::Filters::KeepFilter
|
5
26
|
module RemoveFilter
|
6
27
|
extend Helpers
|
7
28
|
|
8
29
|
##
|
9
|
-
#
|
30
|
+
# Performs input conversion according to filter requirements.
|
10
31
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# @example
|
14
|
-
# RemoveFilter.evaluate("Quake 3", :L) #=> " 3" remove only letters
|
15
|
-
# RemoveFilter.evaluate("Quake 3", :N) #=> "Quake " remove only numbers
|
16
|
-
# RemoveFilter.evaluate("Quake 3", :L, :N) #=> " " remove only letters or numbers
|
17
|
-
# RemoveFilter.evaluate("Quake 3", :Lu, :N) #=> "uake " remove only uppercased letters or numbers
|
18
|
-
# RemoveFilter.evaluate("Quake ˩", :Latin) #=> " ˩" remove only latin characters
|
32
|
+
# This method returns the object itself when the first argument is not a String.
|
19
33
|
#
|
20
|
-
# @
|
21
|
-
#
|
22
|
-
#
|
23
|
-
# normalize :attribute_c, :with => [:custom_filter, [:remove, :Ll, :Space]]
|
24
|
-
# normalize :attribute_d, :with => [:custom_filter, {:remove => [:Ll, :Space]}]
|
25
|
-
#
|
26
|
-
# @param [String] input A character sequence
|
27
|
-
# @param [[Symbol]*] properties Array of Symbols equivalent to Regexp property for \\p{} construct.
|
28
|
-
# @return [String] The clean character sequence or the object itself
|
29
|
-
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
30
|
-
# @see KeepFilter Normatron::Filters::KeepFilter
|
31
|
-
# @todo Raise exception for empty properties
|
34
|
+
# @param input [String] The String to be filtered
|
35
|
+
# @param properties [[Symbol]*] Symbols equivalent to Regexp property for @\\p{}@ construct
|
36
|
+
# @return [String] A new clean String
|
32
37
|
def self.evaluate(input, *properties)
|
33
38
|
input.kind_of?(String) ? evaluate_regexp(input, :remove, properties) : input
|
34
39
|
end
|
@@ -1,26 +1,34 @@
|
|
1
1
|
module Normatron
|
2
2
|
module Filters
|
3
|
+
|
4
|
+
##
|
5
|
+
# Remove multiple occurences of the same character.
|
6
|
+
#
|
7
|
+
# If no option are given, all runs of identical characters are replaced by a single character.
|
8
|
+
#
|
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"
|
13
|
+
#
|
14
|
+
# @example Using as ActiveRecord::Base normalizer
|
15
|
+
# normalize :attribute_a, :with => [:custom_filter, :squeeze]
|
16
|
+
# normalize :attribute_b, :with => [:custom_filter, [:squeeze, "a-f"]]
|
17
|
+
# normalize :attribute_c, :with => [:custom_filter, {:squeeze => ["a-f"]}]
|
18
|
+
#
|
19
|
+
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-squeeze String#squeeze
|
20
|
+
# @see SquishFilter Normatron::Filters::SquishFilter
|
21
|
+
# @see StripFilter Normatron::Filters::StripFilter
|
3
22
|
module SqueezeFilter
|
4
23
|
|
5
24
|
##
|
6
|
-
#
|
7
|
-
# If no option are given, all runs of identical characters are replaced by a single character.
|
8
|
-
#
|
9
|
-
# @example
|
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"
|
25
|
+
# Performs input conversion according to filter requirements.
|
13
26
|
#
|
14
|
-
#
|
15
|
-
# normalize :attribute_a, :with => [:custom_filter, :squeeze]
|
16
|
-
# normalize :attribute_b, :with => [:custom_filter, [:squeeze, "a-f"]]
|
17
|
-
# normalize :attribute_c, :with => [:custom_filter, {:squeeze => ["a-f"]}]
|
27
|
+
# This method returns the object itself when the first argument is not a String.
|
18
28
|
#
|
19
|
-
# @param [String]
|
20
|
-
# @param [[String]*]
|
21
|
-
# @return [String]
|
22
|
-
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-squeeze String#squeeze
|
23
|
-
# @see SquishFilter Normatron::Filters::SquishFilter
|
29
|
+
# @param input [String] The String to be filtered
|
30
|
+
# @param targets [[String]*] Characters to be affected
|
31
|
+
# @return [String] A new squeezed String
|
24
32
|
def self.evaluate(input, *targets)
|
25
33
|
return input unless input.kind_of?(String)
|
26
34
|
targets.any? ? input.squeeze(targets.last) : input.squeeze
|
@@ -2,24 +2,31 @@ require 'normatron/filters/helpers'
|
|
2
2
|
|
3
3
|
module Normatron
|
4
4
|
module Filters
|
5
|
+
|
6
|
+
##
|
7
|
+
# Strip input, remove line-breaks and multiple spaces.
|
8
|
+
#
|
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"
|
14
|
+
#
|
15
|
+
# @example Using as ActiveRecord::Base normalizer
|
16
|
+
# normalize :attribute, :with => [:custom_filter, :squish]
|
17
|
+
#
|
18
|
+
# @see http://api.rubyonrails.org/classes/String.html#method-i-squish String#squish
|
19
|
+
# @see SqueezeFilter Normatron::Filters::SqueezeFilter
|
20
|
+
# @see StripFilter Normatron::Filters::StripFilter
|
5
21
|
module SquishFilter
|
6
22
|
|
7
23
|
##
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
# SquishFilter.evaluate(" the simpsons ") #=> "the simpsons"
|
12
|
-
# SquishFilter.evaluate("family guy") #=> "family guy"
|
13
|
-
# SquishFilter.evaluate("the \n simpsons") #=> "the simpsons"
|
14
|
-
# SquishFilter.evaluate("the\nsimpsons") #=> "the simpsons"
|
24
|
+
# Performs input conversion according to filter requirements.
|
15
25
|
#
|
16
|
-
#
|
17
|
-
# normalize :attribute, :with => [:custom_filter, :squish]
|
26
|
+
# This method returns the object itself when the first argument is not a String.
|
18
27
|
#
|
19
|
-
# @param [String]
|
20
|
-
# @return [String]
|
21
|
-
# @see http://api.rubyonrails.org/classes/String.html#method-i-squish String#squish
|
22
|
-
# @see SqueezeFilter Normatron::Filters::SqueezeFilter
|
28
|
+
# @param input [String] The String to be filtered
|
29
|
+
# @return [String] A new squished String
|
23
30
|
def self.evaluate(input)
|
24
31
|
input.kind_of?(String) ? input.squish : input
|
25
32
|
end
|
@@ -2,29 +2,37 @@ require 'normatron/filters/helpers'
|
|
2
2
|
|
3
3
|
module Normatron
|
4
4
|
module Filters
|
5
|
+
|
6
|
+
##
|
7
|
+
# Removes traling and leading spaces.
|
8
|
+
#
|
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"
|
14
|
+
#
|
15
|
+
# @example Using as ActiveRecord::Base normalizer
|
16
|
+
# normalize :attribute_a, :with => :strip
|
17
|
+
# normalize :attribute_b, :with => { :strip => :L }
|
18
|
+
# normalize :attribute_c, :with => [:custom_filter, :strip]
|
19
|
+
# normalize :attribute_d, :with => [:custom_filter, [:strip, :L]]
|
20
|
+
# normalize :attribute_e, :with => [:custom_filter, {:strip => :R}]
|
21
|
+
#
|
22
|
+
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-strip String#strip
|
23
|
+
# @see SqueezeFilter Normatron::Filters::SqueezeFilter
|
24
|
+
# @see SquishFilter Normatron::Filters::SquishFilter
|
5
25
|
module StripFilter
|
6
26
|
extend Helpers
|
7
27
|
|
8
28
|
##
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# @example
|
12
|
-
# StripFilter.evaluate(" copy ") #=> "copy"
|
13
|
-
# StripFilter.evaluate(" copy ", :L) #=> "copy "
|
14
|
-
# StripFilter.evaluate(" copy ", :R) #=> " copy"
|
15
|
-
# StripFilter.evaluate(" copy ", :LR) #=> "copy"
|
29
|
+
# Performs input conversion according to filter requirements.
|
16
30
|
#
|
17
|
-
#
|
18
|
-
# normalize :attribute_a, :with => :strip
|
19
|
-
# normalize :attribute_b, :with => { :strip => :L }
|
20
|
-
# normalize :attribute_c, :with => [:custom_filter, :strip]
|
21
|
-
# normalize :attribute_d, :with => [:custom_filter, [:strip, :L]]
|
22
|
-
# normalize :attribute_e, :with => [:custom_filter, {:strip => :R}]
|
31
|
+
# This method returns the object itself when the first argument is not a String.
|
23
32
|
#
|
24
|
-
# @param [String]
|
25
|
-
# @param [Symbol]
|
26
|
-
# @return [String]
|
27
|
-
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-strip String#strip
|
33
|
+
# @param input [String] The String to be filtered
|
34
|
+
# @param edges [Symbol] @:L@ to strip trailing spaces, @:R@ for leading spaces or @:LR@ for both
|
35
|
+
# @return [String] A new stripped String
|
28
36
|
def self.evaluate(input, edges=:LR)
|
29
37
|
return input unless input.kind_of?(String)
|
30
38
|
|
@@ -2,25 +2,31 @@ require 'normatron/filters/helpers'
|
|
2
2
|
|
3
3
|
module Normatron
|
4
4
|
module Filters
|
5
|
+
|
6
|
+
##
|
7
|
+
# Replaces uppercased characters by lowercased and vice versa.
|
8
|
+
#
|
9
|
+
# @example Out of box
|
10
|
+
# SwapcaseFilter.evaluate("As you Wish!") #=> "aS YOU wISH!"
|
11
|
+
#
|
12
|
+
# @example Using as ActiveRecord::Base normalizer
|
13
|
+
# normalize :attribute_a, :with => :swapcase
|
14
|
+
# normalize :attribute_b, :with => [:custom_filter, :swapcase]
|
15
|
+
#
|
16
|
+
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-swapcase String#swapcase
|
17
|
+
# @see DownFilter Normatron::Filters::DownFilter
|
18
|
+
# @see TitleizeFilter Normatron::Filters::TitleizeFilter
|
19
|
+
# @see UpcaseFilter Normatron::Filters::UpcaseFilter
|
5
20
|
module SwapcaseFilter
|
6
21
|
extend Helpers
|
7
22
|
|
8
23
|
##
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# @example
|
12
|
-
# SwapcaseFilter.evaluate("As you Wish!") #=> "aS YOU wISH!"
|
24
|
+
# Performs input conversion according to filter requirements.
|
13
25
|
#
|
14
|
-
#
|
15
|
-
# normalize :attribute_a, :with => :swapcase
|
16
|
-
# normalize :attribute_b, :with => [:custom_filter, :swapcase]
|
26
|
+
# This method returns the object itself when the first argument is not a String.
|
17
27
|
#
|
18
|
-
# @param [String]
|
19
|
-
# @return [String]
|
20
|
-
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-swapcase String#swapcase
|
21
|
-
# @see DownFilter Normatron::Filters::DownFilter
|
22
|
-
# @see TitleizeFilter Normatron::Filters::TitleizeFilter
|
23
|
-
# @see UpcaseFilter Normatron::Filters::UpcaseFilter
|
28
|
+
# @param input [String] The String to be filtered
|
29
|
+
# @return [String] A new swapcased String
|
24
30
|
def self.evaluate(input)
|
25
31
|
return input unless input.kind_of?(String)
|
26
32
|
input.gsub(/([\p{Ll}])|(\p{Lu})|([^\p{Ll}\p{Lu}])/u) { $3 || ($2 ? mb_send(:downcase, $2) : mb_send(:upcase, $1)) }
|