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 =
"2.0.1".freeze
- VALID_OPTIONS =
[:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex, :if, :unless].freeze
- MULTIBYTE_WHITE =
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
"\u180E\u200B\u200C\u200D\u2060\uFEFF".freeze
- MULTIBYTE_SPACE =
/[[:space:]#{MULTIBYTE_WHITE}]/.freeze
- MULTIBYTE_SPACE_AT_ENDS =
/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/.freeze
- MULTIBYTE_BLANK =
/[[:blank:]#{MULTIBYTE_WHITE}]/.freeze
- MULTIBYTE_BLANK_REPEATED =
/#{MULTIBYTE_BLANK}+/.freeze
- MULTIBYTE_SUPPORTED =
"\u0020" == " "
- NEWLINES =
/[\r\n]+/.freeze
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
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/strip_attributes.rb', line 88
def self.narrow(attributes, options = {})
if options[:except]
except = Array(options[:except]).map(&:to_s)
attributes.except(*except)
elsif options[:only]
only = Array(options[:only]).map(&:to_s)
attributes.slice(*only)
else
attributes
end
end
|
.strip(record_or_string, options = {}) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/strip_attributes.rb', line 36
def self.strip(record_or_string, options = {})
if record_or_string.respond_to?(:attributes)
strip_record(record_or_string, options)
else
strip_string(record_or_string, options)
end
end
|
.strip_record(record, options = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/strip_attributes.rb', line 44
def self.strip_record(record, options = {})
attributes = narrow(record.attributes, options)
attributes.each do |attr, value|
original_value = value
value = strip_string(value, options)
record[attr] = value if original_value != value
end
record
end
|
.strip_string(value, options = {}) ⇒ Object
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
83
84
|
# File 'lib/strip_attributes.rb', line 56
def self.strip_string(value, options = {})
return value unless value.is_a?(String)
allow_empty = options[:allow_empty]
collapse_spaces = options[:collapse_spaces]
replace_newlines = options[:replace_newlines]
regex = options[:regex]
value = value.dup
value.gsub!(regex, "") if regex
if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_SPACE)
value.gsub!(MULTIBYTE_SPACE_AT_ENDS, "")
else
value.strip!
end
value.gsub!(NEWLINES, " ") if replace_newlines
if collapse_spaces
if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_BLANK)
value.gsub!(MULTIBYTE_BLANK_REPEATED, " ")
else
value.squeeze!(" ")
end
end
(value.blank? && !allow_empty) ? nil : value
end
|
.validate_options(options) ⇒ Object
100
101
102
103
|
# File 'lib/strip_attributes.rb', line 100
def self.validate_options(options)
return if (options.keys - VALID_OPTIONS).empty?
raise ArgumentError, "Options does not specify #{VALID_OPTIONS} (#{options.keys.inspect})"
end
|