Module: BEL::Quoting
- Included in:
- BELRDF::Reader::NanopubYielder, Completion::MatchNamespaceValueRule, Language::Annotation, Language::DocumentProperty, Language::StatementGroup, BELRDF::Reader::NanopubYielder
- Defined in:
- lib/bel/quoting.rb
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
-
#always_quote(identifier) ⇒ Object
deprecated
Deprecated.
Use #quote instead. Will be removed in a future release.
-
#ensure_quotes(identifier) ⇒ Object
deprecated
Deprecated.
Use #quote_if_needed instead. Will be removed in a future release.
-
#identifier_value?(value) ⇒ Boolean
Returns whether the
valuerepresents an identifier. -
#quote(value) ⇒ String
Returns
valuesurrounded by double quotes. -
#quote_if_needed(value) ⇒ String
Returns
valuewith quoting applied only if necessary. -
#quoted?(value) ⇒ Boolean
Returns whether the
valueis surrounded by double quotes. -
#quotes_required?(identifier) ⇒ Boolean
deprecated
Deprecated.
Use #quoted? or #unquoted? instead. Will be removed in a future release.
-
#remove_quotes(identifier) ⇒ Object
deprecated
Deprecated.
Use #unquote instead. Will be removed in a future release.
-
#string_value?(value) ⇒ Boolean
Returns whether the
valuerepresents a string value. -
#unquote(value) ⇒ String
Returns
valuewith surrounded quotes removed. -
#unquoted?(value) ⇒ Boolean
Returns whether the
valueis not surrounded by double quotes.
Instance Method Details
#always_quote(identifier) ⇒ Object
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 " Deprecation Warning\n -------------------\n The BEL::Quoting::always_quote method is deprecated and\n will be removed in a future relase.\n Call module method BEL::Quoting.quote instead.\n DOC\n quote(identifier)\nend\n".gsub(/^\s+/, '') |
#ensure_quotes(identifier) ⇒ Object
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 " Deprecation Warning\n -------------------\n The BEL::Quoting::ensure_quotes method is deprecated and\n will be removed in a future relase.\n Call module method BEL::Quoting.quote_if_needed instead.\n DOC\n quote_if_needed(identifier)\nend\n".gsub(/^\s+/, '') |
#identifier_value?(value) ⇒ Boolean
Returns whether the value represents an identifier. An identifier consists of only word characters (e.g. [0-9A-Za-z_]).
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.
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.
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.
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
Use #quoted? or #unquoted? instead. Will be removed in a future release.
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 " Deprecation Warning\n -------------------\n The BEL::Quoting::quotes_required? method is deprecated and \n will be removed in a future relase.\n You can use BEL::Quoting.quoted? and BEL::Quoting.unquoted?\n going forward.\n DOC\n [NonWordMatcher, KeywordMatcher].any? { |m|\n m.match identifier.to_s\n }\nend\n".gsub(/^\s+/, '') |
#remove_quotes(identifier) ⇒ Object
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 " Deprecation Warning\n -------------------\n The BEL::Quoting::remove_quotes method is deprecated and\n will be removed in a future relase.\n Call module method BEL::Quoting.unquote instead.\n DOC\n unquote(identifier)\nend\n".gsub(/^\s+/, '') |
#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_]).
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.
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.
134 135 136 |
# File 'lib/bel/quoting.rb', line 134 def unquoted?(value) !quoted?(value) end |