Class: EJX::Template::JS

Inherits:
Object
  • Object
show all
Defined in:
lib/ejx/template/js.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, modifiers = []) ⇒ JS

Returns a new instance of JS.



5
6
7
8
# File 'lib/ejx/template/js.rb', line 5

def initialize(value, modifiers = [])
  @modifiers = modifiers
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/ejx/template/js.rb', line 3

def value
  @value
end

Instance Method Details

#to_js(indentation: 4, var_generator: nil, append: "__output", promises: '__promises') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ejx/template/js.rb', line 10

def to_js(indentation: 4, var_generator: nil, append: "__output", promises: '__promises')
  output = @value
  
  if @modifiers.include? :escape
    if output =~ /\A\s*(var|const|let)\s+(\S+)/
      "#{' '*indentation}#{output}#{output.strip.end_with?(';') ? '' : ';'}\n#{' '*indentation}__ejx_append(#{$2}, #{append}, 'escape', #{promises});\n"
    else
      "#{' '*indentation}__ejx_append(#{output.gsub(/;\s*\Z/, '')}, #{append}, 'escape', #{promises});\n"
    end
  elsif @modifiers.include? :unescape
    "#{' '*indentation}__ejx_append(#{output.gsub(/;\s*\Z/, '')}, #{append}, 'unescape', #{promises});\n"
  elsif @modifiers.include? :comment
    "#{' '*indentation}#{output.index("\n").nil? ? "// #{output}" : "/* #{output.gsub(/\n\s+/, "\n"+(' '*indentation)+"   ")} */"}\n"
  else
    "#{' '*indentation}#{output}\n"
  end
end