Skip to content

Commit

Permalink
Made a nil_strip and nil_squish_strip differentiation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Feb 7, 2014
1 parent cc68ea7 commit 6babcd4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/activerecord_utilities.rb
@@ -1,5 +1,5 @@
module ActiverecordUtilities
# this is for shared utilies, like string manipulation methods.
# this is for shared utilies, like string manipulation methods.

extend ActiveSupport::Concern

Expand All @@ -18,7 +18,7 @@ def nil_trim_attributes(*attributes) # this assigns the attributes to be trimmed
end

protected
# any def below this is an instance method
# any def below this is an instance method
def trim_attributes
if !self.attributes_to_trim.nil?
self.attributes_to_trim.each do |a|
Expand Down
13 changes: 12 additions & 1 deletion lib/utilities/strings.rb
@@ -1,10 +1,21 @@
module Utilities::Strings

# Strips space, leaves internal widespace as is
def self.nil_strip(string) # string should have content or be empty
if !string.nil?
string.strip!
string = nil if string == ''
end
string
end

# Strips space both condenses space
def self.nil_squish_strip(string)
if !string.nil?
string.squish!
string = nil if string == ''
end
string
end
end

end
5 changes: 3 additions & 2 deletions spec/lib/activerecord_utilities_spec.rb
Expand Up @@ -12,6 +12,7 @@
expect { TestStringManipulations.nil_trim_attributes(:col1, :foo) }.to raise_error
end
end

context 'instance methods' do
let(:teststr) {
TestStringManipulations.nil_trim_attributes(:col1, :col3)
Expand All @@ -30,7 +31,7 @@
end
end

#include dummy class to test the activerecord_utilities
# Dummy class to test activerecord_utilities
class TestStringManipulations

attr_accessor :col1, :col2, :col3
Expand All @@ -48,4 +49,4 @@ def valid?
trim_attributes
end

end
end

0 comments on commit 6babcd4

Please sign in to comment.