Module: MithrilRails::MSX

Defined in:
lib/mithril_rails/msx.rb,
lib/mithril_rails/msx/template.rb

Defined Under Namespace

Classes: Template

Class Method Summary collapse

Class Method Details

.contextObject



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

def self.context
  # lazily loaded during first request and reloaded every time when in dev or test
  unless @context && ::Rails.env.production?
    contents =
      # If execjs uses therubyracer, there is no 'global'. Make sure
      # we have it so JSX script can work properly.
      'var global = global || this;' +

      # search for transformer file using sprockets - allows user to override
      # this file in his own application
      File.read(::Rails.application.assets.resolve('MSXTransformer.js'))

    @context = ExecJS.compile(contents)
  end

  @context
end

.transform(code) ⇒ Object



25
26
27
28
# File 'lib/mithril_rails/msx.rb', line 25

def self.transform(code)
  result = context.call('JSXTransformer.transform', code)
  return result['code']
end