Class: Gummi::Fields::SanitizedString
- Inherits:
-
Virtus::Attribute
- Object
- Virtus::Attribute
- Gummi::Fields::SanitizedString
- Defined in:
- lib/gummi/fields/sanitized_string.rb
Instance Method Summary collapse
Instance Method Details
#coerce(value) ⇒ Object
5 6 7 8 |
# File 'lib/gummi/fields/sanitized_string.rb', line 5 def coerce(value) return nil if value.blank? sanitize_string_for_query(value.to_s) end |
#mapping ⇒ Object
10 11 12 |
# File 'lib/gummi/fields/sanitized_string.rb', line 10 def mapping { type: 'string' } end |
#sanitize_string_for_query(str) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gummi/fields/sanitized_string.rb', line 14 def sanitize_string_for_query(str) # Escape special characters escaped_characters = Regexp.escape('\/\\+-&|!(){}[]^~*?:') str = str.gsub(/([#{escaped_characters}])/) do |match| '\\'+match end # Escape odd quotes quote_count = str.count '"' str = str.gsub(/(.*)"(.*)/, '\1\"\3') if quote_count % 2 == 1 str end |