Skip to content

Support Arrays as well as Strings. #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/attribute_normalizer.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
16 changes: 16 additions & 0 deletions lib/attribute_normalizer/normalizers/base_normalizer.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions lib/attribute_normalizer/normalizers/blank_normalizer.rb
Original file line number Diff line number Diff line change
@@ -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
end
10 changes: 6 additions & 4 deletions lib/attribute_normalizer/normalizers/phone_normalizer.rb
Original file line number Diff line number Diff line change
@@ -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
end
8 changes: 5 additions & 3 deletions lib/attribute_normalizer/normalizers/squish_normalizer.rb
Original file line number Diff line number Diff line change
@@ -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
end
8 changes: 5 additions & 3 deletions lib/attribute_normalizer/normalizers/strip_normalizer.rb
Original file line number Diff line number Diff line change
@@ -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
end
6 changes: 4 additions & 2 deletions lib/attribute_normalizer/normalizers/whitespace_normalizer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/article_spec.rb
Original file line number Diff line number Diff line change
@@ -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') }
Expand Down
6 changes: 6 additions & 0 deletions spec/author_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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