Module: DatabaseConsistency::Helper::Parentheses
- Defined in:
- lib/database_consistency/helper/parentheses.rb
Overview
Parenthesis bookkeeping for SQL predicates: what a pair of parentheses encloses, and whether a fragment is still waiting for one to close.
Class Method Summary collapse
-
.depth_after(depth, char) ⇒ Object
Tracks nesting depth character by character.
-
.strip_outer(sql) ⇒ Object
Repeatedly removes one wrapping layer when the whole fragment is enclosed, e.g.
-
.unclosed?(sql) ⇒ Boolean
Returns true while the fragment opens a group it never closes.
-
.wrapping?(sql) ⇒ Boolean
Returns true only when one outer pair encloses the whole string, not when that parenthesis closes earlier inside the expression.
Class Method Details
.depth_after(depth, char) ⇒ Object
Tracks nesting depth character by character.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/database_consistency/helper/parentheses.rb', line 41 def depth_after(depth, char) case char when '(' depth + 1 when ')' depth - 1 else depth end end |
.strip_outer(sql) ⇒ Object
Repeatedly removes one wrapping layer when the whole fragment is
enclosed, e.g. ((foo)) -> foo.
12 13 14 15 16 17 18 |
# File 'lib/database_consistency/helper/parentheses.rb', line 12 def strip_outer(sql) stripped_sql = sql.strip stripped_sql = stripped_sql[1..-2].strip while wrapping?(stripped_sql) stripped_sql end |
.unclosed?(sql) ⇒ Boolean
Returns true while the fragment opens a group it never closes.
36 37 38 |
# File 'lib/database_consistency/helper/parentheses.rb', line 36 def unclosed?(sql) sql.count('(') > sql.count(')') end |
.wrapping?(sql) ⇒ Boolean
Returns true only when one outer pair encloses the whole string, not when that parenthesis closes earlier inside the expression.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/database_consistency/helper/parentheses.rb', line 22 def wrapping?(sql) return false unless sql.start_with?('(') && sql.end_with?(')') depth = 0 sql[1..-2].each_char do |char| depth = depth_after(depth, char) return false if depth.negative? end depth.zero? end |