Module: StripAttributes

Defined in:
lib/strip_attributes/version.rb,
lib/strip_attributes/matchers.rb,
lib/strip_attributes/shoulda/macros.rb,
lib/strip_attributes.rb

Defined Under Namespace

Modules: Matchers, Shoulda

Constant Summary collapse

VERSION =
"1.5.1"
VALID_OPTIONS =
[:only, :except, :allow_empty, :collapse_spaces, :regex]
MULTIBYTE_SUPPORTED =
"\u0020" == " "

Class Method Summary collapse

Class Method Details

.narrow(attributes, options = {}) ⇒ Object

Necessary because Rails has removed the narrowing of attributes using :only and :except on Base#attributes



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/strip_attributes.rb', line 27

def self.narrow(attributes, options = {})
  if except = options && options[:except]
    except = Array(except).collect { |attribute| attribute.to_s }
    attributes.except(*except)
  elsif only = options && options[:only]
    only = Array(only).collect { |attribute| attribute.to_s }
    attributes.slice(*only)
  else
    attributes
  end
end

.strip(record, options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/strip_attributes.rb', line 39

def self.strip(record, options)
    attributes = self.narrow(record.attributes, options)

    if options
      allow_empty     = options[:allow_empty]
      collapse_spaces = options[:collapse_spaces]
      regex           = options[:regex]
    end

    attributes.each do |attr, value|
      original_value = value

      if value.respond_to?(:strip)
        value = (value.blank? && !allow_empty) ? nil : value.strip
      end

      if regex && value.respond_to?(:gsub!)
        value.gsub!(regex, '')
      end

      if MULTIBYTE_SUPPORTED
        # Remove leading and trailing Unicode invisible and whitespace characters.
        # The POSIX character class [:space:] corresponds to the Unicode class Z
        # ("separator"). We also include the following characters from Unicode class
        # C ("control"), which are spaces or invisible characters that make no
        # sense at the start or end of a string:
        #   U+180E MONGOLIAN VOWEL SEPARATOR
        #   U+200B ZERO WIDTH SPACE
        #   U+200C ZERO WIDTH NON-JOINER
        #   U+200D ZERO WIDTH JOINER
        #   U+2060 WORD JOINER
        #   U+FEFF ZERO WIDTH NO-BREAK SPACE
        if value.respond_to?(:gsub!)
          value.gsub!(/\A[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+|[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+\z/, '')
        end
      end

      if collapse_spaces && value.respond_to?(:squeeze!)
        value.squeeze!(' ')
      end

      record[attr] = value if original_value != value
    end
end

.validate_options(options) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/strip_attributes.rb', line 84

def self.validate_options(options)
  if keys = options && options.keys
    unless (keys - VALID_OPTIONS).empty?
      raise ArgumentError, "Options does not specify #{VALID_OPTIONS} (#{options.keys.inspect})"
    end
  end
end