Class: Ngannotate::Processor

Inherits:
Sprockets::Processor
  • Object
show all
Defined in:
lib/ngannotate/processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nameObject


5
6
7
# File 'lib/ngannotate/processor.rb', line 5

def self.name
  'Ngannotate::Processor'
end

Instance Method Details

#evaluate(context, locals) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/ngannotate/processor.rb', line 22

def evaluate(context, locals)
  return data if skip

  opt = { add: true }.merge!(parse_opt)
  r = @context.call 'window.annotate', data, opt
  r['src']
end

#parse_optObject


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ngannotate/processor.rb', line 30

def parse_opt
  opt = {}
  opt_str = ENV['NG_OPT']
  if opt_str
    opt = Hash[opt_str.split(',').map { |e| e.split('=') }]
    opt.symbolize_keys!
  end
  if ENV['NG_REGEXP']
    opt[:regexp] = ENV['NG_REGEXP']
  end
  opt
end

#prepareObject


9
10
11
12
13
# File 'lib/ngannotate/processor.rb', line 9

def prepare
  return if skip
  ngannotate_source = File.open(File.join(File.dirname(__FILE__), '../../vendor/ngannotate.js')).read
  @context = ExecJS.compile "window = {};" + ngannotate_source
end

#skipObject

Skip processing in environments where it does not make sense


18
19
20
# File 'lib/ngannotate/processor.rb', line 18

def skip
  ::Rails.env.development? || ::Rails.env.test?
end