Class: ERBLint::Utils::RubyToERB

Inherits:
Object
  • Object
show all
Defined in:
lib/erb_lint/utils/ruby_to_erb.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.escape_quote(str) ⇒ Object



46
47
48
# File 'lib/erb_lint/utils/ruby_to_erb.rb', line 46

def escape_quote(str)
  str.gsub('"', """)
end

.html_options_to_tag_attributes(hash_node) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/erb_lint/utils/ruby_to_erb.rb', line 9

def html_options_to_tag_attributes(hash_node)
  hash_node.children.map do |pair_node|
    key_node, value_node = *pair_node
    key = ruby_to_erb(key_node, "=") { |s| s.tr("_", "-") }
    value = ruby_to_erb(value_node, "=") { |s| escape_quote(s) }
    [key, "\"#{value}\""].join("=")
  end.join(" ")
end

.ruby_to_erb(node, indicator = nil, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/erb_lint/utils/ruby_to_erb.rb', line 18

def ruby_to_erb(node, indicator = nil, &block)
  return node if node.nil? || node.is_a?(String)

  case node.type
  when :str, :sym
    s = node.children.first.to_s
    s = yield s if block_given?
    s
  when :true, :false
    node.type.to_s
  when :nil
    ""
  when :dstr
    node.children.map do |child|
      case child.type
      when :str
        ruby_to_erb(child, indicator, &block)
      when :begin
        ruby_to_erb(child.children.first, indicator, &block)
      else
        raise Error, "unexpected #{child.type} in :dstr node"
      end
    end.join
  else
    "<%#{indicator} #{node.loc.expression.source} %>"
  end
end