Class: CanCan::Export::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/cancan/export/compiler.rb

Instance Method Summary collapse

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



5
6
7
8
9
# File 'lib/cancan/export/compiler.rb', line 5

def initialize
  @mtimes = {}
  @lines = {}
  @js = {}
end

Instance Method Details

#to_js(block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cancan/export/compiler.rb', line 11

def to_js(block)
  source_lines = definition(block)
  return @js[source_lines] if @js[source_lines]
  
  case block
  when Proc then source_lines[0] =~ / (?:do|{)\s*(?:\|([\s\w\d,]+)\|)?\s*$/
  when Method then source_lines[0] =~ /def [\w\d\.]+\(?([\s\w\d,]+)\)?\s*$/
  end
  arguments = $1.scan(/\w+/)
  
  coffeefied_source_lines = source_lines[1..-1]*"\n"
  coffeefied_source_lines.gsub!(/\.(\w+)\?/, '.is_\1()') # .present? -> .present()
  coffeefied_source_lines.gsub!(/\.(\w+)!/, '.do_\1()') # .sub! -> .sub!()
  coffeefied_source_lines.gsub!(/(\d+)\.([a-z_]+)/, '(\1).\2()') # 4.hours -> (4).hours()
  coffeefied_source_lines.gsub!(/:([\w\d]+)/, '"\1"') # 4.hours -> (4).hours()
  #$log.info coffeefied_source_lines, caller_at: [0, 9..20]
  
  @js[source_lines] = CoffeeScript.compile "(#{arguments*', '}) ->\n#{coffeefied_source_lines}", bare: true
end