Module: QuoteSql::Quoting
- Included in:
- QuoteSql
- Defined in:
- lib/quote_sql/quoting.rb
Instance Method Summary collapse
- #escape(item) ⇒ Object
- #escape_array(ary) ⇒ Object
-
#escape_regex(regexp) ⇒ Object
quote ruby regex with a postgres regex.
Instance Method Details
#escape(item) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/quote_sql/quoting.rb', line 3 def escape(item) case item when Regexp escape_regex(item) when Array escape_array(item) else quote(item) end end |
#escape_array(ary) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/quote_sql/quoting.rb', line 14 def escape_array(ary) type = nil dive = ->(ary) do ary.flat_map do |elem| if elem.is_a? Array dive[s] elsif !elem.nil? and (type ||= elem.class.to_s) != elem.class.to_s raise TypeError, "Array elements have to be the same kind" else quote elem end end.join(',') end ary = "[#{dive[ary]}]" "ARRAY#{ary}" end |
#escape_regex(regexp) ⇒ Object
quote ruby regex with a postgres regex
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/quote_sql/quoting.rb', line 34 def escape_regex(regexp) # https://gist.github.com/glv/24bedd7d39f16a762528d7b30e366aa7 pregex = regexp.to_s.gsub(/^\(\?-?[mix]+:|\)$/, '') if pregex[/[*+?}]\+|\(\?<|&&|\\k|\\g|\\p\{/] raise RegexpError, "cant convert Regexp #{sub}" end pregex.gsub!(/\\h/, "[[:xdigit:]]") pregex.gsub!(/\\H/, "[^[:xdigit:]]") pregex.gsub!(/\?[^>]>/, '') pregex.gsub!(/\{,/, "{0,") pregex.gsub!(/\\z/, "\\Z") quote(pregex) end |