Sha256: 75efb7fd109ef1b1c7728b149ead5baa3f2fe194d5fd8afa4334d0b38725a01d

Contents?: true

Size: 619 Bytes

Versions: 2

Compression:

Stored size: 619 Bytes

Contents

require 'cgi'
require 'uri'

class String

  # String#escape is an extensible escaping mechanism for string.  currently
  # it suports
  #   '<div>foo bar</div>'.esc(:html)
  #   'foo bar'.esc(:uri)
  #   'foo bar'.esc(:cgi)
  #   "foo 'bar'".esc(:sql)

  def escape which = :html
    case which
    when :html
      ::CGI.escapeHTML(self)
    when :cgi
      ::CGI.escape(self)
    when :uri
      ::URI.escape(self)
    when :sql  # TODO - leverage DBI or something
      gsub(%r/'/, "''")
    else
      raise ArgumentError, "do not know how to escape '#{ which }'"
    end
  end

  alias_method 'esc', 'escape'
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ramaze-0.3.9 lib/ramaze/snippets/string/esc.rb
ramaze-0.3.9.1 lib/ramaze/snippets/string/esc.rb