normatron 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +131 -125
- data/lib/normatron.rb +3 -2
- data/lib/normatron/configuration.rb +24 -3
- data/lib/normatron/extensions/active_record.rb +40 -28
- data/lib/normatron/filters.rb +254 -6
- data/spec/configuration_spec.rb +53 -0
- data/spec/extensions/active_record_spec.rb +114 -0
- data/spec/filters_spec.rb +199 -0
- data/spec/normatron_spec.rb +18 -0
- data/spec/spec_helper.rb +0 -3
- data/spec/support/model_model.rb +3 -0
- data/spec/support/schema.rb +4 -7
- metadata +12 -18
- data/lib/normatron/filters/conversions.rb +0 -29
- data/lib/normatron/filters/string_inflections.rb +0 -155
- data/spec/lib/normatron/configuration_spec.rb +0 -45
- data/spec/lib/normatron/extensions/active_record_spec.rb +0 -147
- data/spec/lib/normatron/filters/conversions_spec.rb +0 -40
- data/spec/lib/normatron/filters/string_inflections_spec.rb +0 -268
- data/spec/lib/normatron/filters_spec.rb +0 -22
- data/spec/lib/normatron_spec.rb +0 -22
- data/spec/support/client_model.rb +0 -3
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Normatron do
|
4
|
+
|
5
|
+
specify do
|
6
|
+
described_class.should respond_to :configuration
|
7
|
+
described_class.should respond_to :configure
|
8
|
+
described_class.should respond_to :setup
|
9
|
+
described_class.should respond_to :config
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:config) { Normatron.configuration }
|
13
|
+
|
14
|
+
describe :configuration do
|
15
|
+
it { config.should be_an_instance_of Normatron::Configuration }
|
16
|
+
it { expect { |b| Normatron.setup(&b) }.to yield_control }
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
require "active_support/core_ext/string"
|
2
1
|
require "active_record"
|
3
2
|
require "normatron"
|
4
3
|
|
5
4
|
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
6
|
-
|
7
5
|
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
8
|
-
|
9
6
|
ActiveRecord::Base.send(:include, Normatron::Extensions::ActiveRecord) if defined?(ActiveRecord::Base)
|
data/spec/support/schema.rb
CHANGED
@@ -1,10 +1,7 @@
|
|
1
1
|
ActiveRecord::Schema.define(:version => 0) do
|
2
|
-
create_table :
|
3
|
-
t.string :
|
4
|
-
t.
|
5
|
-
|
6
|
-
t.date :birth_date
|
7
|
-
t.string :phone
|
8
|
-
t.integer :login_count
|
2
|
+
create_table :models do |t|
|
3
|
+
t.string :field_a
|
4
|
+
t.string :field_b
|
5
|
+
t.string :field_c
|
9
6
|
end
|
10
7
|
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.
|
4
|
+
version: 0.2.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-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -87,22 +87,18 @@ extra_rdoc_files: []
|
|
87
87
|
files:
|
88
88
|
- lib/normatron/filters.rb
|
89
89
|
- lib/normatron/configuration.rb
|
90
|
-
- lib/normatron/filters/conversions.rb
|
91
|
-
- lib/normatron/filters/string_inflections.rb
|
92
90
|
- lib/normatron/extensions/active_record.rb
|
93
91
|
- lib/normatron.rb
|
94
92
|
- MIT-LICENSE
|
95
93
|
- Rakefile
|
96
94
|
- README.textile
|
97
95
|
- spec/spec_helper.rb
|
98
|
-
- spec/
|
99
|
-
- spec/lib/normatron/filters_spec.rb
|
100
|
-
- spec/lib/normatron/filters/conversions_spec.rb
|
101
|
-
- spec/lib/normatron/filters/string_inflections_spec.rb
|
102
|
-
- spec/lib/normatron/configuration_spec.rb
|
103
|
-
- spec/lib/normatron/extensions/active_record_spec.rb
|
104
|
-
- spec/support/client_model.rb
|
96
|
+
- spec/support/model_model.rb
|
105
97
|
- spec/support/schema.rb
|
98
|
+
- spec/filters_spec.rb
|
99
|
+
- spec/normatron_spec.rb
|
100
|
+
- spec/configuration_spec.rb
|
101
|
+
- spec/extensions/active_record_spec.rb
|
106
102
|
homepage: https://github.com/fernandors87/normatron
|
107
103
|
licenses: []
|
108
104
|
post_install_message:
|
@@ -129,12 +125,10 @@ specification_version: 3
|
|
129
125
|
summary: Normalize attributes for ActiveRecord objects.
|
130
126
|
test_files:
|
131
127
|
- spec/spec_helper.rb
|
132
|
-
- spec/
|
133
|
-
- spec/lib/normatron/filters_spec.rb
|
134
|
-
- spec/lib/normatron/filters/conversions_spec.rb
|
135
|
-
- spec/lib/normatron/filters/string_inflections_spec.rb
|
136
|
-
- spec/lib/normatron/configuration_spec.rb
|
137
|
-
- spec/lib/normatron/extensions/active_record_spec.rb
|
138
|
-
- spec/support/client_model.rb
|
128
|
+
- spec/support/model_model.rb
|
139
129
|
- spec/support/schema.rb
|
130
|
+
- spec/filters_spec.rb
|
131
|
+
- spec/normatron_spec.rb
|
132
|
+
- spec/configuration_spec.rb
|
133
|
+
- spec/extensions/active_record_spec.rb
|
140
134
|
has_rdoc:
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module Normatron
|
2
|
-
module Filters
|
3
|
-
module Conversions
|
4
|
-
class << self
|
5
|
-
|
6
|
-
##
|
7
|
-
# Convert a blank string on a nil object.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# StringInflections.blank("") #=> nil
|
11
|
-
# StringInflections.blank(" ") #=> nil
|
12
|
-
# StringInflections.blank("\n") #=> nil
|
13
|
-
# StringInflections.blank("1") #=> "1"
|
14
|
-
# StringInflections.blank(0) #=> 0
|
15
|
-
# @param [String] value Any character sequence
|
16
|
-
# @return [String, nil] The object itself or nil
|
17
|
-
#
|
18
|
-
# @see http://api.rubyonrails.org/classes/String.html#method-i-blank-3F ActiveSupport::CoreExt::Object#blank?
|
19
|
-
def blank(value)
|
20
|
-
if Filters.is_a_string?(value)
|
21
|
-
value.to_s.blank? ? nil : value
|
22
|
-
else
|
23
|
-
value
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,155 +0,0 @@
|
|
1
|
-
module Normatron
|
2
|
-
module Filters
|
3
|
-
##
|
4
|
-
# Contains methods that perform modifications on Strings.
|
5
|
-
# In general, modifications are only for removal and/or character replacement.
|
6
|
-
# All methods take String or ActiveSupport::Multibyte::Chars variables as main argument.
|
7
|
-
# Where character case need to be changed, all accented characters will also be affected.
|
8
|
-
# There is no type coersion, ie all filters return the same object type passed by the argument.
|
9
|
-
#
|
10
|
-
# @author Fernando Rodrigues da Silva
|
11
|
-
module StringInflections
|
12
|
-
class << self
|
13
|
-
|
14
|
-
##
|
15
|
-
# Remove all non letter characters.
|
16
|
-
#
|
17
|
-
# @example
|
18
|
-
# StringInflections.alphas("Doom 3") #=> "Doom"
|
19
|
-
# StringInflections.alphas("\n") #=> ""
|
20
|
-
# StringInflections.alphas("1") #=> ""
|
21
|
-
# StringInflections.alphas(0) #=> 0
|
22
|
-
# @param [String] value Any character sequence
|
23
|
-
# @return [String, nil] The object itself or a alpha characters sequence
|
24
|
-
#
|
25
|
-
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
26
|
-
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub String#gsub
|
27
|
-
def alphas(value)
|
28
|
-
if Filters.is_a_string?(value)
|
29
|
-
value.gsub(/[^\p{L}]/u, '')
|
30
|
-
else
|
31
|
-
value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
##
|
36
|
-
# The first character will be uppercased, all others will be lowercased.
|
37
|
-
#
|
38
|
-
# @example
|
39
|
-
# StringInflections.capitalize("mASTER OF PUPPETS") #=> "Master of puppets"
|
40
|
-
# StringInflections.capitalize(" mASTER OF PUPPETS") #=> " master of puppets"
|
41
|
-
# StringInflections.capitalize(1) #=> 1
|
42
|
-
# @param [String] value Any character sequence
|
43
|
-
# @return [String] The object itself or a capitalized string
|
44
|
-
#
|
45
|
-
# @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-capitalize ActiveSupport::Multibyte::Chars#capitalize
|
46
|
-
def capitalize(value)
|
47
|
-
evaluate(:capitalize, value)
|
48
|
-
end
|
49
|
-
|
50
|
-
##
|
51
|
-
# Replace all underscores with dashes.
|
52
|
-
#
|
53
|
-
# @example
|
54
|
-
# StringInflections.dasherize("__ shoot _ to _ thrill __") #=> "-- shoot - to - thrill --"
|
55
|
-
# StringInflections.dasherize(1) #=> 1
|
56
|
-
# @param [String] value Any character sequence
|
57
|
-
# @return [String] The object itself or a capitalized string
|
58
|
-
#
|
59
|
-
# @see http://api.rubyonrails.org/classes/String.html#method-i-dasherize ActiveSupport::CoreExt::Object#dasherize
|
60
|
-
def dasherize(value)
|
61
|
-
evaluate(:dasherize, value)
|
62
|
-
end
|
63
|
-
|
64
|
-
##
|
65
|
-
# Remove all non digit characters.
|
66
|
-
#
|
67
|
-
# @example
|
68
|
-
# StringInflections.digits("Quake 3") #=> "3"
|
69
|
-
# StringInflections.digits("\n") #=> ""
|
70
|
-
# StringInflections.digits("1") #=> "1"
|
71
|
-
# StringInflections.digits(0) #=> 0
|
72
|
-
# @param [String] value Any character sequence
|
73
|
-
# @return [String, nil] The object itself or a digit characters sequence
|
74
|
-
#
|
75
|
-
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
76
|
-
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub String#gsub
|
77
|
-
def digits(value)
|
78
|
-
if Filters.is_a_string?(value)
|
79
|
-
value.gsub(/\D/, '')
|
80
|
-
else
|
81
|
-
value
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
##
|
86
|
-
# Lowercase all characters on the sequence.
|
87
|
-
#
|
88
|
-
# @example
|
89
|
-
# StringInflections.downcase("KILL'EM ALL") #=> "kill'em all"
|
90
|
-
# StringInflections.downcase("ÊXITO") #=> "êxito"
|
91
|
-
# StringInflections.downcase("1") #=> "1"
|
92
|
-
# StringInflections.downcase(0) #=> 0
|
93
|
-
# @param [String] value Any character sequence
|
94
|
-
# @return [String, nil] The object itself or a downcased string
|
95
|
-
#
|
96
|
-
# @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-downcase ActiveSupport::Multibyte::Chars#downcase
|
97
|
-
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-downcase String#downcase
|
98
|
-
def downcase(value)
|
99
|
-
evaluate(:downcase, value)
|
100
|
-
end
|
101
|
-
|
102
|
-
def lstrip(value)
|
103
|
-
evaluate(:lstrip, value)
|
104
|
-
end
|
105
|
-
|
106
|
-
def rstrip(value)
|
107
|
-
evaluate(:rstrip, value)
|
108
|
-
end
|
109
|
-
|
110
|
-
def squeeze(value)
|
111
|
-
evaluate(:squeeze, value)
|
112
|
-
end
|
113
|
-
|
114
|
-
def squish(value)
|
115
|
-
evaluate(:squish, value)
|
116
|
-
end
|
117
|
-
|
118
|
-
def strip(value)
|
119
|
-
evaluate(:strip, value)
|
120
|
-
end
|
121
|
-
|
122
|
-
def title(value)
|
123
|
-
evaluate(:titlecase, value)
|
124
|
-
end
|
125
|
-
|
126
|
-
def upcase(value)
|
127
|
-
evaluate(:upcase, value)
|
128
|
-
end
|
129
|
-
|
130
|
-
private
|
131
|
-
|
132
|
-
def evaluate(method_name, value)
|
133
|
-
if need_type_cast?(method_name, value)
|
134
|
-
value.mb_chars.send(method_name)
|
135
|
-
elsif Filters.is_a_string?(value)
|
136
|
-
value.send(method_name)
|
137
|
-
else
|
138
|
-
value
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def need_type_cast?(method_name, value)
|
143
|
-
if value.is_a?(String)
|
144
|
-
case method_name
|
145
|
-
when :capitalize, :downcase, :titlecase, :upcase
|
146
|
-
true
|
147
|
-
end
|
148
|
-
else
|
149
|
-
false
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Normatron::Configuration do
|
4
|
-
|
5
|
-
let(:klass) { Normatron::Configuration }
|
6
|
-
let(:instance) { @instance }
|
7
|
-
|
8
|
-
before :each do
|
9
|
-
@instance = klass.new
|
10
|
-
end
|
11
|
-
|
12
|
-
describe :default_filters do
|
13
|
-
it "should be initializated" do
|
14
|
-
instance.default_filters.should eq [:squish, :blank]
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should be enclause values into an array" do
|
18
|
-
instance.default_filters = :a
|
19
|
-
instance.default_filters.should eq [:a]
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should reject repeated filters" do
|
23
|
-
instance.default_filters = :a, :a
|
24
|
-
instance.default_filters.should eq [:a]
|
25
|
-
|
26
|
-
instance.default_filters += [:a, :b]
|
27
|
-
instance.default_filters.should eq [:a, :b]
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should reject nil filters" do
|
31
|
-
instance.default_filters = nil
|
32
|
-
instance.default_filters.should eq []
|
33
|
-
|
34
|
-
instance.default_filters = :a, nil
|
35
|
-
instance.default_filters.should eq [:a]
|
36
|
-
|
37
|
-
instance.default_filters << nil
|
38
|
-
instance.default_filters.should eq [:a]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
pending "should map filter modules"
|
43
|
-
pending "should allow create inline filters"
|
44
|
-
pending "should"
|
45
|
-
end
|
@@ -1,147 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Normatron::Extensions::ActiveRecord do
|
4
|
-
let(:model_class) { Client }
|
5
|
-
|
6
|
-
before :each do
|
7
|
-
model_class.normalize_options = {}
|
8
|
-
end
|
9
|
-
|
10
|
-
describe :normalize do
|
11
|
-
pending "should remove nil normalization" do
|
12
|
-
pending "attributes"
|
13
|
-
pending "filters"
|
14
|
-
end
|
15
|
-
it "should remove nil normalization filters" do
|
16
|
-
model_class.normalize(:name, :with => [:a, :a, :b, :c, :c, nil])
|
17
|
-
model_class.normalize_options.should == {:name => [:a, :b, :c]}
|
18
|
-
|
19
|
-
model_class.normalize_options = {}
|
20
|
-
model_class.normalize(:name, :with => [nil])
|
21
|
-
model_class.normalize_options.should == {:name => [:squish, :blank]}
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should remove repeated normalization filters" do
|
25
|
-
model_class.normalize(:name, :with => [:a, :a, :b, :c, :c])
|
26
|
-
model_class.normalize_options.should == {:name => [:a, :b, :c]}
|
27
|
-
|
28
|
-
model_class.normalize(:name, :with => [:a, :d, :e])
|
29
|
-
model_class.normalize_options.should == {:name => [:a, :b, :c, :d, :e]}
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should stack repeated normalization attributes" do
|
33
|
-
model_class.normalize(:name, :name, :with => [:a, :b, :c])
|
34
|
-
model_class.normalize_options.should == {:name => [:a, :b, :c]}
|
35
|
-
|
36
|
-
model_class.normalize(:name, :with => [:d, :e])
|
37
|
-
model_class.normalize_options.should == {:name => [:a, :b, :c, :d, :e]}
|
38
|
-
end
|
39
|
-
|
40
|
-
context "with single normalization attribute" do
|
41
|
-
it "should bind to a single attribute" do
|
42
|
-
model_class.normalize(:name, with: :a)
|
43
|
-
model_class.normalize_options.should == {:name => [:a]}
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should bind to multiple attributes" do
|
47
|
-
model_class.normalize(:name, :cpf, with: [:a])
|
48
|
-
model_class.normalize_options.should == {:name => [:a], cpf: [:a]}
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context "with multiple normalization attributes" do
|
53
|
-
it "should bind to a single attribute" do
|
54
|
-
model_class.normalize(:name, with: [:a, :b])
|
55
|
-
model_class.normalize_options.should == {:name => [:a, :b]}
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should bind to multiple attributes" do
|
59
|
-
model_class.normalize(:name, :cpf, with: [:a, :b])
|
60
|
-
model_class.normalize_options.should == {:name => [:a, :b], cpf: [:a, :b]}
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
context "when :with isn't present" do
|
65
|
-
describe "default filters" do
|
66
|
-
it "should bind to a single attribute" do
|
67
|
-
model_class.normalize(:name)
|
68
|
-
model_class.normalize_options.should == {:name => [:squish, :blank]}
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should bind to multiple attributes" do
|
72
|
-
model_class.normalize(:name, :phone)
|
73
|
-
model_class.normalize_options.should == {:name => [:squish, :blank], phone: [:squish, :blank]}
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
describe :apply_normalizations do
|
81
|
-
it "should apply Conversion normalizations" do
|
82
|
-
model_class.normalize(:name, :with => :blank)
|
83
|
-
instance = model_class.new name: " "
|
84
|
-
instance.apply_normalizations
|
85
|
-
instance.name.should be_nil
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should apply StringInflections normalizations" do
|
89
|
-
model_class.normalize(:name, :with => :upcase)
|
90
|
-
instance = model_class.new name: "a"
|
91
|
-
instance.apply_normalizations
|
92
|
-
instance.name.should eq "A"
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should apply instance methods normalizations" do
|
96
|
-
class Client < ActiveRecord::Base
|
97
|
-
def my_filter(value)
|
98
|
-
value.split(//) * "-"
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
model_class.normalize(:name, :with => :my_filter)
|
103
|
-
instance = model_class.new name: "aaa"
|
104
|
-
instance.apply_normalizations
|
105
|
-
instance.name.should eq "a-a-a"
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
pending "should apply blocks normalizations"
|
110
|
-
pending "should apply modules normalizations"
|
111
|
-
pending "should apply configuration blocks normalizations"
|
112
|
-
pending "should remove nil normalization" do
|
113
|
-
pending "attributes"
|
114
|
-
pending "filters"
|
115
|
-
end
|
116
|
-
pending "should ignore nil normalization" do
|
117
|
-
pending "attribute"
|
118
|
-
pending "filter"
|
119
|
-
end
|
120
|
-
pending "should apply normalizations" do
|
121
|
-
pending "using a single filter" do
|
122
|
-
pending "for a single attribute"
|
123
|
-
pending "for all attributes"
|
124
|
-
end
|
125
|
-
pending "using multiple filters" do
|
126
|
-
pending "for a single attribute"
|
127
|
-
pending "for all attributes"
|
128
|
-
end
|
129
|
-
pending "specified into" do
|
130
|
-
pending "native filters module"
|
131
|
-
pending "custom filters module"
|
132
|
-
pending "instance model"
|
133
|
-
pending "instance block"
|
134
|
-
pending "instance lambda"
|
135
|
-
pending "configuration lambda"
|
136
|
-
end
|
137
|
-
end
|
138
|
-
pending "should allow specify" do
|
139
|
-
pending "when the normalizations will be done"
|
140
|
-
pending "conditions to normalizations be done"
|
141
|
-
pending "sequence of normalization chain"
|
142
|
-
end
|
143
|
-
pending "should raise exception" do
|
144
|
-
pending "when given attribute doesn't exist"
|
145
|
-
pending "when given filter doesn't exist"
|
146
|
-
end
|
147
|
-
end
|