Sha256: 6e374c8a4d0d0d09bf1a7fbadbe270b6807e5a8f79101d1935a579390c5a4310

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# Enumerable extensions.
module Enumerable
  def send_each(sym, *args)
    each {|i| i.send(sym, *args)}
  end
end

# Array extensions
class Array
  # Concatenates an array of strings into an SQL string. ANSI SQL and C-style
  # comments are removed, as well as excessive white-space.
  def to_sql
    map {|l| (l =~ /^(.*)--/ ? $1 : l).chomp}.join(' '). \
      gsub(/\/\*.*\*\//, '').gsub(/\s+/, ' ').strip
  end
end

module Sequel
  class ExpressionString < ::String
  end
end

# String extensions
class String
  # Converts a string into an SQL string by removing comments.
  # See also Array#to_sql.
  def to_sql
    split($/).to_sql
  end
  
  # Splits a string into separate SQL statements, removing comments
  # and excessive white-space.
  def split_sql
    to_sql.split(';').map {|s| s.strip}
  end
  
  # Convert a string into an Expression String
  def expr
    Sequel::ExpressionString.new(self)
  end
end

# Symbol extensions
class Symbol
  def DESC
    "#{to_field_name} DESC"
  end
  
  def AS(target)
    "#{to_field_name} AS #{target}"
  end
  
  AS_REGEXP = /(.*)___(.*)/.freeze
  DOUBLE_UNDERSCORE = '__'.freeze
  PERIOD = '.'.freeze
  
  def to_field_name
    s = to_s
    if s =~ AS_REGEXP
      s = "#{$1} AS #{$2}"
    end
    s.split(DOUBLE_UNDERSCORE).join(PERIOD)
  end
  
  def ALL
    "#{to_s}.*"
  end

  def MIN; "min(#{to_field_name})"; end
  def MAX; "max(#{to_field_name})"; end
  def SUM; "sum(#{to_field_name})"; end
  def AVG; "avg(#{to_field_name})"; end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sequel-0.1.8 lib/sequel/core_ext.rb
sequel-0.1.7 lib/sequel/core_ext.rb