lib/dolzenko/safe_interpolate.rb in dolzenko-0.0.23 vs lib/dolzenko/safe_interpolate.rb in dolzenko-0.0.24
- old
+ new
@@ -1,17 +1,24 @@
require "active_support/all"
require "active_record"
require "cgi"
+# http://dolzhenko.org/blog/2010/07/safe-string-interpolation-in-ruby/
module SafeInterpolate
def generic_interpolate(string_block, interpolator)
+ raise ArgumentError, "block returning string to interpolate must be provided" unless string_block
string_with_interpolations = string_block.call
string_with_interpolations.gsub(/\#\{([^}]*)\}/) do
result = eval($1, string_block.binding)
interpolator[result]
end
end
+ # Examples
+ #
+ # include SafeInterpolate
+ # ...
+ # sql_interpolate { 'name = #{ name }' } # => "name = 'Bob'"
def sql_interpolate(&string_block)
generic_interpolate(string_block, ActiveRecord::Base.connection.method(:quote))
end
def html_interpolate(&string_block)
\ No newline at end of file