normatron 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.textile
CHANGED
@@ -39,18 +39,21 @@ class Product < ActiveRecord::Base
|
|
39
39
|
end
|
40
40
|
</pre>
|
41
41
|
|
42
|
-
And you want the
|
42
|
+
And you want the _name_ attribute be uppercased before saving it into the database.
|
43
43
|
The most usual approach to do this includes:
|
44
44
|
|
45
|
-
* Override the
|
46
|
-
* Use the
|
45
|
+
* Override the _name_ setter and convert the value to an uppercased string.
|
46
|
+
* Use the _before_validation_ callback to run a method or block doing the task.
|
47
47
|
|
48
48
|
Both ways are ilenegant, boring, error prone and very expensive.
|
49
49
|
What led me to make this gem and offer a third way to solve the problem:
|
50
50
|
|
51
51
|
h2. Usage
|
52
52
|
|
53
|
-
h3.
|
53
|
+
h3. The default filters
|
54
|
+
|
55
|
+
Normatron uses *:squish* and *:blank* as default filters.
|
56
|
+
These filters are applied to all attributes in *normalize* function, since no options was given.
|
54
57
|
|
55
58
|
<pre>
|
56
59
|
# ./app/models/products.rb
|
@@ -59,52 +62,87 @@ class Product < ActiveRecord::Base
|
|
59
62
|
normalize :name
|
60
63
|
end
|
61
64
|
|
62
|
-
$ rails console
|
63
|
-
>
|
64
|
-
>
|
65
|
+
$ rails console
|
66
|
+
> p = Product.create name: " memory card "
|
67
|
+
> p.name
|
65
68
|
=> "memory card"
|
66
|
-
>
|
67
|
-
>
|
69
|
+
> p = Product.create name: " "
|
70
|
+
> p.name
|
68
71
|
=> nil
|
69
72
|
</pre>
|
70
73
|
|
71
|
-
|
74
|
+
You can also apply the default filters to multiple attributes as following:
|
72
75
|
|
73
|
-
|
76
|
+
<pre>normalize :login, :email, :address, :city</pre>
|
74
77
|
|
75
|
-
|
78
|
+
h3. The callback
|
76
79
|
|
77
|
-
|
80
|
+
All filters are automatically applyied in the *before_validation* callback.
|
81
|
+
So, the changes occur when you call any method that validates de model.
|
78
82
|
|
79
|
-
|
83
|
+
<pre>
|
84
|
+
$ rails console
|
85
|
+
> p = Product.create name: "mouse"
|
86
|
+
> p.name
|
87
|
+
=> "MOUSE"
|
88
|
+
> p = Product.new name: "keyboard"
|
89
|
+
> p.valid?
|
90
|
+
> p.name
|
91
|
+
=> "KEYBOARD"
|
92
|
+
> p = Product.new name: "dvd"
|
93
|
+
> p.save
|
94
|
+
> p.name
|
95
|
+
=> "DVD"
|
96
|
+
</pre>
|
80
97
|
|
81
|
-
|
98
|
+
It's possible to call the *apply_normalizations* method. That perform the normalization without do the validations.
|
82
99
|
|
83
|
-
|
100
|
+
<pre>
|
101
|
+
> p = Product.new name: "hd"
|
102
|
+
> p.apply_normalizations
|
103
|
+
> p.name
|
104
|
+
=> "HD"
|
105
|
+
</pre>
|
84
106
|
|
85
|
-
h3.
|
107
|
+
h3. Specifying filters
|
108
|
+
|
109
|
+
Just add the *:with* option and pass the filter you want.
|
110
|
+
All Symbols outside the *:with* Hash are flagged as an attribute.
|
111
|
+
|
112
|
+
<pre>normalize :login, :address, :city, :with => :upcase</pre>
|
113
|
+
|
114
|
+
To apply multiple filters, pass it as an array:
|
115
|
+
|
116
|
+
<pre>
|
117
|
+
normalize :login, :address, :city, :with => [:upcase, :blank, :strip]
|
118
|
+
normalize :email, :with => [:downcase, :blank, :strip]
|
119
|
+
</pre>
|
120
|
+
|
121
|
+
h3. Mixing all together
|
122
|
+
|
123
|
+
You can stack filters to an attribute calling the *normalize* method multiple times.
|
86
124
|
|
87
125
|
<pre>
|
88
126
|
# ./app/models/user.rb
|
89
127
|
class User < ActiveRecord::Base
|
90
128
|
attr_accessible :name, :surname, :login, :password, :email
|
129
|
+
|
91
130
|
normalize :name, :surname, :email, :login, :with => [:blank, :squish]
|
92
131
|
normalize :name, :surname, :with => :upcase
|
93
132
|
normalize :email, :with => :downcase
|
94
133
|
end
|
95
134
|
|
96
|
-
$ rails console
|
97
|
-
> u = User.
|
98
|
-
> u.name
|
99
|
-
|
100
|
-
> u.
|
101
|
-
|
102
|
-
> u.
|
103
|
-
|
135
|
+
$ rails console
|
136
|
+
> u = User.new
|
137
|
+
> u.name = " \n \f \r \t "
|
138
|
+
> u.surname = " norris "
|
139
|
+
> u.login = " iKICKasses"
|
140
|
+
> u.email = "GMAIL@CHUCKNORRIS.COM"
|
141
|
+
> u.valid?
|
142
|
+
> u
|
143
|
+
=> #<User id: nil, name: nil, surname: "NORRIS", login: "iKICKasses", email: "gmail@chucknorris.com">
|
104
144
|
</pre>
|
105
145
|
|
106
|
-
You can stack filters to an attribute calling the *normalize* method multiple times.
|
107
|
-
|
108
146
|
h3. Using custom instance method as a filter
|
109
147
|
|
110
148
|
<pre>
|
@@ -131,26 +169,25 @@ Create an instance method that returns the value as you want. The method must ha
|
|
131
169
|
h2. Filters
|
132
170
|
|
133
171
|
table{border:1px solid black}.
|
134
|
-
|_. FILTER|_.
|
135
|
-
|:blank|
|
136
|
-
|:
|
137
|
-
|:
|
138
|
-
|:
|
139
|
-
|:
|
140
|
-
|:
|
141
|
-
|:
|
142
|
-
|:
|
143
|
-
|:
|
144
|
-
|:
|
145
|
-
|:
|
146
|
-
|:squish|" \n \r \t \f "|""|
|
147
|
-
|:squish|" Master of Puppets "|"Master of Puppets"|
|
148
|
-
|:strip|" Final Fantasy "|"Final Fantasy"|
|
149
|
-
|:titlecase or :titleize|" system of a down "|" System Of A Down "|
|
150
|
-
|:upcase|"text"|"TEXT"|
|
172
|
+
|_. FILTER|_. DESCRIPTION|
|
173
|
+
|:blank|Convert a blank String into a Nil object|
|
174
|
+
|:capitalize|Downcase all characters in the sequence and upcase the first|
|
175
|
+
|:dasherize|Replace all underscores with dashes|
|
176
|
+
|:downcase|Downcase all characters in the sequence|
|
177
|
+
|:lstrip|Remove trailing spaces|
|
178
|
+
|:rstrip|Remove leading spaces|
|
179
|
+
|:squeeze|Replace followed occurrences of the same character by a single one|
|
180
|
+
|:squish|Remove trailing, leading, surplus spaces and scape characters|
|
181
|
+
|:strip|Remove trailing and leading spaces|
|
182
|
+
|:title|Downcase all characters in the sequence and upcase the first character of each word|
|
183
|
+
|:upcase|Upcase all characters in the sequence|
|
151
184
|
|
152
185
|
h1. Changelog
|
153
186
|
|
187
|
+
- 0.1.0 :=
|
188
|
+
* Two new filters: dasherize and squeeze
|
189
|
+
* Native filters replaced by StringInflections
|
190
|
+
=:
|
154
191
|
- 0.0.7 :=
|
155
192
|
* Ability to use an instance method as a filter
|
156
193
|
=:
|
@@ -159,8 +196,9 @@ h1. Contributing
|
|
159
196
|
|
160
197
|
* Fork it, add your new features or bug fixes, make your tests and commit.
|
161
198
|
* Report any bug or unexpected behavior.
|
199
|
+
* Share with your friends, forums, communities, job, etc...
|
162
200
|
* Send me your feedback.
|
163
|
-
*
|
201
|
+
* Offer me a job or pay me a beer. =]
|
164
202
|
|
165
203
|
h1. Special Thanks
|
166
204
|
|
@@ -169,6 +207,7 @@ He have a lot of interesting gems on his repo, please take a look.
|
|
169
207
|
|
170
208
|
h1. Licence
|
171
209
|
|
210
|
+
<pre>
|
172
211
|
Copyright 2012 Fernando Rodrigues da Silva
|
173
212
|
|
174
213
|
Permission is hereby granted, free of charge, to any person obtaining
|
@@ -188,4 +227,5 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
188
227
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
189
228
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
190
229
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
191
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
230
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
231
|
+
</pre>
|
data/lib/normatron.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "normatron/filters/
|
1
|
+
require "normatron/filters/string_inflections"
|
2
2
|
|
3
3
|
module Normatron
|
4
4
|
module Extensions
|
@@ -34,7 +34,7 @@ module Normatron
|
|
34
34
|
if self.respond_to? filter
|
35
35
|
value = send(filter, value)
|
36
36
|
else
|
37
|
-
value = Normatron::Filters::
|
37
|
+
value = Normatron::Filters::StringInflections.send(filter, value)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require "active_support/core_ext/string"
|
2
|
+
|
3
|
+
module Normatron
|
4
|
+
module Filters
|
5
|
+
module StringInflections
|
6
|
+
class << self
|
7
|
+
|
8
|
+
##
|
9
|
+
# Convert a blank string into a nil object.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# StringInflections.blank("") #=> nil
|
13
|
+
# StringInflections.blank(" ") #=> nil
|
14
|
+
# StringInflections.blank("\n") #=> nil
|
15
|
+
# StringInflections.blank("1") #=> "1"
|
16
|
+
# StringInflections.blank(0) #=> 0
|
17
|
+
# @param [String] value Any character sequence
|
18
|
+
# @return [String, nil] The object itself or nil
|
19
|
+
#
|
20
|
+
# @see http://api.rubyonrails.org/classes/String.html#method-i-blank-3F ActiveSupport::CoreExt::Object#blank?
|
21
|
+
def blank(value)
|
22
|
+
if is_a_string?(value)
|
23
|
+
value.to_s.blank? ? nil : value
|
24
|
+
else
|
25
|
+
value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Downcase all characters in the sequence, except the first.
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# StringInflections.capitalize("mASTER OF PUPPETS") #=> "Master of puppets"
|
34
|
+
# StringInflections.capitalize(" mASTER OF PUPPETS") #=> " master of puppets"
|
35
|
+
# StringInflections.capitalize(1) #=> 1
|
36
|
+
# @param [String] value Any character sequence
|
37
|
+
# @return [String] The object itself or a capitalized string
|
38
|
+
#
|
39
|
+
# @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-capitalize ActiveSupport::Multibyte::Chars#capitalize
|
40
|
+
def capitalize(value)
|
41
|
+
evaluate(:capitalize, value)
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Replace all underscores with dashes.
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
# StringInflections.dasherize("__ shoot _ to _ thrill __") #=> "-- shoot - to - thrill --"
|
49
|
+
# StringInflections.dasherize(1) #=> 1
|
50
|
+
# @param [String] value Any character sequence
|
51
|
+
# @return [String] The object itself or a capitalized string
|
52
|
+
#
|
53
|
+
# @see http://api.rubyonrails.org/classes/String.html#method-i-dasherize ActiveSupport::CoreExt::Object#dasherize
|
54
|
+
def dasherize(value)
|
55
|
+
evaluate(:dasherize, value)
|
56
|
+
end
|
57
|
+
|
58
|
+
def downcase(value)
|
59
|
+
evaluate(:downcase, value)
|
60
|
+
end
|
61
|
+
|
62
|
+
def lstrip(value)
|
63
|
+
evaluate(:lstrip, value)
|
64
|
+
end
|
65
|
+
|
66
|
+
def rstrip(value)
|
67
|
+
evaluate(:rstrip, value)
|
68
|
+
end
|
69
|
+
|
70
|
+
def squeeze(value)
|
71
|
+
evaluate(:squeeze, value)
|
72
|
+
end
|
73
|
+
|
74
|
+
def squish(value)
|
75
|
+
evaluate(:squish, value)
|
76
|
+
end
|
77
|
+
|
78
|
+
def strip(value)
|
79
|
+
evaluate(:strip, value)
|
80
|
+
end
|
81
|
+
|
82
|
+
def title(value)
|
83
|
+
evaluate(:titlecase, value)
|
84
|
+
end
|
85
|
+
|
86
|
+
def upcase(value)
|
87
|
+
evaluate(:upcase, value)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def evaluate(method_name, value)
|
93
|
+
if need_type_cast?(method_name, value)
|
94
|
+
value.mb_chars.send(method_name)
|
95
|
+
elsif is_a_string?(value)
|
96
|
+
value.send(method_name)
|
97
|
+
else
|
98
|
+
value
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def need_type_cast?(method_name, value)
|
103
|
+
if value.is_a?(String)
|
104
|
+
case method_name
|
105
|
+
when :capitalize, :downcase, :titlecase, :upcase
|
106
|
+
true
|
107
|
+
end
|
108
|
+
else
|
109
|
+
false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def is_a_string?(value)
|
114
|
+
value.is_a?(ActiveSupport::Multibyte::Chars) || value.is_a?(String)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,266 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Normatron::Filters::StringInflections do
|
6
|
+
|
7
|
+
let(:mod) { Normatron::Filters::StringInflections }
|
8
|
+
let(:val) { @value }
|
9
|
+
|
10
|
+
describe :blank do
|
11
|
+
it "should return nil for empty strings" do
|
12
|
+
value = ""
|
13
|
+
mod.blank(value).should be_nil
|
14
|
+
mod.blank(value.mb_chars).should be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return nil for blank spaces strings" do
|
18
|
+
value = " "
|
19
|
+
mod.blank(value).should be_nil
|
20
|
+
mod.blank(value.mb_chars).should be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should return nil for \n \t \r \f strings' do
|
24
|
+
value = "\n \t \r \f"
|
25
|
+
mod.blank(value).should be_nil
|
26
|
+
mod.blank(value.mb_chars).should be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not affect filled string" do
|
30
|
+
value = "baCon"
|
31
|
+
expected = "baCon"
|
32
|
+
mod.blank(value).should eq expected
|
33
|
+
mod.blank(value.mb_chars).should eq expected.mb_chars
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not affect non string objects" do
|
37
|
+
mod.blank(nil).should eq nil
|
38
|
+
mod.blank(1).should eq 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe :capitalize do
|
43
|
+
it "should upcase first char and downcase others" do
|
44
|
+
value = "mASTER OF PUPPETS"
|
45
|
+
expected = "Master of puppets"
|
46
|
+
mod.capitalize(value).should eq expected
|
47
|
+
mod.capitalize(value.mb_chars).should eq expected.mb_chars
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should downcase all chars in string starting with spaces" do
|
51
|
+
value = " mASTER OF PUPPETS"
|
52
|
+
expected = " master of puppets"
|
53
|
+
mod.capitalize(value).should eq expected
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should affect accented chars" do
|
57
|
+
value = "ILÍADA"
|
58
|
+
expected = "Ilíada"
|
59
|
+
mod.capitalize(value).should eq expected
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not affect non string objects" do
|
63
|
+
mod.capitalize(nil).should eq nil
|
64
|
+
mod.capitalize(1).should eq 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe :dasherize do
|
69
|
+
it "should replaces underscores with dashes" do
|
70
|
+
value = "__ shoot _ to _ thrill __"
|
71
|
+
expected = "-- shoot - to - thrill --"
|
72
|
+
mod.dasherize(value).should eq expected
|
73
|
+
mod.dasherize(value.mb_chars).should eq expected.mb_chars
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should not affect non string objects" do
|
77
|
+
mod.dasherize(nil).should eq nil
|
78
|
+
mod.dasherize(1).should eq 1
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe :downcase do
|
83
|
+
it "should downcase all characters" do
|
84
|
+
value = "KILL'EM ALL"
|
85
|
+
expected = "kill'em all"
|
86
|
+
mod.downcase(value).should eq expected
|
87
|
+
mod.downcase(value.mb_chars).should eq expected.mb_chars
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should affect accented chars" do
|
91
|
+
value = "ÊXITO"
|
92
|
+
expected = "êxito"
|
93
|
+
mod.downcase(value).should eq expected
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should not affect non string objects" do
|
97
|
+
mod.downcase(nil).should eq nil
|
98
|
+
mod.downcase(1).should eq 1
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe :lstrip do
|
103
|
+
it "should remove trailing spaces" do
|
104
|
+
value = " black "
|
105
|
+
expected = "black "
|
106
|
+
mod.lstrip(value).should eq expected
|
107
|
+
mod.lstrip(value.mb_chars).should eq expected.mb_chars
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should not affect non string objects" do
|
111
|
+
mod.lstrip(nil).should eq nil
|
112
|
+
mod.lstrip(1).should eq 1
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe :rstrip do
|
117
|
+
it "should remove leading spaces" do
|
118
|
+
value = " load "
|
119
|
+
expected = " load"
|
120
|
+
mod.rstrip(value).should eq expected
|
121
|
+
mod.rstrip(value.mb_chars).should eq expected.mb_chars
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should not affect non string objects" do
|
125
|
+
mod.rstrip(nil).should eq nil
|
126
|
+
mod.rstrip(1).should eq 1
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe :squeeze do
|
131
|
+
it "should replace multiple occurrences of a char for a single one" do
|
132
|
+
value = "rock'n roll \n\n ain't noise pollution"
|
133
|
+
expected = "rock'n rol \n ain't noise polution"
|
134
|
+
mod.squeeze(value).should eq expected
|
135
|
+
mod.squeeze(value.mb_chars).should eq expected.mb_chars
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should not affect non string objects" do
|
139
|
+
mod.squeeze(nil).should eq nil
|
140
|
+
mod.squeeze(1).should eq 1
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe :squish do
|
145
|
+
it "should remove trailing and leading spaces" do
|
146
|
+
value = " and justice for all... "
|
147
|
+
expected = "and justice for all..."
|
148
|
+
mod.squish(value).should eq expected
|
149
|
+
mod.squish(value.mb_chars).should eq expected.mb_chars
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should remove multiple spaces" do
|
153
|
+
value = "and justice for all..."
|
154
|
+
expected = "and justice for all..."
|
155
|
+
mod.squish(value).should eq expected
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'should remove \n \r \f \t' do
|
159
|
+
value = "\tand\njustice\rfor\fall..."
|
160
|
+
expected = "and justice for all..."
|
161
|
+
mod.squish(value).should eq expected
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should not affect non string objects" do
|
165
|
+
mod.squish(nil).should eq nil
|
166
|
+
mod.squish(1).should eq 1
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe :strip do
|
171
|
+
it "should remove trailing and leading spaces" do
|
172
|
+
value = " reload "
|
173
|
+
expected = "reload"
|
174
|
+
mod.strip(value).should eq expected
|
175
|
+
mod.strip(value.mb_chars).should eq expected.mb_chars
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should not affect non string objects" do
|
179
|
+
mod.strip(nil).should eq nil
|
180
|
+
mod.strip(1).should eq 1
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe :title do
|
185
|
+
it "should upcase first char of each word, others downcased" do
|
186
|
+
value = "dirty DEEDS DONE dirt cheap"
|
187
|
+
expected = "Dirty Deeds Done Dirt Cheap"
|
188
|
+
mod.title(value).should eq expected
|
189
|
+
mod.title(value.mb_chars).should eq expected.mb_chars
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should affect accented chars" do
|
193
|
+
value = "isto NÃO é verdade"
|
194
|
+
expected = "Isto Não É Verdade"
|
195
|
+
mod.title(value).should eq expected
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should not affect non string objects" do
|
199
|
+
mod.title(nil).should eq nil
|
200
|
+
mod.title(1).should eq 1
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
describe :upcase do
|
205
|
+
it "should upcase all characters" do
|
206
|
+
value = "Ride the lightning"
|
207
|
+
expected = "RIDE THE LIGHTNING"
|
208
|
+
mod.upcase(value).should eq expected
|
209
|
+
mod.upcase(value.mb_chars).should eq expected.mb_chars
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should affect accented chars" do
|
213
|
+
value = "ébrio"
|
214
|
+
expected = "ÉBRIO"
|
215
|
+
mod.upcase(value).should eq expected
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should not affect non string objects" do
|
219
|
+
mod.upcase(nil).should eq nil
|
220
|
+
mod.upcase(1).should eq 1
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
pending "chomp with args"
|
225
|
+
pending "squeeze with args"
|
226
|
+
pending :camelize
|
227
|
+
pending :center
|
228
|
+
pending :chomp
|
229
|
+
pending :chop
|
230
|
+
pending :classify
|
231
|
+
pending :clear
|
232
|
+
pending :constantize
|
233
|
+
pending :deconstantize
|
234
|
+
pending :demodulize
|
235
|
+
pending :dump
|
236
|
+
pending :excerpt
|
237
|
+
pending :foreign_key
|
238
|
+
pending :highlight
|
239
|
+
pending :html
|
240
|
+
pending :humanize
|
241
|
+
pending :just
|
242
|
+
pending :ljust
|
243
|
+
pending :md
|
244
|
+
pending :ordinalize
|
245
|
+
pending :parameterize
|
246
|
+
pending :permalink
|
247
|
+
pending :pluralize
|
248
|
+
pending :pluralize
|
249
|
+
pending :reverse
|
250
|
+
pending :rjust
|
251
|
+
pending :safe_constantize
|
252
|
+
pending :simple_format
|
253
|
+
pending :singularize
|
254
|
+
pending "squeeze with args"
|
255
|
+
pending :succ
|
256
|
+
pending :swapcase
|
257
|
+
pending :tableize
|
258
|
+
pending :textile
|
259
|
+
pending :transliterate
|
260
|
+
pending :trim
|
261
|
+
pending :truncate
|
262
|
+
pending :underscore
|
263
|
+
pending :wrap
|
264
|
+
pending "remove accents"
|
265
|
+
pending "move :blank to another module"
|
266
|
+
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -69,17 +69,18 @@ executables: []
|
|
69
69
|
extensions: []
|
70
70
|
extra_rdoc_files: []
|
71
71
|
files:
|
72
|
-
- lib/normatron.rb
|
73
72
|
- lib/normatron/configuration.rb
|
74
|
-
- lib/normatron/filters/
|
73
|
+
- lib/normatron/filters/string_inflections.rb
|
75
74
|
- lib/normatron/extensions/active_record.rb
|
75
|
+
- lib/normatron.rb
|
76
76
|
- MIT-LICENSE
|
77
77
|
- Rakefile
|
78
78
|
- README.textile
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
- spec/lib/normatron_spec.rb
|
81
|
+
- spec/lib/normatron/filters/string_inflections_spec.rb
|
79
82
|
- spec/lib/normatron/configuration_spec.rb
|
80
83
|
- spec/lib/normatron/extensions/active_record_spec.rb
|
81
|
-
- spec/lib/normatron_spec.rb
|
82
|
-
- spec/spec_helper.rb
|
83
84
|
- spec/support/client_model.rb
|
84
85
|
- spec/support/schema.rb
|
85
86
|
homepage: https://github.com/fernandors87/normatron
|
@@ -107,9 +108,11 @@ signing_key:
|
|
107
108
|
specification_version: 3
|
108
109
|
summary: Normalize attributes for ActiveRecord objects.
|
109
110
|
test_files:
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/lib/normatron_spec.rb
|
113
|
+
- spec/lib/normatron/filters/string_inflections_spec.rb
|
110
114
|
- spec/lib/normatron/configuration_spec.rb
|
111
115
|
- spec/lib/normatron/extensions/active_record_spec.rb
|
112
|
-
- spec/lib/normatron_spec.rb
|
113
|
-
- spec/spec_helper.rb
|
114
116
|
- spec/support/client_model.rb
|
115
117
|
- spec/support/schema.rb
|
118
|
+
has_rdoc:
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require "active_support/all"
|
2
|
-
|
3
|
-
module Normatron
|
4
|
-
module Filters
|
5
|
-
module Native
|
6
|
-
class << self
|
7
|
-
def apply(filter, value)
|
8
|
-
return value unless value.is_a? String
|
9
|
-
|
10
|
-
case filter
|
11
|
-
when :upcase, :downcase, :capitalize, :titlecase, :titleize
|
12
|
-
value.mb_chars.send(filter).to_s
|
13
|
-
when :squish, :lstrip, :rstrip, :strip
|
14
|
-
value.send(filter)
|
15
|
-
when :blank
|
16
|
-
send("to_#{filter}", value)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def filter_names
|
21
|
-
[:upcase, :downcase, :capitalize, :titlecase, :titleize, :squish, :lstrip, :rstrip, :strip, :blank]
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def to_blank(value)
|
27
|
-
value.blank? ? nil : value
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|