module StrSanitizer::Quotes

This module sanitizes the given string by adding backslash () before quote It also can check if a string has any quote or not.

Author: Jakaria (mailto: jakariablaine120@gmail.com) Copyright: Copyright © 2017 Jakaria

Public Instance Methods

both_quotes(str) click to toggle source

Find any single quote (') and double quote (“) in a string and add backslash () before the single quote and double quote (”).

Params:

str

A string which has single quote and double quote that need to be escaped.

Returns:

String

The string which was passed to the parameter with escaped single quote and

double quote.

# File lib/str_sanitizer/quotes.rb, line 43
def both_quotes(str)
  single_quote(double_quote(str))
end
double_quote(str) click to toggle source

Find any double quote (“) in a string and add backslash () before the double quote.

Params:

str

A string which has double quote that needs to be escaped.

Returns:

String

The string which was passed to the parameter with escaped double quotes.

# File lib/str_sanitizer/quotes.rb, line 19
def double_quote(str)
  str.gsub(/"/, /\\"/.source)
end
has_any_quote?(str, pos = 0) click to toggle source

Search through the given string to see if it has any quote.

Params:

str

A string which needs to be checked for any quote.

pos

From where it should start searching. Default is 0.

Returns:

+Boolean|Nil+

Returns true if it has any quote, nil for no quote

# File lib/str_sanitizer/quotes.rb, line 91
def has_any_quote?(str, pos = 0)
  true if has_single_quote?(str, pos) || has_double_quote?(str, pos)
end
has_both_quotes?(str, pos = 0) click to toggle source

Search through the given string to see if it has both, single (') and double quote (“).

Params:

str

A string which needs to be checked for both quotes.

pos

From where it should start searching. Default is 0.

Returns:

+Boolean|Nil+

Returns true if it has both quotes, nil for no quote.

# File lib/str_sanitizer/quotes.rb, line 79
def has_both_quotes?(str, pos = 0)
  true if has_single_quote?(str, pos) && has_double_quote?(str, pos)
end
has_double_quote?(str, pos = 0) click to toggle source

Search through the given string to see if it has any double quote (“).

Params:

str

A string which needs to be checked for double quote.

pos

From where it should start searching. Default is 0.

Returns:

+Boolean|Nil+

Returns true if it has any double quote, nil for no double quote

# File lib/str_sanitizer/quotes.rb, line 67
def has_double_quote?(str, pos = 0)
  true if str.match('"', pos)
end
has_single_quote?(str, pos = 0) click to toggle source

Search through the given string to see if it has any single quote (').

Params:

str

A string which needs to be checked for single quote.

pos

From where it should start searching. Default is 0.

Returns:

+Boolean|Nil+

Returns true if it has any single quote, nil for no single quote

# File lib/str_sanitizer/quotes.rb, line 55
def has_single_quote?(str, pos = 0)
  true if str.match("'", pos)
end
single_quote(str) click to toggle source

Find any single quote (') in a string and add backslash () before the single quote.

Params:

str

A string which has single quote that needs to be escaped.

Returns:

String

The string which was passed to the parameter with escaped single quotes.

# File lib/str_sanitizer/quotes.rb, line 30
def single_quote(str)
  str.gsub(/'/, /\\'/.source)
end