Module: BEL::Quoting

Overview

The Quoting module implements quoting rules consistent with BEL and BEL Script. Double quotes are used to group a string together which may contain whitespace or special characters.

A value can either be an identifier or a string value. An identifier can only include the characters [0-9A-Za-z_]. A string value is necessary when at least one of [^0-9A-Za-z_] exists in the value.

Uses:

BEL: The BEL parameters must be an identifier or string value.

BEL Script: BEL parameters, document property values, and annotation values must be an identifier or string value.

Constant Summary collapse

Keywords =

Declares BEL Script keywords that cause problems with the OpenBEL Framework parser.

%w(SET DEFINE a g p r m)
KeywordMatcher =

Regular expression that matches one of Keywords.

Regexp.compile(/^(#{Keywords.join('|')})$/)
NonWordMatcher =

Regular expression that matches on any non-word character.

Regexp.compile(/[^0-9a-zA-Z_]/)
StrictQuotedMatcher =

Regular expression that matches a value surrounded by unescaped double quotes.

Regexp.compile(/\A".*?(?<!\\)"\Z/m)
LenientQuotedMatcher =

Regular expression that matches a value surrounded by double quotes that may be escaped.

Regexp.compile(/\A".*?"\Z/m)
QuoteNotEscapedMatcher =

Regular expression that matches double quotes that are not escaped.

Regexp.compile(/(?<!\\)"/m)

Instance Method Summary collapse

Instance Method Details

#always_quote(identifier) ⇒ Object

Deprecated.

Use #quote instead. Will be removed in a future release.



207
208
209
210
211
212
213
214
215
216
# File 'lib/bel/quoting.rb', line 207

def always_quote identifier
  warn <<-DOC.gsub(/^\s+/, '')
    Deprecation Warning
    -------------------
    The BEL::Quoting::always_quote method is deprecated and
    will be removed in a future relase.
    Call module method BEL::Quoting.quote instead.
  DOC
  quote(identifier)
end

#ensure_quotes(identifier) ⇒ Object

Deprecated.

Use #quote_if_needed instead. Will be removed in a future release.



181
182
183
184
185
186
187
188
189
190
# File 'lib/bel/quoting.rb', line 181

def ensure_quotes identifier
  warn <<-DOC.gsub(/^\s+/, '')
    Deprecation Warning
    -------------------
    The BEL::Quoting::ensure_quotes method is deprecated and
    will be removed in a future relase.
    Call module method BEL::Quoting.quote_if_needed instead.
  DOC
  quote_if_needed(identifier)
end

#identifier_value?(value) ⇒ Boolean

Returns whether the value represents an identifier. An identifier consists of only word characters (e.g. [0-9A-Za-z_]).

Examples:

Returns true when representing an identifier.

identifier_value?("AKT1_HUMAN")
# => true

Returns false when not representing an identifier.

identifier_value?("apoptotic process")
# => false

Returns:

  • (Boolean)

    true if value is an identifier, false if value is not an identifier



151
152
153
154
155
156
# File 'lib/bel/quoting.rb', line 151

def identifier_value?(value)
  string = value.to_s
  [NonWordMatcher, KeywordMatcher].none? { |matcher|
    matcher.match string
  }
end

#quote(value) ⇒ String

Returns value surrounded by double quotes. This method is idempotent so value will only be quoted once regardless of how may times the method is called on it.

Examples:

Quoting a BEL parameter.

quote("apoptotic process")
# => "\"apoptotic process\""

Escaping quotes within a value.

quote("vesicle fusion with \"Golgi apparatus\"")
# => "\"vesicle fusion with \\\"Golgi apparatus\\\"\""

Returns:

  • (String)

    value surrounded by double quotes



54
55
56
57
58
59
# File 'lib/bel/quoting.rb', line 54

def quote(value)
  string   = value.to_s
  unquoted = unquote(string)
  escaped  = unquoted.gsub(QuoteNotEscapedMatcher, "\\\"")
  %Q{"#{escaped}"}
end

#quote_if_needed(value) ⇒ String

Returns value with quoting applied only if necessary. A value consisting of only word character (e.g. [0-9A-Za-z_]) does not need quoting. A value consisting of at least one non-word character (e.g. [^0-9A-Za-z_]) will requiring quoting.

Examples:

Quotes added when value includes spaces.

quote_if_needed("apoptotic process")
# => "\"apoptotic process\""

Quotes added when value includes double quote.

quote_if_needed("vesicle fusion with \"Golgi apparatus\"")
# => "\"vesicle fusion with \\\"Golgi apparatus\\\"\""

No quotes necessary for identifier.

quote_if_needed("AKT1_HUMAN")
# => "AKT1_HUMAN"

Returns:

  • (String)

    original value or quoted value



97
98
99
100
101
102
103
# File 'lib/bel/quoting.rb', line 97

def quote_if_needed(value)
  if string_value?(value)
    quote(value)
  else
    value.to_s
  end
end

#quoted?(value) ⇒ Boolean

Returns whether the value is surrounded by double quotes.

Examples:

Returns true when value is quoted.

quoted?("\"vesicle fusion with \"Golgi apparatus\"")
# => true

Returns false when value is not quoted.

quoted?("apoptotic process")
# => false

Returns:

  • (Boolean)

    true if value is quoted, false if value is not quoted



117
118
119
120
# File 'lib/bel/quoting.rb', line 117

def quoted?(value)
  string = value.to_s
  (string =~ LenientQuotedMatcher) != nil
end

#quotes_required?(identifier) ⇒ Boolean

Deprecated.

Use #quoted? or #unquoted? instead. Will be removed in a future release.

Returns:

  • (Boolean)


220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/bel/quoting.rb', line 220

def quotes_required? identifier
  warn <<-DOC.gsub(/^\s+/, '')
    Deprecation Warning
    -------------------
    The BEL::Quoting::quotes_required? method is deprecated and 
    will be removed in a future relase.
    You can use BEL::Quoting.quoted? and BEL::Quoting.unquoted?
    going forward.
  DOC
  [NonWordMatcher, KeywordMatcher].any? { |m|
    m.match identifier.to_s
  }
end

#remove_quotes(identifier) ⇒ Object

Deprecated.

Use #unquote instead. Will be removed in a future release.



194
195
196
197
198
199
200
201
202
203
# File 'lib/bel/quoting.rb', line 194

def remove_quotes identifier
  warn <<-DOC.gsub(/^\s+/, '')
    Deprecation Warning
    -------------------
    The BEL::Quoting::remove_quotes method is deprecated and
    will be removed in a future relase.
    Call module method BEL::Quoting.unquote instead.
  DOC
  unquote(identifier)
end

#string_value?(value) ⇒ Boolean

Returns whether the value represents a string value. A string value consists of at least one non-word character (e.g. [^0-9A-Za-z_]).

Examples:

Returns true when representing a string value.

string_value?("apoptotic process")
# => true

Returns false when not representing a string value.

string_value?("AKT1_HUMAN")
# => false

Returns:

  • (Boolean)

    true if value is a string value, false if value is not a string value



172
173
174
175
176
177
# File 'lib/bel/quoting.rb', line 172

def string_value?(value)
  string = value.to_s
  [NonWordMatcher, KeywordMatcher].any? { |matcher|
    matcher.match string
  }
end

#unquote(value) ⇒ String

Returns value with surrounded quotes removed.

Examples:

Unquoting a BEL parameter.

unquote("\"apoptotic process\"")
# => "apoptotic process"

Escaped quotes are preserved.

unquote("\"vesicle fusion with \"Golgi apparatus\"\"")

Returns:

  • (String)

    value with surrounding double quotes removed



71
72
73
74
75
76
77
78
# File 'lib/bel/quoting.rb', line 71

def unquote(value)
  string = value.to_s
  if string =~ StrictQuotedMatcher
    string[1...-1]
  else
    string
  end
end

#unquoted?(value) ⇒ Boolean

Returns whether the value is not surrounded by double quotes.

Examples:

Returns true when value is not quoted.

unquoted?("apoptotic process")
# => true

Returns false when value is quoted.

unquoted?("\"vesicle fusion with \"Golgi apparatus\"")
# => false

Returns:

  • (Boolean)

    true if value is not quoted, false if value is quoted



134
135
136
# File 'lib/bel/quoting.rb', line 134

def unquoted?(value)
  !quoted?(value)
end