Module: ArelExtensions::Visitors

Defined in:
lib/arel_extensions/visitors/mssql.rb,
lib/arel_extensions/visitors/mysql.rb,
lib/arel_extensions/visitors/ibm_db.rb,
lib/arel_extensions/visitors/oracle.rb,
lib/arel_extensions/visitors/sqlite.rb,
lib/arel_extensions/visitors/to_sql.rb,
lib/arel_extensions/visitors/oracle12.rb,
lib/arel_extensions/visitors/postgresql.rb,
lib/arel_extensions/visitors/convert_format.rb

Defined Under Namespace

Modules: MSSQL

Class Method Summary collapse

Class Method Details

.strftime_to_format(format, mapping) ⇒ Object

Convert date format in strftime syntax to whatever the RDBMs wants, based on the table of conversion mapping.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/arel_extensions/visitors/convert_format.rb', line 5

def self.strftime_to_format format, mapping
  @mapping_regexps ||= {}
  @mapping_regexps[mapping] ||=
    Regexp.new(
      mapping
        .keys
        .map{|k| Regexp.escape(k)}
        .join('|')
    )

  regexp = @mapping_regexps[mapping]
  s = StringScanner.new format
  res = StringIO.new
  while !s.eos?
    res <<
      case
      when s.scan(regexp)
        if v = mapping[s.matched]
          v
        else
          # Should never happen.
          s.matched
        end
      when s.scan(/[^%]+/)
        s.matched
      when s.scan(/./)
        s.matched
      end
  end
  res.string
end