diff --git a/lib/attribute_normalizer.rb b/lib/attribute_normalizer.rb index 4bfbe0c..fc3369f 100644 --- a/lib/attribute_normalizer.rb +++ b/lib/attribute_normalizer.rb @@ -1,3 +1,4 @@ +require 'attribute_normalizer/normalizers/base_normalizer' require 'attribute_normalizer/normalizers/blank_normalizer' require 'attribute_normalizer/normalizers/phone_normalizer' require 'attribute_normalizer/normalizers/strip_normalizer' diff --git a/lib/attribute_normalizer/normalizers/base_normalizer.rb b/lib/attribute_normalizer/normalizers/base_normalizer.rb new file mode 100644 index 0000000..2218aae --- /dev/null +++ b/lib/attribute_normalizer/normalizers/base_normalizer.rb @@ -0,0 +1,16 @@ +module AttributeNormalizer + module Normalizers + module BaseNormalizer + def normalize(value, options = {}) + case value + when String + perform_normalization(value) + when Array + value.map{|v| perform_normalization(v)} + else + value + end + end + end + end +end diff --git a/lib/attribute_normalizer/normalizers/blank_normalizer.rb b/lib/attribute_normalizer/normalizers/blank_normalizer.rb index 8efc668..91a1a57 100644 --- a/lib/attribute_normalizer/normalizers/blank_normalizer.rb +++ b/lib/attribute_normalizer/normalizers/blank_normalizer.rb @@ -1,9 +1,16 @@ module AttributeNormalizer module Normalizers module BlankNormalizer + extend BaseNormalizer def self.normalize(value, options = {}) - value.nil? || (value.is_a?(String) && value !~ /\S/) ? nil : value + val = super + val.reject!{|v| v.nil?} if val.is_a? Array + val + end + + def self.perform_normalization(value) + value !~ /\S/ ? nil : value end end end -end \ No newline at end of file +end diff --git a/lib/attribute_normalizer/normalizers/phone_normalizer.rb b/lib/attribute_normalizer/normalizers/phone_normalizer.rb index 5655efd..e7ed5e9 100644 --- a/lib/attribute_normalizer/normalizers/phone_normalizer.rb +++ b/lib/attribute_normalizer/normalizers/phone_normalizer.rb @@ -1,10 +1,12 @@ module AttributeNormalizer module Normalizers module PhoneNormalizer - def self.normalize(value, options = {}) - value = value.is_a?(String) ? value.gsub(/[^0-9]+/, '') : value - value.is_a?(String) && value.empty? ? nil : value + extend BaseNormalizer + + def self.perform_normalization(value) + value = value.gsub(/[^0-9]+/, '') + value.empty? ? nil : value end end end -end \ No newline at end of file +end diff --git a/lib/attribute_normalizer/normalizers/squish_normalizer.rb b/lib/attribute_normalizer/normalizers/squish_normalizer.rb index 8cafbc0..77a2876 100644 --- a/lib/attribute_normalizer/normalizers/squish_normalizer.rb +++ b/lib/attribute_normalizer/normalizers/squish_normalizer.rb @@ -1,9 +1,11 @@ module AttributeNormalizer module Normalizers module SquishNormalizer - def self.normalize(value, options = {}) - value.is_a?(String) ? value.strip.gsub(/\s+/, ' ') : value + extend BaseNormalizer + + def self.perform_normalization(value) + value.strip.gsub(/\s+/, ' ') end end end -end \ No newline at end of file +end diff --git a/lib/attribute_normalizer/normalizers/strip_normalizer.rb b/lib/attribute_normalizer/normalizers/strip_normalizer.rb index 8c39378..321b9fa 100644 --- a/lib/attribute_normalizer/normalizers/strip_normalizer.rb +++ b/lib/attribute_normalizer/normalizers/strip_normalizer.rb @@ -1,9 +1,11 @@ module AttributeNormalizer module Normalizers module StripNormalizer - def self.normalize(value, options = {}) - value.is_a?(String) ? value.strip : value + extend BaseNormalizer + + def self.perform_normalization(value) + value.strip end end end -end \ No newline at end of file +end diff --git a/lib/attribute_normalizer/normalizers/whitespace_normalizer.rb b/lib/attribute_normalizer/normalizers/whitespace_normalizer.rb index 49547e6..5b6865b 100644 --- a/lib/attribute_normalizer/normalizers/whitespace_normalizer.rb +++ b/lib/attribute_normalizer/normalizers/whitespace_normalizer.rb @@ -1,8 +1,10 @@ module AttributeNormalizer module Normalizers module WhitespaceNormalizer - def self.normalize(value, options = {}) - value.is_a?(String) ? value.gsub(/[^\S\n]+/, ' ').gsub(/\s?\n\s?/, "\n").strip : value + extend BaseNormalizer + + def self.perform_normalization(value) + value.gsub(/[^\S\n]+/, ' ').gsub(/\s?\n\s?/, "\n").strip end end end diff --git a/spec/article_spec.rb b/spec/article_spec.rb index b756558..4befb82 100644 --- a/spec/article_spec.rb +++ b/spec/article_spec.rb @@ -1,6 +1,7 @@ require File.dirname(File.expand_path(__FILE__)) + '/test_helper' describe Article do + it { should normalize_attribute(:title).from([' Social Life at the Edge of Chaos ']).to(['Social Life at the Edge of Chaos']) } it { should normalize_attribute(:title).from(' Social Life at the Edge of Chaos ').to('Social Life at the Edge of Chaos') } it { should normalize_attribute(:slug) } it { should normalize_attribute(:slug).from(' Social Life at the Edge of Chaos ').to('social-life-at-the-edge-of-chaos') } diff --git a/spec/author_spec.rb b/spec/author_spec.rb index d06b6f7..ed2ffc2 100644 --- a/spec/author_spec.rb +++ b/spec/author_spec.rb @@ -11,26 +11,32 @@ # :strip normalizer it { should normalize_attribute(:first_name).from(' this ').to('this') } it { should normalize_attribute(:first_name).from(' ').to('') } + it { should normalize_attribute(:first_name).from([' ', ' ']).to(['', '']) } # :squish normalizer it { should normalize_attribute(:nickname).from(' this nickname ').to('this nickname') } + it { should normalize_attribute(:nickname).from([' this nickname ']).to(['this nickname']) } # :blank normalizer it { should normalize_attribute(:last_name).from('').to(nil) } it { should normalize_attribute(:last_name).from(' ').to(nil) } it { should normalize_attribute(:last_name).from(' this ').to(' this ') } + it { should normalize_attribute(:last_name).from([' ']).to([]) } + it { should normalize_attribute(:last_name).from([' ', 'two']).to(['two']) } # :whitespace normalizer it { should normalize_attribute(:biography).from(" this line\nbreak ").to("this line\nbreak") } it { should normalize_attribute(:biography).from("\tthis\tline\nbreak ").to("this line\nbreak") } it { should normalize_attribute(:biography).from(" \tthis \tline \nbreak \t \nthis").to("this line\nbreak\nthis") } it { should normalize_attribute(:biography).from(' ').to('') } + it { should normalize_attribute(:biography).from([' ']).to(['']) } end context 'on default attribute with the default normalizer changed' do it { should normalize_attribute(:phone_number).from('no-numbers-here').to(nil) } it { should normalize_attribute(:phone_number).from('1.877.987.9875').to('18779879875') } it { should normalize_attribute(:phone_number).from('+ 1 (877) 987-9875').to('18779879875') } + it { should normalize_attribute(:phone_number).from(['+ 1 (877) 987-9875']).to(['18779879875']) } end end