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

#columnObject



37
38
39
# File 'opal/lib/opal/fragment.rb', line 37

def column
  @sexp.column if @sexp
end

#inspectObject

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



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

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

#lineObject



33
34
35
# File 'opal/lib/opal/fragment.rb', line 33

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}:#{@sexp.column}*/#{@code}"
  else
    @code
  end
end