Module: StrSanitizer::Quotes

Included in:
StrSanitizer
Defined in:
lib/str_sanitizer/quotes.rb

Overview

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: [email protected]) Copyright: Copyright © 2017 Jakaria

Instance Method Summary collapse

Instance Method Details

#both_quotes(str) ⇒ Object

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.



43
44
45
# File 'lib/str_sanitizer/quotes.rb', line 43

def both_quotes(str)
  single_quote(double_quote(str))
end

#double_quote(str) ⇒ Object

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.



19
20
21
# File 'lib/str_sanitizer/quotes.rb', line 19

def double_quote(str)
  str.gsub(/"/, /\\"/.source)
end

#has_any_quote?(str, pos = 0) ⇒ Boolean

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

Returns:

  • (Boolean)


91
92
93
# 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) ⇒ Boolean

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.

Returns:

  • (Boolean)


79
80
81
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


67
68
69
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/str_sanitizer/quotes.rb', line 55

def has_single_quote?(str, pos = 0)
  true if str.match("'", pos)
end

#single_quote(str) ⇒ Object

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.



30
31
32
# File 'lib/str_sanitizer/quotes.rb', line 30

def single_quote(str)
  str.gsub(/'/, /\\'/.source)
end