Class: Opal::Fragment

Inherits:
Object show all
Defined in:
opal/lib/opal/fragment.rb

Overview

A fragment holds a string of generated javascript that will be written to the destination. It also keeps hold of the original sexp from which it was generated. Using this sexp, when writing fragments in order, a mapping can be created of the original location => target location, aka, source-maps!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, sexp = nil) ⇒ Fragment

Returns a new instance of Fragment



11
12
13
14
# File 'opal/lib/opal/fragment.rb', line 11

def initialize(code, sexp = nil)
  @code = code.to_s
  @sexp = sexp
end

Instance Attribute Details

#codeObject (readonly)

String of javascript this fragment holds



9
10
11
# File 'opal/lib/opal/fragment.rb', line 9

def code
  @code
end

Instance Method Details

#inspectObject

inspect the contents of this fragment, f("fooo")



26
27
28
# File 'opal/lib/opal/fragment.rb', line 26

def inspect
  "f(#{@code.inspect})"
end

#lineObject



30
31
32
# File 'opal/lib/opal/fragment.rb', line 30

def line
  @sexp.line if @sexp
end

#to_codeObject

In debug mode we may wish to include the original line as a comment



17
18
19
20
21
22
23
# File 'opal/lib/opal/fragment.rb', line 17

def to_code
  if @sexp
    "/*:#{@sexp.line}*/#{@code}"
  else
    @code
  end
end