Module: Binxtils::InputNormalizer
- Extended by:
- Functionable
- Defined in:
- lib/binxtils/input_normalizer.rb
Instance Method Summary collapse
- #boolean(param = nil) ⇒ Object
- #present_or_false?(val) ⇒ Boolean
- #regex_escape(val) ⇒ Object
- #sanitize(str = nil) ⇒ Object
- #string(val) ⇒ Object
Instance Method Details
#boolean(param = nil) ⇒ Object
7 8 9 10 11 |
# File 'lib/binxtils/input_normalizer.rb', line 7 def boolean(param = nil) return false if param.blank? ActiveRecord::Type::Boolean.new.cast(param.to_s.strip) end |
#present_or_false?(val) ⇒ Boolean
19 20 21 |
# File 'lib/binxtils/input_normalizer.rb', line 19 def present_or_false?(val) val.to_s.present? end |
#regex_escape(val) ⇒ Object
30 31 32 33 |
# File 'lib/binxtils/input_normalizer.rb', line 30 def regex_escape(val) # Lazy hack, good enough for current purposes. Improve if required! string(val)&.gsub(/\W/, ".") end |
#sanitize(str = nil) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/binxtils/input_normalizer.rb', line 23 def sanitize(str = nil) Rails::Html::Sanitizer.full_sanitizer.new.sanitize(str.to_s, encode_special_chars: true) .strip .gsub("&", "&") # ampersands are commonly used - keep them normal .gsub(/\s+/, " ") # remove extra whitespace end |
#string(val) ⇒ Object
13 14 15 16 17 |
# File 'lib/binxtils/input_normalizer.rb', line 13 def string(val) return nil if val.blank? val.strip.gsub(/\s+/, " ") end |