12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/sql_beautifier/sort_expression.rb', line 12
def self.parse(text)
stripped = text.strip
direction_match = stripped.match(DIRECTION_PATTERN)
if direction_match
expression = stripped[0...direction_match.begin(0)].strip
direction = direction_match[1].downcase
nulls = direction_match[2]&.downcase&.squeeze(" ")
return new(expression: expression, direction: direction, nulls: nulls)
end
nulls_match = stripped.match(NULLS_ONLY_PATTERN)
if nulls_match
expression = stripped[0...nulls_match.begin(0)].strip
return new(expression: expression, nulls: nulls_match[1].downcase.squeeze(" "))
end
new(expression: stripped)
end
|