Sha256: d9742a82a723fdf5dffd8992e25bceb8a277e974687d7d4cb82b0eddfb230e24

Contents?: true

Size: 730 Bytes

Versions: 30

Compression:

Stored size: 730 Bytes

Contents

module Gummi
  module DbLayer
    module Fields
      class SanitizedString < Virtus::Attribute

        def coerce(value)
          return nil if value.blank?
          sanitize_string_for_query(value.to_s)
        end

        def mapping
          { type: 'string' }
        end

        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
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
gummi-1.0.10 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.9 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.8 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.7 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.6 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.5 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.4 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.16 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.3 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.2 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-1.0.0 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.14 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.13 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.12 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.11 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.10 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.9 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.8 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.7 lib/gummi/db_layer/fields/sanitized_string.rb
gummi-0.3.6 lib/gummi/db_layer/fields/sanitized_string.rb