Class: OccamsRecord::BindsConverter::Named

Inherits:
Object
  • Object
show all
Defined in:
lib/occams-record/binds_converter/named.rb

Overview

Converts Rails-style named binds (:foo) into native Ruby format (%foo).

Instance Method Summary collapse

Constructor Details

#initialize(sql) ⇒ Named

Returns a new instance of Named.



7
8
9
# File 'lib/occams-record/binds_converter/named.rb', line 7

def initialize(sql)
  @sql = sql
end

Instance Method Details

#to_sObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/occams-record/binds_converter/named.rb', line 11

def to_s
  @sql.gsub(/([:\\]?):([a-zA-Z]\w*)/) do |match|
    if $1 == ":".freeze # skip PostgreSQL casts
      match # return the whole match
    elsif $1 == "\\".freeze # escaped literal colon
      match[1..-1] # return match with escaping backslash char removed
    else
      "%{#{$2}}"
    end
  end
end