Class: RailsDbAdmin::ErbStringParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_admin/erp_string_parser.rb

Constant Summary collapse

ERB_REGEX =
/\<\%\=\s*(\$|@{1,2})?[A-Za-z][A-Za-z0-9_]*\s*\%\>/

Class Method Summary collapse

Class Method Details

.render(str, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rails_db_admin/erp_string_parser.rb', line 6

def render(str, options = {})
  locals = options[:locals] || {}

  # convert all variables to underscore as
  # ERB parser has issues with capitalized variables
  _str = str.gsub(ERB_REGEX) do |w|
    w.delete(' ').underscore
  end
  # convert all to locals to underscore
  _locals = {}
  locals.each do |k, v|
    _locals[k.to_s.delete(' ').underscore] = v
  end

  query_params = RailsDbAdmin::QueryParams.new(_locals)
  ERB.new(_str).result(query_params.get_binding)
end